std :: advance将迭代器“ it”前进n个元素位置。
句法 :
template
void advance (InputIterator& it, Distance n);
it : Iterator to be advanced
n : Number of element positions to advance.
This shall only be negative for random-access and bidirectional iterators.
Return type :
None.
动机问题:给出了一个向量容器。任务是打印备用元素。
例子 :
Input : 10 40 20 50 80 70
Output : 10 20 80
// C++ program to illustrate
// using std::advance
#include
// Driver code
int main()
{
// Vector container
std::vector vec;
// Initialising vector
for (int i = 0; i < 10; i++)
vec.push_back(i * 10);
// Printing the vector elements
for (int i = 0; i < 10; i++) {
std::cout << vec[i] << " ";
}
std::cout << std::endl;
// Declaring the vector iterator
std::vector::iterator it = vec.begin();
// Printing alternate elements
while (it < vec.end()) {
std::cout << *it << " ";
std::advance(it, 2);
}
}
输出:
0 10 20 30 40 50 60 70 80 90
0 20 40 60 80
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。