vector

#include <vector>
#include <iostream>
#include <cstdio>
#include <cctype>
using namespace std;
typedef long long ll;
vector < ll > vec;
inline ll read () {
    ll x=0,f=1;
    char ch=getchar();
    while(!isdigit(ch)) {
        if(ch=='-') f=-1;
        ch=getchar();
    }
    while(isdigit(ch)) {
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
int n;
signed main() {
    n=read();
    for(register int i=1; i<=n; i++) vec.push_back(read());
    for(vector < ll >::iterator it=vec.begin(); it!=vec.end(); it++) cout<<*it<<' ';
    cout<<endl;
    for(register int i=0;i<=vec.size()-1;i++) cout<<vec[i]<<' ';//如果这样输出 下标必须为0开始
    return 0;
//vec.push_back(x) 把x元素插入到vector末尾
//vec.insert(pos,elem);在pos位置插入elem元素拷贝 返回新数据位置
//vec.insert(pos,n,elem);在pos位置插入n个elem元素
//vec.insert(pos,beg,end);在pos位置插入beg-end的元素
//vec.clear();清空vector
//vec.erase(beg,end); 清空beg-end的数字
//vec.erase(pos);清空pos位置
}

 例题

#include <bits/stdc++.h>
#define rep(i,j,n) for(register int i=j;i<=n;i++)
#define Rep(i,j,n) for(register int i=j;i>=n;i--)
#define low(x) x&(-x)
using namespace std ;
typedef long long LL ;
const int inf = INT_MAX >> 1 ;
inline LL In() { LL res(0) , f(1) ; register char c ;
#define gc c = getchar()
    while(isspace(gc)) ; c == '-' ? f = - 1 , gc : 0 ;
    while(res = (res << 1) + (res << 3) + (c & 15) , isdigit(gc)) ;
    return res * f ;
#undef gc
}

int m , n ;
vector < int > vec ;
inline void Ot() {
    m = In() ; n = In() ;
    int ans(0) ;
    rep(i,1,n) {
        int x = In() ;
        if(find(vec.begin() , vec.end() , x) == vec.end()) {
            vec.push_back(x) ;
            ans ++ ;
        }
        if(vec.size() > m) vec.erase(vec.begin()) ;
    }
    cout << ans << endl ;
}
signed main() {
//  freopen("testdata.txt","w",stdout) ;
    return Ot() , 0 ;
}
P1540

 

posted @ 2019-01-31 11:24  Isaunoya  阅读(181)  评论(0编辑  收藏  举报
TOP