STL(Standard Template Library)中vector容器是最常见的容器之一,设计中经常需要遍历vector容器,本文介绍三种常用的vector遍历方式。
一、下标索引遍历
void traverseVector_1(vector<int> v)
{
for(unsigned int i = 0; i < v.size(); ++i)
{
cout<<v[i]<<" ";
}
cout<<endl;
}
二、迭代器遍历
#include <iostream>
#include <vector>
using namespace std;
void traverseVector_2(vector<int> v)
{
vector<int>::iterator it = v.begin();
for(; it != v.end(); ++it)
{
cout<<(*it)<<" ";
}
cout<<endl;
}
三、copy函数遍历
#include <iostream>
#include <vector>
using namespace std;
void traverseVector_3(vector<int> v)
{
copy(v.begin(), v.end(), ostream_iterator<int>(cout, " ") );
cout<<endl;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)