一、迭代器的概念
迭代器是顺序容器和关联容器的对象元素进行连续访问。
操作时,需要保存容器对象的首部和尾部,进而可以连续访问对象中的元素了。
迭代器内置于 顺序容器对象中的一般化方法:iterator
iterator可存放 容器对象的成员函数:begin 与 end:
begin:返回一个iterator指向容器的第一个元素
end: 返回一个iterator指向容器的最后一个元素
二、通过vector容器中的迭代器方法,遍历容器对象中的元素
① vector 容器 基本的元素全部遍历
#include <iostream>
#include <vector>
#include <string>
#include <string.h>
using namespace std;
int main()
{
//定义一个vector容器对象
string str;
vector <string> s_vec;//定义一个容量为5的vector容器对象
char buf[20];
for(int n=0; n<5; n++) //往vector容器对象中添加元素
{
memset(buf,0,20);
sprintf(buf,"%s%d%s","李大脚",n,"号");
string s = buf;
s_vec.push_back(s);
}
//定义一个iterator
vector <string>::iterator iter;
for(iter = s_vec.begin(); iter<s_vec.end(); iter++)//遍历容器对象中的每一个元素
{
cout << *iter << endl;
}
return 0;
}
运行结果:
李大脚0号
李大脚1号
李大脚2号
李大脚3号
李大脚4号
② 通过迭代器,定义一个新的vector对象拷贝就的vector对象的部分元素
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
//定义一个vector 对象
vector <int> first_s;
for(int n=0; n<10; n++)
{
first_s.push_back(n);
}
vector <int>::iterator itmp;
cout<<"旧对象元素:";
for(itmp = first_s.begin(); itmp<first_s.end(); itmp++)
{
cout << *itmp;
}
cout<<endl;
vector <int> second_s;//定义第二个对象进行拷贝
cout<<"进行拷贝,拷贝后半部分的元素"<<endl;
for(itmp = first_s.begin()+first_s.size()/2; itmp<first_s.end(); itmp++)
{
second_s.push_back(*itmp);
}
cout<<"新对象元素:";
for(itmp = second_s.begin(); itmp<second_s.end(); itmp++)
{
cout << *itmp;
}
cout<<endl;
return 0;
}
运行结果:
旧对象元素:0123456789
进行拷贝,拷贝后半部分的元素
新对象元素:56789
③ vector可通过数组首元素地址以及最后元素后面的地址来初始化
1)string 数组初始化string vector对象
#include <iostream>
#include <vector>
#include <stireg.h>
using namespace std;
int main()
{
string words[] = {"Dante","Vivan","Alisa"};
vector <string> s_vec(words,words+3);//初始化vector对象
vector <string>::iterator s_tp;
for(s_tp = s_vec.begin(); s_tp<s_vec.end(); s_tp++)//输出对象元素
{
cout << *s_tp <<endl;
}
return 0;
}
运行结果:
Dante
Vivan
Alisa
2)int 数组初始化int vector对象
#include <iostream>
#include <vector>
#include <stireg.h>
using namespace std;
int main()
{
int words[] = {1,2,3,4,555};
vector <int> s_vec(words,words+5);//初始化vector对象
vector <int>::iterator s_tp;
for(s_tp = s_vec.begin(); s_tp<s_vec.end(); s_tp++)//输出对象元素
{
cout << *s_tp <<endl;
}
return 0;
}
运行结果:
1
2
3
4
555
三、iterator指针得到其他应用
① 在vector容器对象头部插入元素可这样写:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
//初始化一个string类型的容器对象
vector <string> v_s(1,"李大蕉");
//头部添加元素
vector <string>::iterator v_p = v_s.begin();
cout<< "插入之元素前:"<< *v_p <<endl;
v_s.insert(v_s.begin(),"周树苗");
v_p = v_s.begin();
cout<< "插入之元素后:"<< *v_p <<endl;
return 0;
}
运行结果:
插入之元素前:李大蕉
插入之元素后:周树苗
② 在vector容器对象中指定位置插入指定数量元素
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
//定义一个string类型的 vector 对象
vector <string> v_s(1,"vivia");
vector <string> :: iterator v_p = v_s.begin();
cout << "添加元素之前:";
for(; v_p < v_s.end(); v_p++)
{
cout<< *v_p <<endl;
}
v_s.insert(v_s.begin()+1,9,"Dante");//在di'er第二个位置添加9个Dante新元素
v_p = v_s.begin();
cout << "添加元素之后:";
for(; v_p < v_s.end(); v_p++)
cout<< *v_p <<" ";
cout<<endl;
return 0;
}
运行结果
添加元素之前:vivia
添加元素之后:vivia Dante Dante Dante Dante Dante Dante Dante Dante Dante
③ 在vector容器对象中插入指定内存地址范围内的数据元素
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string s_array[3] = {"Dante","Vivan","Aiver"};
vector <string> v_s(3);
vector <string>::iterator v_p = v_s.begin();
cout<<"添加元素之前:";
for(; v_p < v_s.end(); v_p++)
cout << *v_p <<" ";
cout<<endl;
v_s.insert(v_s.begin(),s_array,s_array+3);
v_p = v_s.begin();
cout<<"添加元素之后:";
for(; v_p < v_s.end(); v_p++)
cout << *v_p <<" ";
cout<<endl;
return 0;
}
运行结果:
添加元素之前:
添加元素之后:Dante Vivan Aiver
④ 删除vector容器对象中的元素
1)指定位置删除
使用vector内置方法erase方法,只需要把要删除的元素对应的iterator指针
传给erase即可删除对应的元素。
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string s_array[] = {"Dante","Vian","Alisi"};
vector <string> v_s;
//添加数据
v_s.insert(v_s.begin(),s_array,s_array+3);
cout<<"删除之前:";
vector <string>::iterator v_p = v_s.begin();
for(; v_p < v_s.end(); v_p++)
{
cout<<*v_p<<" ";
}
cout<<endl;
//删除Alisi
v_s.erase(v_s.begin()+2);
v_p = v_s.begin();
cout<<"删除之后:";
for(; v_p < v_s.end(); v_p++)
{
cout<<*v_p<<" ";
}
cout<<endl;
return 0;
}
运行结果:
删除之前:Dante Vian Alisi
删除之后:Dante Vian
2)全部元素删除 – v_s.erase(v_s.begin(),v_s.end());
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main()
{
string s_array[] = {"Dante","Vian","Alisi"};
vector <string> v_s;
//添加数据
v_s.insert(v_s.begin(),s_array,s_array+3);
cout<<"删除之前:";
vector <string>::iterator v_p = v_s.begin();
for(; v_p < v_s.end(); v_p++)
{
cout<<*v_p<<" ";
}
cout<<endl;
//删除全部元素
v_s.erase(v_s.begin(),v_s.end());
v_p = v_s.begin();
cout<<"删除之后:";
for(; v_p < v_s.end(); v_p++)
{
cout<<*v_p<<" ";
}
cout<<endl;
return 0;
}
运行结果:
删除之前:Dante Vian Alisi
删除之后: