C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
C++ Algorithm Function move ()C++ Algorithm move()function is used for moving the elements. It accepts three arguments and then moves the elements belonging to the range [first,last) into a range that starts with 'result'. Syntaxtemplate<class InputIterator, class OutputIterator> OutputIterator move(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 initial position of the moved elements. Return valueThe function returns an iterator of the first element to the sequence of moved ones. Example 1#include <iostream> #include <algorithm> #include <utility> #include <vector> #include <string> int main () { std::vector<std::string> a = {"suraj","aman","vanshika","chhavi"}; std::vector<std::string> b (4); std::cout << "Move function.\n"; std::move ( a.begin(), a.begin()+4, b.begin() ); std::cout << "a contains " << a.size() << " elements:"; std::cout << " (The state of which is valid.)"; std::cout << '\n'; std::cout << "b contains " << b.size() << " elements:"; for (std::string& x: b) std::cout << " [" << x << "]"; std::cout << '\n'; std::cout << "Moving the conatiner a...\n"; a = std::move (b); std::cout << "a contains " << a.size() << " elements:"; for (std::string& x: a) std::cout << " [" << x << "]"; std::cout << '\n'; std::cout << "b is in valid state"; std::cout << '\n'; return 0; } Output: Move function. a contains 4 elements: (The state of which is valid.) b contains 4 elements: [suraj] [aman] [vanshika] [chhavi] Moving the conatiner a... a contains 4 elements: [suraj] [aman] [vanshika] [chhavi] b is in valid state Example 2#include<bits/stdc++.h> int main() { std :: vector <int> u1 {9, 14, 21, 18}; std :: vector <int> u2 {14, 14, 14, 14}; std :: cout << "u1 contains :"; for(int j = 0; j < u1.size(); j++) std :: cout << " " << u1[j]; std :: cout << "\n"; std :: cout << "u2 contains :"; for(unsigned int j = 0; j < u2.size(); j++) std :: cout << " " << u2[j]; std :: cout << "\n\n"; std :: move (u1.begin(), u1.begin() + 4, u2.begin() + 1); std :: cout << "u2 contains after move function:"; for(unsigned int j = 0; j < u2.size(); j++) std :: cout << " " << u2[j]; std :: cout << "\n"; return 0; } Output: u1 contains : 9 14 21 18 u2 contains : 14 14 14 14 u2 contains after move function: 14 9 14 21 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 all_of Function
|