Różnice między wybraną wersją a wersją aktualną.
— |
partial_sum [2009/04/29 16:31] (aktualna) sosna.a utworzono |
||
---|---|---|---|
Linia 1: | Linia 1: | ||
+ | ====== Algorytm partial_sum ====== | ||
+ | Zadaniem algorytmu jest liczenie sum częściowych. | ||
+ | Innymi słowy liczy on sumę wszystkich elementów poprzedzających daną pozycję w ciągu na którym zostanie użyty i wstawia je do ciągu 'result'. Występuje on również w drugiej 'wersji', gdzie sumowanie elementów zastępuje się inną, wybraną przez programistę operacją. | ||
+ | |||
+ | ===== Nagłówek ===== | ||
+ | <code cpp> | ||
+ | #include <numeric> | ||
+ | </code> | ||
+ | |||
+ | ===== Definicje ===== | ||
+ | == partial_sum 'wersja' podstawowa == | ||
+ | <code cpp> | ||
+ | |||
+ | |||
+ | template <class InputIterator, class OutputIterator> | ||
+ | |||
+ | OutputIterator partial_sum ( InputIterator first, InputIterator last, | ||
+ | |||
+ | OutputIterator result ) | ||
+ | |||
+ | { | ||
+ | |||
+ | iterator_traits<InputIterator>::value_type val; | ||
+ | |||
+ | *result++ = val = *first++; | ||
+ | |||
+ | while (first!=last) | ||
+ | |||
+ | *result++ = val = val + *first++; | ||
+ | |||
+ | return result; | ||
+ | |||
+ | } | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | </code> | ||
+ | == partial_sum 'wersja' pozwalająca na użycie algorytmu z operacją inną niż sumowanie== | ||
+ | <code cpp> | ||
+ | |||
+ | template <class InputIterator, class OutputIterator> | ||
+ | |||
+ | OutputIterator partial_sum ( InputIterator first, InputIterator last, | ||
+ | |||
+ | OutputIterator result, BinaryOperation binary_op ) | ||
+ | |||
+ | { | ||
+ | |||
+ | iterator_traits<InputIterator>::value_type val; | ||
+ | |||
+ | *result++ = val = *first++; | ||
+ | |||
+ | while (first!=last) | ||
+ | |||
+ | *result++ = val = binary_op(val,*first++) for binary_op version | ||
+ | |||
+ | return result; | ||
+ | |||
+ | } | ||
+ | </code> | ||
+ | |||
+ | ===== Parametry ===== | ||
+ | <code> | ||
+ | first - początek zakresu źródłowego | ||
+ | last - koniec zakresu źródłowego | ||
+ | result - iterator wyjściowy wskazujący miejsce zapisu | ||
+ | binary_op - operacja (obiekt lub funkcja) przyjmująca za argumenty elementy wskazywane przez iteratory z | ||
+ | przedziału [first, last] i zwracająca wynik swojego działania (zamiast sumy). | ||
+ | </code> | ||
+ | |||
+ | ===== Wartość zwracana ===== | ||
+ | <code> | ||
+ | Iterator na koniec stworzonego ciągu. | ||
+ | </code> | ||
+ | |||
+ | ===== Warunki użycia ===== | ||
+ | * zakres [first, last) jest prawidłowy\\ | ||
+ | |||
+ | ===== Złożoność ===== | ||
+ | liniowa | ||
+ | |||
+ | ===== Przykłady użycia ===== | ||
+ | <code cpp> | ||
+ | #include <iostream> | ||
+ | #include <functional> | ||
+ | #include <numeric> | ||
+ | |||
+ | using namespace std; | ||
+ | |||
+ | int moja_funkcja(const int& a, const int& b) { return (a+b+50); } | ||
+ | |||
+ | |||
+ | int main () { | ||
+ | int wartosci[] = {1,2,3,4,5,6,7}; | ||
+ | int wynik[7]; | ||
+ | |||
+ | partial_sum (wartosci, wartosci+7, wynik); | ||
+ | cout << "Uzycie standardowej wersji partial_sum: "; | ||
+ | for (int i=0; i<7; ++i) { cout << wynik[i] << ' '; } | ||
+ | cout << endl; | ||
+ | |||
+ | partial_sum (wartosci, wartosci+7, wynik, multiplies<int>()); | ||
+ | cout << "Uzycie funkcji multiplies: "; | ||
+ | for (int i=0; i<7; ++i) { cout << wynik[i] << ' '; } | ||
+ | cout << endl; | ||
+ | |||
+ | partial_sum (wartosci, wartosci+7, wynik, moja_funkcja); | ||
+ | cout << "Uzycie wlasnej funkcji: "; | ||
+ | for (int i=0; i<7; ++i) { cout << wynik[i] << ' '; } | ||
+ | cout << endl; | ||
+ | return 0; | ||
+ | } | ||
+ | </code> |