C++ vector容器的多种遍历方式

vector容器的遍历方式

#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>

using namespace std;

template<typename Type> //类型模板 
Type v_print(const Type val)
{
	cout<<val<<" "; //有了模板val可以使用多种类型 
	return val;
}

int v_assign(int &i) 
{
	i=rand()%10;    //头文件<cstdlib>
	return i;
} 

int main(void)
{
	int a[]={10,11,12,3,4,5,6,7,8,9};
	int len=sizeof(a)/sizeof(a[0]); 

//方式一:普通for循环,下标法
	cout<<"一、普通for循环:"<<endl; 
	vector <int> v1;
	v1.reserve(len); //预留空间后才能用下标法赋值,否则崩溃 
	for (int i=0;i<len;i++) v1[i]=a[i];
	cout<<"当前vector的大小:"<<v1.size()<<endl;  //虽然成功赋值但size()依旧为0 
	for (int i=0;i<len;i++) cout<<v1[i]<<" "; cout<<endl;
	
	v1.resize(len); //resize可行但仅为测试。不如直接声明 v1(10); 
	for (int i=0;i<len;i++) v1[i]=a[i];
	cout<<"当前vector的大小:"<<v1.size()<<endl;
	for (int i=0;i<len;i++) cout<<v1.at(i)<<" "; cout<<endl<<endl;

//方式二:for auto循环,C++11新语句
	cout<<"二、for auto循环:"<<endl; 
	vector <int> v2;
	for (auto arr:a) v2.push_back(arr);
	for (auto vec:v2) cout<<vec<<" "; cout<<endl<<endl;
	/*显而易见,for auto比方式一更简洁 */ 
	for (auto &v:v2) cout<<(++v)++<<" "; cout<<endl;
	for (auto vec:v2) cout<<vec<<" "; cout<<endl<<endl;
	//for (auto &v:vector) 用&v还能在循环中改变vector元素的值 
	
//方式三:迭代器
	//迭代器 iterator 是一种用于遍历容器元素的数据类型
	//vector<T>::iterator iter;//定义一个T类型vector的迭代器
    //除了这种普通迭代器,还有插入迭代器、流迭代器、反向迭代器和移动迭代器
	//操作符 *iter 访问迭代器所指向的元素的值,与指针类似 
	//只读迭代器声明时用: vector<T>::const_iterator const_iter;
	cout<<"三、迭代器方式:"<<endl; 
	vector <int> v3(10);
	for(vector<int>::size_type ix=0;ix!=v3.size();ix++) v3[ix]=rand()%10;
	//vector<int>::size_type 等价于unsigned int
	cout<<"for语句:"<<endl;
	vector<int>::iterator it=v3.begin(); 
	for(;it!=v3.end();it++) cout<<*it<<" "; cout<<endl;
	//或者写成一行: for(vector<int>::iterator it=v3.begin();it!=v3.end();it++)...
    //或用auto关键字更容易理解: for(auto it=v3.begin();it!=v3.end();it++)...
	cout<<"while语句:"<<endl;
	it=v3.begin(); //迭代器指回到起始位置 
	while (it!=v3.end()) cout<<*(it++)<<" "; cout<<endl; //一定要it++,不能++it 
	cout<<"for auto语句:"<<endl;
	for (auto it=v3.begin();it!=v3.end();it++) cout<<*it<<" "; cout<<endl<<endl;
	
//方式四:for_each() 必须头文件<algorithm>
	cout<<"四、for_each()方式:"<<endl; 
	vector <int> v4(10);
	cout<<"调用库函数或自定义函数:"<<endl;
    for_each(v4.begin(), v4.end(), v_assign); //调用自定义函数v_assign()随机赋个一位整数 
	for_each(v4.begin(), v4.end(), v_print<int>); //调用自定义函数v_print()列印元素 
	cout<<endl;
	cout<<"直接使用Lambda匿名函数:"<<endl;
	for_each(v4.begin(), v4.end(), [](const int& val)->void{cout<<val<<" ";});
	cout<<endl<<endl;		//关于Lambda函数另找时间讨论学习

//方式五:transform() 必须头文件<algorithm>
	cout<<"五、transform()方式:"<<endl; 
	vector <int> v5(10);
	transform(v5.begin(),v5.end(),v5.begin(),v_assign);
	transform(v5.begin(),v5.end(),v5.begin(),v_print<int>); cout<<endl;
	transform(v5.begin(),v5.end(),v5.begin(),[](const int& val)->int{cout<<val<<" ";});
	cout<<endl<<endl;

//方式六:copy()+迭代器
	cout<<"六、copy()+迭代器:"<<endl; 
	vector<int> tmp(3); //创建3个元素的临时容器,默认值{0,0,0}
	copy(v4.begin(),v4.end(),back_inserter(tmp)); //插入迭代器 back_inserter (尾部追加)
	for_each(tmp.begin(), tmp.end(), v_print<int>); cout<<endl;
	copy(v4.begin()+1,v4.begin()+5, inserter(tmp,tmp.begin()+1)); //插入迭代器 inserter
	for_each(tmp.begin(), tmp.end(), v_print<int>); cout<<endl<<endl;
	
	vector <string> v6(10,"a");	
	ostream_iterator<string> output(cout," "); //流迭代器 ostream_iterator
	copy(v6.begin(),v6.end(),output); cout<<endl<<endl;
	//或者写成一行:copy(v6.begin(), v6.end(), ostream_iterator<string>(cout, " "));

	//最后测试一下用 v_print()输出string
	for_each(v6.begin(), v6.end(), v_print<string>); cout<<endl;
	transform(v6.begin(), v6.end(), v6.begin(), v_print<string>); 
	
	return 0;
}

输出结果:

一、普通for循环:
当前vector的大小:0
10 11 12 3 4 5 6 7 8 9
当前vector的大小:10
10 11 12 3 4 5 6 7 8 9

二、for auto循环:
10 11 12 3 4 5 6 7 8 9
11 12 13 4 5 6 7 8 9 10
12 13 14 5 6 7 8 9 10 11

三、迭代器方式:
for语句:
1 7 4 0 9 4 8 8 2 4
while语句:
1 7 4 0 9 4 8 8 2 4
for auto语句:
1 7 4 0 9 4 8 8 2 4

四、for_each()方式:
调用库函数或自定义函数:
5 5 1 7 1 1 5 2 7 6
直接使用Lambda匿名函数:
5 5 1 7 1 1 5 2 7 6

五、transform()方式:
1 4 2 3 2 2 1 6 8 5
1 4 2 3 2 2 1 6 8 5

六、copy()+迭代器:
0 0 0 5 5 1 7 1 1 5 2 7 6
0 5 1 7 1 0 0 5 5 1 7 1 1 5 2 7 6

a a a a a a a a a a

a a a a a a a a a a
a a a a a a a a a a
--------------------------------
Process exited after 0.4503 seconds with return value 0
请按任意键继续. . .

vector[i]和vector.at(i)区别

后者比前者多了对i是否越界的检查,如i>=size()会抛出std::out_of_range异常,建议使用后者。

for_each()和transform()的区别

1.前者靠形参&引用方式传递可以是void型函数,后者必须有return来返回值;
2.后者还有5个参数的形式,比如可以将两个源容器a和b的元素相加,并将结果追加到目标容器c尾部。
       如:transform(a.begin(), a.end(), b.begin(), back_inserter(c), aplusb<int>());  //back_inserter 是插入迭代器之一

#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>

using namespace std;

template <class Type>Type apb (Type a, Type b)
{
    return (a+b);
}

int main(void)
{
 	vector<int> a(4),b={1,2,3,4,5,6,7};
 	for (auto v:b) cout<<v<<" "; cout<<endl;

	copy(b.begin(), b.end(), inserter(a,a.begin()+4));
	for (auto v:a) cout<<v<<" "; cout<<endl;

	vector<int> tmp;
	transform(b.begin(), b.end(), a.begin(), back_inserter(tmp), apb<int>);
   	for (auto v:tmp) cout<<v<<" "; cout<<endl;
    
}

/*
运行结果:

1 2 3 4 5 6 7
0 0 0 1 2 3 4 5 6 7 0
1 2 3 5 7 9 11

--------------------------------
Process exited after 0.614 seconds with return value 0
请按任意键继续. . .
*/

 

vector 相关文章:

C++ vector声明和赋值的相关函数

C++ vector容器的多种遍历方式

C++ vector 删除和排序的相关函数

C++ vector 赋值、删除、排序类之外的其他函数

C++ vector 容器的全排列算法 next_permutation

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Hann Yang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值