vector的begin的基本操作
程序员文章站
2022-03-01 23:18:45
...
#include <vector>
#include <iostream>
int main()
{
using namespace std;
vector<int> c1; //创建一个vector对象c1
vector<int>::iterator c1_Iter; //初始化一个迭代器对象
vector<int>::const_iterator c1_cIter; //初始化一个常量迭代器对象
c1.push_back(1); //添加两个元素1.2
c1.push_back(2);
cout << "The vector c1 contains elements:";
c1_Iter = c1.begin(); //将第一个元素的地址,赋值给迭代器
for (; c1_Iter != c1.end(); c1_Iter++)
{
cout << " " << *c1_Iter;
}
cout << endl;
cout << "The vector c1 now contains elements:";
c1_Iter = c1.begin();
*c1_Iter = 20; //第一个迭代器,解运算符改变了begin地址的值
for (; c1_Iter != c1.end(); c1_Iter++)
{
cout << " " << *c1_Iter;
}
cout << endl;
}
常量迭代器无法改变vector里面的值,而普通的迭代器可以。