std::move_backward in C++
Moves the elements in the range [first,last] starting from the end into the range terminating at result.
The function begins by moving *(last-1) into *(result-1), and then follows backward by the elements preceding these, until first is reached (and including it).
Template :
BidirectionalIterator2 move_backward (BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result);
result
Bidirectional iterator to the past-the-end position in the destination sequence.
This shall not point to any element in the range [first,last].
Return Type :
An iterator to the first element of the destination sequence where elements
have been moved.
Examples:
Input : vec1 contains : 3 4 5 7 8 vec2 contains : 8 9 6 2 4 7 Output : arr2 contains : 8 9 6 5 7 8 /*3 elements from 3rd position of vector vec1 moved to starting 4th position of vec2*/
// CPP program to illustrate // std::move and std::move_backward // STL library functions #include<bits/stdc++.h> // Driver code int main() { std :: vector <int> vec1 {1, 2, 3, 4, 5}; std :: vector <int> vec2 {7, 7, 7, 7, 7}; // Print elements std :: cout << "Vector1 contains :"; for(int i = 0; i < vec1.size(); i++) std :: cout << " " << vec1[i]; std :: cout << "\n"; // Print elements std :: cout << "Vector2 contains :"; for(unsigned int i = 0; i < vec2.size(); i++) std :: cout << " " << vec2[i]; std :: cout << "\n\n"; // std :: move_backward function std :: move_backward (vec2.begin(), vec2.begin() + 3, vec1.begin() + 3); // Print elements std :: cout << "Vector1 contains after std::move_backward function:"; for(unsigned int i = 0; i < vec1.size(); i++) std :: cout << " " << vec1[i]; std :: cout << "\n"; return 0; } |
chevron_right
filter_none
Output:
Vector1 contains : 1 2 3 4 5 Vector2 contains : 7 7 7 7 7 Vector1 contains after std::move_backward function: 7 7 7 4 5
This article is contributed by Sachin Bisht. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Find the Deepest Node in a Binary Tree Using Queue STL - SET 2
- C++ program to compare two Strings using Operator Overloading
- not1 and not2 function templates in C++ STL with Examples
- Go vs C++
- Trie Data Structure using smart pointer and OOP in C++
- Modular Exponentiation of Complex Numbers
- std::hash class in C++ STL
- History of C++
- What are Forward declarations in C++
- __builtin_inf() functions of GCC compiler
- Structures in C++
- Variables in C++
- Implementing Forward Iterator in BST
- Count substrings that contain all vowels | SET 2
- Machine Learning in C++



