stl使用

本文展示了C++中iostream、cstring等库的使用,以及vector、stack、queue、set和unordered_map等STL容器的基本操作,包括字符串处理、数据存储与遍历、集合操作等。
摘要由CSDN通过智能技术生成
#include <iostream>
#include<cstring>
#include<vector>
#include<stack>
#include<queue>
#include<set>
#include<unordered_map>
#include<unordered_set>
#include<map>
using namespace std;

class Person{
public:
    int age;
    string name;
    Person(){}
    Person(int age,string name):age(age),name(name){}
    void set_tt(int age,string name){
        this->age=age;
        this->name=name;
    }
};
int main()
{
//    //stl使用
//
//    //string
//
//    string str;
//    //这种可以读入空格
//    getline(cin,str);
//    str.append("fanbo");
//    str+="zeng";
//    cout<<str<<endl;
//
//    //查找 注意是str.find 不是 find
//    int pos = str.find('n');
//    cout<<pos<<endl;
//    //最后一次出现
//    int rpos=str.rfind('n');
//    cout<<rpos<<endl;
//
//    //替换从1开始的3个字符串为
//    str.replace(1,3,"cao");
//    cout<<str<<endl;
//
//    //字符串比较
//    string ch="aaaa";
//    string sh="aaab";
    cout<<ch.compare(sh)<<endl;
//    cout<<ch.size()<<endl;
//    //将string类型转换为char
//    //ch.c_str()
//
//    //插入删除
//    ch.insert(1,"bbbb");
//    cout<<ch<<endl;


    //vector
//    vector<int> v1;
//    for(int i=1;i<=10;i++)
//        v1.push_back(i);
//    cout<<v1.empty()<<endl;
//    cout<<v1.size()<<endl;
//    //遍历
//    for(auto p:v1)
//        cout<<p<<" ";
//        cout<<endl;
//        v1.pop_back();
//    for(vector<int>::iterator it = v1.begin();it!=v1.end();it++)
//        cout<<*it<<" ";

//    vector<int> v2;
//    for(int i=1;i<=10;i++)
//        v2.push_back(i*10);
//    v1.swap(v2);
//    for(auto p:v1)
//        cout<<p<<" ";

        //栈
//        stack<int>s;
//        s.push(10);
//        s.push(20);
//        s.push(30);
//
//        //size
//        cout<<s.size()<<endl;
//        while(!s.empty()){
//            cout<<s.top()<<endl;
//            //出栈
//            s.pop();
//        }

//        //队列
//
//        queue<Person> q;
//
//        int n;
//        cin>>n;
//        Person * arr = new Person[n];
//
//        for(int i=0;i<n;i++){
//            int age;
//            string name;
//            cin>>age>>name;
//            arr[i].set_tt(age,name);
//        }
//
//        for(int i=0;i<n;i++){
//            q.push(arr[i]);
//        }
//
//        while(!q.empty()){
//
//            cout<<q.front().age<<" "<<q.front().name<<endl;
//            q.pop();
//        }
//
//        set<int>s1;
//        s1.insert(1);
//        s1.insert(100);
//        s1.insert(12);
//        s1.insert(12);
//        s1.erase(1);
//        for(set<int>::iterator it=s1.begin();it!=s1.end();it++)
//            cout<<*it<<" ";
//
//        if(s1.find(1)==s1.end()){
//            cout<<"shibai"<<endl;
//        }
//        cout<<s1.count(12)<<endl;
        //

        //只是无序 但同样只能唯一
//        unordered_set<int>s2;
//        s2.insert(10);
//        s2.insert(1);
//        s2.insert(11111);
//         s2.insert(981);
//        for(auto s : s2)
//            cout<<s<<endl;

        map<string,int>mp;
        mp["fanb0"]=1;
        mp["zeng"]=2;
        mp["z"]=2;
//        for(auto i:mp){
//            cout<<i.first<<" "<<i.second<<endl;
//        }
        mp.erase("fanb0");
        for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++){
            cout<<(*it).first<<endl;
        }


        unordered_map<string,int>mpp;
        mpp["fanb0"]=1;
        mpp["zeng"]=2;
        mpp["z"]=2;
//        for(auto i:mp){
//            cout<<i.first<<" "<<i.second<<endl;
//        }
        mp.erase("fanb0");
        for(unordered_map<string,int>::iterator it=mpp.begin();it!=mpp.end();it++){
            cout<<(*it).first<<endl;
        }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值