1.vector 作为容量
初始化:
vector<int> myvector(10,1);
vector作为函数参数进行调用
#include<iostream>
//vector 是C++中最常用的容器类
#include<vector>
using namespace std;
void main(){
//注意:在使用函数之前,一定要申明!
void init_vector(vector <int> &vecTest);
void show_vector(vector <int> &vecTest);
void insert_array_to_vector(vector<int> &vecTest,int a []);
//申明一个vector
vector<int> vec;
//初始化vector
init_vector(vec);
//显示vector
show_vector(vec);
//向vector中插入一个数组
int a[] = {12,29,83};
insert_array_to_vector(vec,a);
//vec.insert(vec.begin()+3,100);
show_vector(vec);
}
/*初始化容器*/
void init_vector(vector <int> &vecTest){
for(int i=0;i<10;i++)
//push_back()方法在vector的末尾附加元素
vecTest.push_back(i);
/*
vector<int>::iterator it = vecTest.begin();
//it是地址
while(it<vecTest.end()){
cout<<*it<<endl;
it++;
}*/
}
/* 显示容器中的所有数据*/
void show_vector(vector <int> &vecTest){
vector<int>::iterator it = vecTest.begin();
//it 是一个地址
while(it<vecTest.end()){
cout<<*it<<endl;
it++;
}
}
/*向容器中插入一个数组*/
void insert_array_to_vector(vector<int> &vecTest,int a []){
int length = sizeof(a)/sizeof(a[0]);
vector<int>::iterator insertPosition = vecTest.begin()+length-1;
vecTest.insert(insertPosition,a,a+3);
}
2.iterator 迭代器
const_iterator :
for (vector<string>::const_iterator iter = text.begin(); iter != text.end(); ++ iter){
cout << *iter << endl; //ok: print each element in text
*iter = " "; // error: *iter is const
}
vector<int>::const_iterator 和 const vector<int>::iterator的区别:
for(vector<int>::const_iterator citer=ivec.begin();citer!=ivec.end();citer++)
{
cout<<*citer;
//*citer=3; error
}
const vector<int>::iterator newiter=ivec.begin();
*newiter=11; //可以修改指向容器的元素
//newiter++; //迭代器本身不能被修改
iterator的遍历典型应用:
#include <vector>
#include <iostream>
using namespace std;
int main()
{
vector<int> ivec;
ivec.push_back(1);
ivec.push_back(2);
ivec.push_back(3);
ivec.push_back(4);
for(vector<int>::iterator iter = ivec.begin();1. iter != ivec.end(); ++iter)
cout << *iter << endl;
}
通过对iterator的解引用访问容器的所有元素:
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<int> the_vector;
vector<int>::iterator the_iterator;
vector<int>:: iterator thee_iterator;
for( int i=0; i < 10; i++ )
the_vector.push_back(i);
int total = 0;
int total1 = 0;
the_iterator = the_vector.begin();
while( the_iterator != the_vector.end() ) {
total += *the_iterator;
total1 += *thee_iterator;
the_iterator++;
}
cout << "Total=" << total << endl;
}
3.pair的使用
#include<iostream>
#include<vector>
using namespace std;
int main()
{
vector<pair<int,int> >VP;
VP.push_back(make_pair<int,int>(10,50));
VP.push_back(make_pair<int,int>(10,20));
VP.push_back(make_pair<int,int>(20,40));
VP.push_back(make_pair<int,int>(40,80));
VP.push_back(make_pair<int,int>(30,90));
vector<pair<int,int> > ::iterator iter; //访问vector
iter=VP.begin();
iter++;
iter++;
VP.erase(iter);//删除元素(20,40)
for(iter=VP.begin();iter!=VP.end();iter++)
{
cout<<iter->first<<"/t"<<iter->second<<endl;
}
return 0;
}
4.map 容器 可以存储值和出现的次数
5.sort函数的用法
函数模版:
template <class RandomAccessIterator>
void sort ( RandomAccessIterator first, RandomAccessIterator last );
template <class RandomAccessIterator, class Compare>
void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
6.reserve()与resize()
reserve():重新指定容器的容量
resize():重新指定容器中有效元素的数量