STL基本知识vector

来源:互联网 发布:天龙八部 知乎 编辑:程序博客网 时间:2024/04/26 11:26

#include
#include
#include

using namespace std;

void output(int &s)   //输出函数
{
    cout<<s<<endl;

}

bool mycomp(const int &s1,const int &s2)
{
    return s1>s2;
}

int main(int argc,char * argv[])
{
    vector myvt;

    myvt.insert(myvt.begin(),2);
    myvt.insert(myvt.begin()+1,4);
    myvt.insert(myvt.end(),1);
    cout<<"原顺序 :"<    for_each(myvt.begin(),myvt.end(),output);

    cout<<"升序:"<    sort(myvt.begin(),myvt.end());   //默认升序排列
    for_each(myvt.begin(),myvt.end(),output);

    cout<<"降序:"<    sort(myvt.begin(),myvt.end(),mycomp);
    for_each(myvt.begin(),myvt.end(),output);
}