美文网首页
STL--vector、deque、list、set、map、s

STL--vector、deque、list、set、map、s

作者: lickang | 来源:发表于2018-12-22 11:31 被阅读0次

    vector(向量容器)

    #include<stdio.h>
    #include<vector>
    #include<algorithm>
    using namespace std;
    bool cmp(int &x,int &y) {
        return x>y;
    }
    
    int main() {
        int a[10]={2,4,1,6,8,12,67,45,89,456};
        vector <int> vec(a,a+10);//用a的10个元素初始化vec
        printf("size:%d\n",vec.size());
        printf("正向输出vec:");
        vector<int>::iterator it,it1;
        for(it=vec.begin();it!=vec.end();it++) {
            printf("%d\t",*it);
        }
        printf("\n");
        int x=6;
        it1 = find(vec.begin(),vec.end(),x);
        if(it1!=vec.end()) {
            printf("find it = %d\n",*it1);
        }else {
            printf("Not find it.\n");
        }
        printf("递减排序vec:\n");
        sort(vec.begin(),vec.end(),cmp);
        vector<int>::reverse_iterator rit;
        printf("反向输出vec:");
        for(rit = vec.rbegin();rit!=vec.rend();++rit) {
            printf("%d\t",*rit);
        }
        printf("\n");
        return 0;
    }
    
    程序运行结果

    deque(双端队列容器)

    #include<deque>
    using namespace std;
    void Display(deque<int> &dq) {
        deque<int>::iterator iter;
        for(iter=dq.begin();iter!=dq.end();iter++) {
            printf("%d\t",*iter);
        }
        printf("\n");
    }
    int main() {
        deque<int> dq; //建立双端队列
        dq.push_front(1); //队头插入
        dq.push_back(2);//队尾插入
        dq.push_front(3);//队头插入
        dq.push_back(4);//队尾插入
        Display(dq);
    
        dq.pop_front();//删除队头元素
        dq.pop_back();//删除队尾元素
        Display(dq);
        return 0;
    }
    
    程序运行结果

    list(链表容器)

    #include<iostream>
    #include<list>
    using namespace std;
    void Display(list<int> &myList) {
        list<int>::iterator it;
        for(it=myList.begin();it!=myList.end();it++) {
            cout<<*it<<" ";
        }
        cout<<endl;
    }
    int main() {
        int a[]={2,4,1,6,112,56,78,234,678,7897,3453,345,123,123,123,123};
        int n = sizeof(a)/sizeof(a[0]);//数组的长
        list<int> myList(a,a+n);
        myList.remove(6);//删除链表中所有指定值的元素
        cout<<"remove:";
        Display(myList);
        myList.reverse();//链表反转
        cout<<"reverse:";
        Display(myList);
        myList.unique();//删除链表中所有重复的元素
        cout<<"unique:";
        Display(myList);
        myList.sort();//递增排序
        cout<<"sort:";
        Display(myList);
        myList.sort(greater<int>());//递减排序
        cout<<"sort(greater<int>()):";
        Display(myList);
        return 0;
    }
    
    程序运行结果

    set(集合容器)

    #include<stdio.h>
    #include<set>
    #include<algorithm>
    using namespace std;
    void Display(set<int> &myset) {
        set<int>::iterator it;
        for(it = myset.begin();it!=myset.end();it++) {
            printf("%d ",*it);
        }
        printf("\n");
    }
    int main() {
        set<int> set1,set2,set3;
        int a[]={2,1,5,6,9};
        int n = sizeof(a)/sizeof(a[0]);
        set1.insert(a,a+n);//插入
        int b[]={4,7,9,3,1};
        int m = sizeof(b)/sizeof(b[0]);
        set2.insert(b,b+m);//插入
        set<int>::iterator it3;
        printf("set1:");
        Display(set1);
        printf("set2:");
        Display(set2);
        insert_iterator< set<int> > insert_it(set3,set3.begin());//定义插入迭代器insert_it
        set_union(set1.begin(),set1.end(),set2.begin(),set2.end(),insert_it);//并集
        printf("union:");
        Display(set3);
        set3.clear();
        set_intersection(set1.begin(),set1.end(),set2.begin(),set2.end(),insert_it);//交集
        printf("intersection:");
        Display(set3);
        set3.clear();
        set_difference(set1.begin(),set1.end(),set2.begin(),set2.end(),insert_it);//差集
        printf("different:");
        Display(set3);
        return 0;
    }
    
    程序运行结果

    map(映射容器)

    #include<stdio.h>
    #include<map>
    #include<algorithm>
    #pragma warning(disable:C4786)
    using namespace std;
    void Display(map<char,int> &mymap){
        map<char,int>::iterator it;
        for(it = mymap.begin();it!=mymap.end();it++) {
            printf("[%c,%d] ",it->first,it->second);
        }
        printf("\n");
    }
    int main() {
        map<char,int> mymap;
        mymap.insert(pair<char,int>('a',1));//插入方式1
        mymap.insert(map<char,int>::value_type('b',2));//插入方式2
        mymap['c']=3;//插入方式3
        printf("map:");
        Display(mymap);
        return 0;
    }
    
    程序运行结果

    相关文章

      网友评论

          本文标题:STL--vector、deque、list、set、map、s

          本文链接:https://www.haomeiwen.com/subject/cxlikqtx.html