C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm Function copy()C++ Algorithm copy() function is used to copy all the elements of the container [first,last] into a different container starting from result. Syntaxtemplate<class InputIterator, class OutputIterator>OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result); Parameterfirst: It is an input iterator to the first element of the range, where the element itself is included in the range. last: It is an input iterator to the last element of the range, where the element itself is not included in the range. result: It is an output iterator to the first element of the new container in which the elements are copied. Return valueAn iterator to the last element of the new range beginning with result is returned. Example 1#include<iostream> #include<algorithm> #include<vector> int main() { int newints[]={15,25,35,45,55,65,75}; std::vector<int> newvector(7); std::copy (newints, newints+7, newvector.begin()); 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: 15 25 35 45 55 65 75 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 accesed. ExceptionsThe function throws an exception if any of the container elements throws one.
Next TopicC++ Algorithm copy_if Function
|