std ::移动
将[first,last]范围内的元素移到从结果开始的范围内。
[first,last]中元素的值将传输到结果所指向的元素。调用之后,[first,last]范围内的元素处于未指定但有效的状态。
模板 :
OutputIterator move (InputIterator first, InputIterator last, OutputIterator result);
Parameters :
first, last
Input iterators to the initial and final positions in a sequence
to be moved. The range used is [first,last], which contains all
the elements between first and last, including the element pointed
by first but not the element pointed by last.
result
Output iterator to the initial position in the destination sequence.
This shall not point to any element in the range [first,last].
Return type :
An iterator to the end of the destination range where elements have been moved.
例子:
Input :
vec1 contains : 1 2 3 4 5
vec2 contains : 7 7 7 7 7
Output :
arr2 contains : 7 1 2 3 4
/*First 4 elements of vector vec1 moved to vec2 starting second position*/
// CPP program to illustrate
// std::move and std::move_backward
// STL library functions
#include
// Driver code
int main()
{
std :: vector vec1 {1, 2, 3, 4, 5};
std :: vector 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 function
// move first 4 element from vec1 to starting position of vec2
std :: move (vec1.begin(), vec1.begin() + 4, vec2.begin() + 1);
// Print elements
std :: cout << "Vector2 contains after std::move function:";
for(unsigned int i = 0; i < vec2.size(); i++)
std :: cout << " " << vec2[i];
std :: cout << "\n";
return 0;
}
输出:
Vector1 contains : 1 2 3 4 5
Vector2 contains : 7 7 7 7 7
Vector2 contains after std::move function: 7 1 2 3 4
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。