顺序容器5

#include<iostream>
#include<string>
#include<deque>
#include<list>
#include<vector>

using namespace std;

int main()
{
    vector<int>  ivec;

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

    cout << ivec.front() << endl;//返回第一个数据
    cout << ivec.back() << endl;//返回最后一个数据

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

    cout << *ivec.begin() << endl;
    //end指向最后一个的下一个
    cout << *--ivec.end() << endl;
    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.at(1) << endl;
    cout << ivec.at(2) << endl;

    //cout << ivec.at(3000) << endl;//会抛出异常
    try{//用try捕获异常
        cout << ivec.at(300) << endl;
    }
    catch (out_of_range)
    {
        cout << "error" << endl;
    }

    //如果容器是空的
    list<int> ilist;//空的使用back和front会抛出异常,所以在使用前检查是不是空的ilist.emty()
    //list链表没有下标,所以不能使用

    system("pause");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_42655231/article/details/82714032
今日推荐