STL基本知识vector

本文详细介绍了在C++编程中利用STL(标准模板库)进行数据排序与输出的方法,包括使用insert函数插入元素,sort函数进行升序与降序排列,并通过自定义比较函数实现特定排序需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值