C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm Functions copy_backward()C++ Algorithm copy_backward() function is used for copying of elements in the backward order, it accepts three arguments and then copies the elements belonging to the range [first,last]. The copying of elements begins in the reverse order with termination point at 'result'. Syntaxtemplate<class BidirectionalIterator1, class BidirectionalIterator2> BidirectionalIterator2 copy_backward(BidirectionalIterator1 first, BidirectionalIterator1 last, BidirectionalIterator2 result); Parameterfirst: It is a bidirectional iterator to the first element of the range, where the element itself is included in the range. last: It is a bidirectional iterator to the last element of the range, where the element itself is not included in the range. result: It is a bidirectional iterator to the final position of copied elements. Return valueThe function returns an iterator of the first element to the sequence of copied ones. Example 1#include <iostream> #include <algorithm> #include <vector> int main () { std::vector<int> newvector; for (int k=1; k<=5; k++) newvector.push_back(k*5); newvector.resize(newvector.size()+3); std::copy_backward ( newvector.begin(), newvector.begin()+5, newvector.end() ); std::cout << "newvector contains:"; for (std::vector<int>::iterator ti=newvector.begin(); ti!=newvector.end(); ++ti) std::cout << ' ' << *ti; std::cout << '\n'; return 0; } Output: newvector contains: 5 10 15 5 10 15 20 25 ComplexityThe complexity of the function is linear starting from the first element to the last one. Data racesSome or all of the container objects are accessed. ExceptionsThe function throws an exception if any of the container elements throws one.
Next TopicC++ Algorithm copy_n Function
|