1、构造函数
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int main ()
{
string str[]={"Alex","John","Robert"};
// 空的 vector 对象
vector<int> v1;
// 新建一个包含10个空元素的 vector 对象
vector<int> v2(10);
// 新建一个包含10个值为0的 vector 对象
vector<int> v3(10,0);
// 新建一个 vector 容器并用string数组初始化
vector<string> v4(str+0,str+3);
vector<string>::iterator sIt = v4.begin();
while ( sIt != v4.end() )
cout << *sIt++ << " ";
cout << endl;
// 拷贝构造
vector<string> v5(v4);
for ( int i=0; i<3; i++ )
cout << v5[i] << " ";
cout << endl;
return 0;
}
运行结果:
// Alex John Robert
// Alex John Robert
2、assign——初始化或者重置容器内元素
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
using namespace std;
int main ()
{
int ary[]={1,2,3,4,5};
vector<int> v;
// 将ary数组成员分配给v容器
v.assign(ary,ary+5);
copy(v.begin(),v.end(),
ostream_iterator<int>(cout," "));
cout << endl;
// 用3个100重置v容器
v.assign(3,100);
copy(v.begin(),v.end(),
ostream_iterator(cout," "));
cout << endl;
return 0;
}
运行结果:
// 1 2 3 4 5
// 100 100 100
3、at——按照指定偏移量访问容器元素,类似于[]运算符
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> v(3,0);
v[0] = 100;
v.at(1) = 200;
for ( int i=0; i<3; i++ )
cout << v.at(i) << " ";
cout << endl;
return 0;
}
运行结果:
// 100 200 0
4、back——返回容器的最后一个元素值,等价于*(容器.end()-1)或容器[容器.size-1]
#include <iostream>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
template<class T, class D>
class Member
{
public:
Member(T t, D d) : name(t), sal(d) {}
void print();
private:
T name;
D sal;
};
template<class T, class D>
void Member::print()
{
cout << name << " " << sal << endl;
}
//======================================
int main ()
{
typedef Member<string,double> M;
vector<M> v;
v.push_back(M("Robert",60000));
v.push_back(M("Linda",75000));
vector<M>::iterator It = v.begin();
cout << "Entire vector:" << endl;
while ( It != v.end() )
(It++)->print();
cout << endl;
cout << "Return from back()" << endl;
v.back().print();
return 0;
}
运行结果:
// Entire vector:
// Robert 60000
// Linda 75000
//
// Return from back()
// Linda 75000
5、begin——返回容器头
#include <iostream>
#include <vector>
#include <iterator>
#include <numeric>
using namespace std;
int main ()
{
vector<int> v(5);
iota(v.begin(),v.end(),1);
vector<int>::iterator It = v.begin();
while ( It != v.end() )
cout << *It++ << " ";
cout << endl;
// third element of the vector
It = v.begin()+2;
cout << *It << endl;
return 0;
}
运行结果:
// 1 2 3 4 5
// 3
6、capacity——容器就像一个翻倍变大的盒子,capacity返回的是盒子当前的大小,size返回的是当前的盒子里面有多少东西,盒子装不下的时候capacity会自动翻倍
#include <iostream>
#include <vector>
using namespace std;
int main ()
{
vector<int> v(10);
cout << "Size of v = "
<< v.size() << endl;
cout << "Capacity of v = "
<< v.capacity() << endl;
v.resize(100);
cout << "After resizing:" << endl;
cout << "Size of v = "
<< v.size() << endl;
cout << "Capacity of v = "
<< v.capacity() << endl;
return 0;
}
运行结果:
// Size of v = 10
// Capacity of v = 10
// After resizing:
// Size of v = 100
// Capacity of v = 100
7、clear——容器就像一个盒子,clear便是拿走盒子里面的东西
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
template <class T>
class Print
{
public:
void operator () (T& t)
{
cout << t << " ";
}
};
//==============================
int main ()
{
vector<int> v(10);
Print<int> print;
fill(v.begin(),v.end(),5);
cout << "Vector v : ";
for_each(v.begin(),v.end(),print);
cout << endl;
cout << "Size of v = " << v.size() << endl;
cout << "v.clear" << endl;
v.clear();
cout << "Vector v : ";
for_each(v.begin(),v.end(),print);
cout << endl;
cout << "Size of v = " << v.size()
<< endl;
cout << "Vector v is ";
v.empty() ? cout << "" : cout << "not ";
cout << "empty" << endl;
return 0;
}
运行结果:
// Vector v : 5 5 5 5 5 5 5 5 5 5
// Size of v = 10
// v.clear
// Vector v :
// Size of v = 0
// Vector v is empty
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。



