1. 程式人生 > >順序容器操作2

順序容器操作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<iostream>
#include<vector>
#include<list>
#include<deque>

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的元素