STL_vector

#include <vector>
#include <iostream>

using namespace std;

/*自己设计比较函数的话*/
bool Comp(const int &a, const int &b)
{
    if(a != b) reutrn a>b;
    else return a>b;
}

int main()
{
    vector<int> v;/*定义容器*/
    vector<int>::iterator it;/*迭代器*/    

    /*尾插入*/
    v.push_back(2);
    v.push_back(8);
    v.push_back(9);
    /*随机插入*/
    v.insert(v.begin(),10);    /* v.begin() */
    v.insert(v.begin() + 1,22);/* v.begin() + 1 */
    v.insert(v.end(),5);       /* v.end() */
    v.insert(v.end() - 1,6);   /* v.end() - 1 */
    /*打印,利用迭代器 注意 *it */
    for(it = v.begin();it != v.end();it++)
    {
        cout<<*it<<" ";
    }cout<<endl;
    /*擦除一个*/
    v.erase(v.begin() + 2);      /* 2为元素下标 */for(it = v.begin();it != v.end();it++){cout<<*it<<" ";}cout<<endl;
    /*擦除一段*/
    v.erase(v.begin() + 2, v.begin() + 4); /* 下标2 - 下标4 */for(it = v.begin();it != v.end();it++){cout<<*it<<" ";}cout<<endl;
    /*清空*/
    v.clear();         for(it = v.begin();it != v.end();it++){cout<<*it<<" ";}cout<<endl;
    /*打印大小*/
    cout<< v.size() <<endl;
    /*判断是否为空*/
    cout<< v.empty() <<endl;
    


    /*反转容器 头文件<algorithm> */
    reverse(v.begin(), v.end() );
    /*升序sort 头文件<algorithm>*/
    sort( v.begin(), v.end() );//默认升序
    return 0;
}
原文地址:https://www.cnblogs.com/wwjyt/p/3182817.html