Below are the some of the member functions for modifying operations:
copy : It is used to copy range of elements
copy_n : It is used to copy elements
move : It is used to move range of elements
move_backward : It is used to move range of elements backward
replace : It is used to replace value in range
replace_if : It is used to replace values in range
generate : It is used to generate values for range with function
remove : It is used to remove value from range
unique : It is used to remove consecutive duplicates in range
#include <iostream> #include <vector> #include <algorithm> //for more tutorials on C, C++, STL, DS visit www.ProDeveloperTutorial.com using namespace std; int main () { int myints[]={100, 200, 300, 400, 500, 600, 700}; std::vector<int> myvector (7); std::copy ( myints, myints+7, myvector.begin() ); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; std::vector<int> myvector_1 (5); //copy only 5 elements using copy_n std::copy_n ( myints, 5, myvector_1.begin() ); std::cout << "\nmyvector_1 contains:"; for (std::vector<int>::iterator it = myvector_1.begin(); it!=myvector_1.end(); ++it) std::cout << ' ' << *it; std::vector<std::string> foo = {"one","two","three","four"}; std::vector<std::string> bar (4); // moving ranges: std::cout << "\nMoving ranges...\n"; std::move ( foo.begin(), foo.begin()+4, bar.begin() ); std::cout << "foo contains " << foo.size() << " elements:"; std::cout << " (each in an unspecified but valid state)"; std::cout << '\n'; std::cout << "bar contains " << bar.size() << " elements:"; for (std::string& x: bar) std::cout << " [" << x << "]"; std::cout << '\n'; //replace all occurance of 200 with 99 std::replace (myvector.begin(), myvector.end(), 200, 99); std::cout << "myvector contains:"; for (std::vector<int>::iterator it = myvector.begin(); it!=myvector.end(); ++it) std::cout << ' ' << *it; return 0; }
Output:
myvector contains: 100 200 300 400 500 600 700 myvector_1 contains: 100 200 300 400 500 Moving ranges... foo contains 4 elements: (each in an unspecified but valid state) bar contains 4 elements: [one] [two] [three] [four] myvector contains: 100 99 300 400 500 600 700