练习Vector-1

练习STL,练习的网址戳我

Vector

  • Declaration:定义
    vector<int>v; (creates an empty vector of integers)
  • Size:求大小
    int size=v.size();
  • Pushing an integer into a vector:存入数值
    v.push_back(x);(where x is an integer.The size increases by 1 after this.)
  • Popping the last element from the vector:删掉最后一个值
    v.pop_back(); (After this the size decreases by 1)
  • Sorting a vector:排序
    sort(v.begin(),v.end()); (Will sort all the elements in the vector)

这个函数比较像数组,但是它开的范围比数组大。

练习题的代码

#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
using namespace std;
int n;
vector<int>v;
int main()
{
    cin>>n;
    int t;
    for(int i=0;i<n;i++)
    {
        cin>>t;
        v.push_back(t);
    }
    sort(v.begin(),v.end());
    cout<<v[0];
    for(int i=1;i<n;i++)
        cout<<" "<<v[i];
    cout<<endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值