顺序容器操作2

容器插入

list<string>::iterator iter = slist.begin();
++iter;
++iter;
iter = slist.insert(iter, "----");//插入1
cout << *iter << endl;

for(list<string>::iterator i = slist.begin();
    i != slist.end(); ++i)
    cout  << *i << " ";
cout << endl;

string sarray[4] = {"queke","hanliang","lexm", "kitty"};
slist.insert(slist.end(), sarray+1, sarray+3); //插入2
for(list<string>::iterator i = slist.begin();
    i != slist.end(); ++i)
    cout  << *i << " ";
cout << endl;

slist.insert(slist.begin(), 4, "hello"); //插入3
for(list<string>::iterator i = slist.begin();
    i != slist.end(); ++i)
    cout  << *i << " ";
cout << endl;

1- slist.insert(iter, key)在迭代器iter位置插入元素key,返回插入元素key的位置
2-slist.insert(iter, iter1, iter2)在迭代器iter所指示位置,插入迭代器范围[iter1,iter2)之间的元素副本,范围void
3-slist.insert(iter, n, key)在迭代器iter位置处插入n个值为key的元素,范围void

注意:添加元素会使原来迭代器失效,所以需要更新的容器的迭代器也要更新,如下:

vector<string>::iterator ibeg = a.begin();
vector<string>::iterator iend = a.end();
while(ibeg != iend){ //错误,iend失效
    a.insert(ibeg, "hello");
    ++ibeg;
}

vector<string>::iterator ibeg = a.begin();
while(ibeg != a.end()){ //正确每次重新取容器最后边界
    ibeg = a.insert(++ibeg, "hello");
    ++ibeg;//略过当前插入的元素,取下一个
}

容器大小操作

#include
#include
#include
#include

using namespace std;

int main()
{
    list<int> ilist;
    ilist.push_back(10);
    ilist.push_back(20);
    ilist.push_back(30);

    cout << "number of list:" << ilist.size() << endl;

    list<int>::size_type sum = ilist.size();
    cout << "number of list: " << ilist.size() << endl;

    cout << "max size of list: " <<  ilist.max_size() << endl;

    if(ilist.empty())
        cout << "list is empty ! " << endl;
    else
        cout << "list is not empty" << endl;

    ilist.resize(10);//调整容器大小有10个数,多增加的7个默认为0;
    for(list<int>::iterator iter = ilist.begin();
        iter != ilist.end(); ++iter)
        cout << *iter << " ";
    cout << endl;

    ilist.resize(20, -1); //调整容器大小变为20个,默认多增加的都为-1;
    for(list<int>::iterator iter = ilist.begin();
        iter != ilist.end(); ++iter)
        cout << *iter << " ";
    cout << endl;

    ilist.resize(2);//容器缩小了
    for(list<int>::iterator iter = ilist.begin();
        iter != ilist.end(); ++iter)
        cout << *iter << " ";
    cout << endl;

    ilist.resize(0);
    if(ilist.empty())
        cout << "list is empty ! " << endl;
    else
        cout << "list is not empty" << endl;

    vector<int> ivec(10, 42); //10个42;
    ivec.resize(15);
    ivec.resize(25, -1);
    ivec.resize(5);

    return 0;
}

a.size() 容器的大小,返回size_type
a.max_size(); 容器最大能容纳的数量,返回size_type
a.empty(); 判断容器为空
a.resize(n);重置容器大小
a.resize(n, default);重置容器大小,新添加的元素值都为default
注意:resize可能使迭代器失效,适用vector,list, deque

容器访问

vector<int> ivec;
ivec.push_back(10);
ivec.push_back(20);
ivec.push_back(30);

cout << ivec.front() << endl;//第一个元素,10
cout << ivec.back() << endl;//最后一个元素,30

vector<int>::reference a = ivec.front();//返回引用
vector<int>::reference b = ivec.back();//返回引用
cout << a << endl;
cout << b << endl;

cout << *ivec.begin() << endl;//迭代器访问第一个元素,10
cout << *(--ivec.end()) << endl;//迭代器访问最后元素,30
vector<int>::reference c = *ivec.begin();//迭代器也可以返回引用
vector<int>::reference d = *--ivec.end();
cout << c << endl;
cout << d << endl;

cout << ivec[0] << endl;//下标访问
cout << ivec[1] << endl;
cout << ivec[2] << endl;
cout << ivec[300] << endl; //越界,严重错误,只能终止程序

cout << ivec.at(0) << endl;//at(n)下标访问
cout << ivec.at(1) << endl;
cout << ivec.at(2) << endl;

try {
    cout << ivec.at(300)<< endl; //越界,抛出异常,可以用try捕获
}catch(out_of_range){
    cout << "出错,下标越界!" << endl;
}

list<int> ilist;
if(!ilist.empty())
{
    cout << ilist.front() << endl;
    cout << ilist.back() << endl;
}

//cout << ilist[0] << endl; 链表没有下标,不能用下标访问
//cout << ilist.at(0) << endl;

注意:vector , deque有下标访问,list没有

容器删除
c.erase(iter);删除迭代器iter指向的元素,返回被删除元素的后面一个元素迭代器
c.erase(iter1, iter2);删除迭代器范围内的元素,返回删除的后面一个元素迭代器
c.clear();清除所有容器,返回void
c.pop_back();删除最后一个,返回void
c.pop_front();删除第一个,返回void,适用于deque和list
容器赋值和交换
c1 = c2 容器赋值,类型必须相同

vector<int> ivec1;
vector<int> ivec2;

ivec1.push_back(10);
ivec1.push_back(20);
ivec1.push_back(30);

ivec2 = ivec1;
for(vector<int>::iterator iter = ivec2.begin(); 
    iter != ivec2.end(); ++iter)
    cout << *iter <<  " "; //10,20,30

c1.swap(c2) c1和c2交换元素,元素类型相同

vector<int> ivec1;//10,20,30
vector<int> ivec2;//1,2,3,4

ivec1.push_back(10);
ivec1.push_back(20);
ivec1.push_back(30);

ivec2.push_back(1);
ivec2.push_back(2);
ivec2.push_back(3);
ivec2.push_back(4);

ivec2.swap(ivec1);//ivec2-10,20,30;ivec1-1,2,3,4

c.assign(iter1, iter2)将迭代器范围内元素复制到c中,元素类型兼容,容器类型可以不同

ivec2.assign(++ivec1.begin(), ivec1.end());
for(vector<int>::iterator iter = ivec2.begin();
    iter != ivec2.end(); ++iter)
    cout << *iter << " ";//20,30

c.assign(n,key) 将容器重新设置存储n个值为key的元素

你可能感兴趣的:(c++)