先决条件: C++ STL,C++ STL中的迭代器
迭代器不是迭代任何STL容器的唯一方法。存在一种更好且有效的方法,可以在不使用迭代器的情况下迭代向量。可以使用存储在任何容器中的值对其进行迭代。以下是向量的语法:
句法:
for(auto itr : vector_name)
说明: itr是存储在vector中的值,用于遍历矢量。下面是说明相同内容的程序:
// C++ program to illustrate the above
// topic
#include
using namespace std;
// Driver Code
int main()
{
// Declare the vector
vector arr = { 1, 2, 3, 4 };
// Traversing the vector using
// values directly
for (auto& it : arr) {
// Print the values
cout << it << ' ';
}
return 0;
}
输出:
1 2 3 4
更新向量中的值:要在不使用迭代器的情况下更新向量中的值,请使用引用遍历向量中存储的值并更新值。以下是相同的语法:
句法:
for(auto &itr : vector_name)
说明:这里itr是向量中存储的值的地址,该值用于遍历向量。下面是说明相同内容的程序:
// C++ program to illustrate the updation
// in vector without using iterator
#include
using namespace std;
// Function to update the value in vector
void updateVector(vector arr)
{
cout << "Vector Before Update: ";
for (auto& it : arr) {
cout << it << ' ';
}
// Traverse using the reference to value
// and multiply each value by 2
for (auto& it : arr) {
it *= 2;
}
cout << "\nVector After Update: ";
// Print vector elements
for (auto& it : arr) {
cout << it << ' ';
}
}
// Driver Code
int main()
{
// Declare the vector
vector arr = { 1, 2, 3, 4 };
// Function Call
updateVector(arr);
return 0;
}
输出:
Vector Before Update: 1 2 3 4
Vector After Update: 2 4 6 8
好处:
- 简单易写的代码。
- 比使用迭代器方法更好,更高效。
缺点:
- 它仅在向前方向上进行迭代。
- 不设任何计数器,即,我们无法通过此遍历找到任何元素的索引。为了对元素进行计数,必须明确使用计数器。
我们还可以在C++中的许多不同容器中使用相同的遍历进行迭代。以下是相同的插图:
- 地图:
// C++ program to illustrate the iteration // in Map without using iterator #include
using namespace std; // Driver Code int main() { // Declare the map map Mp; // Inserting values in Map Mp[1] = 1; Mp[2] = 2; Mp[3] = 3; // Iterate using value in Map for (auto it : Mp) { // Print the elements cout << it.first << ' ' << it.second << endl; } return 0; } 输出:1 1 2 2 3 3
- 向量图:
// C++ program to illustrate the iteration // in Map of vectors without using iterator #include
using namespace std; // Driver Code int main() { // Declare the map of vectors map > Mp; // Temporary vector vector temp = { 1, 2, 3 }; // Inserting values in Map Mp[1] = temp; temp = { 2, 3, 8, 9 }; Mp[2] = temp; temp = { 10, -2 }; Mp[3] = temp; // Iterate using value in Map of vectors for (auto it : Mp) { // Print the elements cout << it.first << " -> "; // Traverse each vector map // with it.first and print the // elements for (auto jt : it.second) { cout << jt << ' '; } cout << endl; } return 0; } 输出:1 -> 1 2 3 2 -> 2 3 8 9 3 -> 10 -2
- 放:
// C++ program to illustrate the iteration // in set without using iterator #include
using namespace std; // Driver Code int main() { // Declare the set set S; // Inserting values in set S.insert(3); S.insert(-1); S.insert(3); S.insert(4); // Iterate using value in set for (auto it : S) { // Print the elements cout << it << ' '; } return 0; } 输出:-1 3 4
- 双端队列:
// C++ program to illustrate the iteration // in deque without using iterator #include
using namespace std; // Driver Code int main() { // Declare the deque deque dq; // Inserting values in deque dq.push_front(1); dq.push_front(2); dq.push_front(3); dq.push_back(4); dq.push_back(5); // Iterate using value in set for (auto it : dq) { // Print the elements cout << it << ' '; } return 0; } 输出:3 2 1 4 5
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。