vector中基本操作和算法

最近遇到挺多用vector解题的题,整理一下

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <cmath>
#include <cstring>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <vector>
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const ll mod=1e9+7;
const ll N=11;
const double pai=3.14159265358979;
const ll maxn2=1e3+2;
#define mem(a,b) memset(a,b,sizeof(a))
ll read()
{
    ll x=0,w=1;
    char ch=0;
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')
        {
            w=-1;
        }
        ch=getchar();

    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return w*x;
}
int main()
{
    int b[5]={1,2,3,4,5};
    int f[6]={6,5,4,3,2,1};
    vector<int>a;
    cout << "a只含4个元素,且每个元素为2" << endl;
    a.assign(4, 2);
    cout << "遍历输出" << endl;
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "删除容器a中最后一个元素" << endl;
    a.pop_back();
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "容器a清空" << endl;
    a.clear();
    cout << "输出其中元素" << endl;
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "b是数组,向容器c中填充b数组中第0个到第2个数字" << endl;
    vector<int>c(b,b+3);
    for(vector<int>::iterator it=c.begin();it!=c.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "输出容器c中第一个和最后一个" << endl;
    cout << c.front() << ' ' << c.back() << endl;
    cout << "在容器最后面插入5" << endl;
    c.push_back(5);
    for(vector<int>::iterator it=c.begin();it!=c.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "在第二个元素后面插入元素(从第0个元素开始计数),被插入的数字就是第三个" << endl;
    c.insert(c.begin()+3, 9);
    for(vector<int>::iterator it=c.begin();it!=c.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "c中元素个数" << endl;
    cout << c.size() << endl;
    cout << "将c中元素调至九个,多则删,少则补,补的值为99" << endl;
    c.resize(9,99);
    for(vector<int>::iterator it=c.begin();it!=c.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "向a中差入数组f中的元素,并遍历输出a中元素" << endl;
    for(int i=0;i<=5;++i)
    {
        a.push_back(f[i]);
    }
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "第一个元素到第三个元素" << endl;
    a.erase(a.begin()+1,a.begin()+4);
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "a容器中的值与c容器的值交换,并输出a中元素" << endl;
    a.swap(c);
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "c中元素" << endl;
    for(vector<int>::iterator it=c.begin();it!=c.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "将a中值排序" << endl;
    sort(a.begin(), a.end());
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "将a中的值倒序,并输出a中元素" << endl;
    reverse(a.begin(),a.end());
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "用copy函数将c中的值复制到a中,a中原有值被覆盖,从a中第一个数开始,从0开始计数,只能覆盖,不能使a容器中元素增多,想在后面加元素,要用insert" << endl;
    copy(c.begin(),c.end(),a.begin()+1);
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "从a中第八个数后面(也就是最后一个)开始插入f数组中的元素" << endl;
    a.insert(a.begin()+9, f,f+6);
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
    cout << "从a中第三个数后面(也就是最后一个)开始插入f数组中的元素" << endl;
    a.insert(a.begin()+3, f,f+6);
    for(vector<int>::iterator it=a.begin();it!=a.end();++it)
    {
        cout << *it << ' ';
    }
    cout << endl;
}
输出结果:
a只含4个元素,且每个元素为2
遍历输出
2 2 2 2 
删除容器a中最后一个元素
2 2 2 
容器a清空
输出其中元素

b是数组,向容器c中填充b数组中第0个到第2个数字
1 2 3 
输出容器c中第一个和最后一个
1 3
在容器最后面插入5
1 2 3 5 
在第二个元素后面插入元素(从第0个元素开始计数),被插入的数字就是第三个
1 2 3 9 5 
c中元素个数
5
将c中元素调至九个,多则删,少则补,补的值为99
1 2 3 9 5 99 99 99 99 
向a中差入数组f中的元素,并遍历输出a中元素
6 5 4 3 2 1 
第一个元素到第三个元素
6 2 1 
a容器中的值与c容器的值交换,并输出a中元素
1 2 3 9 5 99 99 99 99 
c中元素
6 2 1 
将a中值排序
1 2 3 5 9 99 99 99 99 
将a中的值倒序,并输出a中元素
99 99 99 99 9 5 3 2 1 
用copy函数将c中的值复制到a中,a中原有值被覆盖,从a中第一个数开始,从0开始计数,只能覆盖,不能使a容器中元素增多,想在后面加元素,要用insert
99 6 2 1 9 5 3 2 1 
从a中第八个数后面(也就是最后一个)开始插入f数组中的元素
99 6 2 1 9 5 3 2 1 6 5 4 3 2 1 
从a中第三个数后面(也就是最后一个)开始插入f数组中的元素
99 6 2 6 5 4 3 2 1 1 9 5 3 2 1 6 5 4 3 2 1 
Program ended with exit code: 0
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值