c++ STL 代码练习


1, vertor

#include <iostream>                                                                                                                   
#include <vector>
#include <algorithm>
using namespace std;

void intVector_out(int i)
{
        cout<<i<<",";
}
int main()
{
        vector<int> intVector;
        intVector.push_back(1);
        intVector.push_back(3);
        intVector.push_back(4);
        intVector.push_back(4);
        intVector.push_back(4);
        intVector.push_back(4);
        intVector.push_back(5);
        intVector.push_back(6);

        vector<int>::iterator iT;
        for(iT = intVector.begin();iT != intVector.end();iT++)
        {
                if(*iT == 4)
                        intVector.erase(iT);
        }
        for(iT = intVector.begin();iT != intVector.end();iT++)
                cout<<*iT<<",";
        cout<<endl;
        return 0;
}

2, map

#include <map>                                                                                                                        
#include <string>
#include <iostream>

using namespace std;

int main()
{
        map<int,int>m1,m2,m3;
        map<int,int>::iterator m1_iter;

        m1.insert(pair<int,int>(1,10));
        m1.insert(std::map<int,int>::value_type(3,30));
        m1.insert(pair<int,int>(2,20));

        m2.insert(pair<int,int>(10,100));
        m2.insert(pair<int,int>(20,200));

        m3.insert(pair<int,int>(30,300));

        cout<<"The original map m1 is:";
        for(m1_iter=m1.begin();m1_iter!=m1.end();m1_iter++)
        cout<<" "<<m1_iter->second;
        cout<<"."<<endl;

        //this is the func of swap
        m1.swap(m2);
        cout<<"\nafter swap with m2,map m1 is:";
        for(m1_iter=m1.begin();m1_iter!=m1.end();m1_iter++)
        cout<<" "<<m1_iter->second;
        cout<<"."<<endl;

        cout<<"\nafter swap with m2,map m1 is:";
        for(m1_iter=m2.begin();m1_iter!=m2.end();m1_iter++)
        cout<<" "<<m1_iter->second;
        cout<<"."<<endl;

        swap(m1,m3);
        cout<<"\nafter swap with m3,map m1 is:";
        for(m1_iter=m1.begin();m1_iter!=m1.end();m1_iter++)
        cout<<" "<<m1_iter->second;
        cout<<"."<<endl;
        for(m1_iter=m3.begin();m1_iter!=m3.end();m1_iter++)
        cout<<" "<<m1_iter->second;
        cout<<"."<<endl;
 char pchar[] = "class";
        char pclass[]= "203";
        string dd1 = pchar;
        string dd2 = pclass;
        map<string,string>mstr;
        mstr.insert(pair<string,string>("name","zhangliang"));
        mstr.insert(pair<string,string>("school","shenyang"));
        mstr.insert(pair<string,string>("addr","liaoning"));
        mstr.insert(pair<string,string>("age","26"));
        //mstr.insert(pair<string,string>(dd1,dd2));
        mstr.insert(pair<string,string>(pchar,pclass));

        map<string,string>::iterator mstr_iter;
                cout<<"\n\033[031m    : "<<mstr["name"].substr(5,10)<<"\033[0m"<<endl;
        mstr_iter = mstr.find("class");
        if(mstr_iter != mstr.end())
                mstr_iter->second = "555";
        mstr_iter = mstr.begin();
        string ss;
        ss = mstr_iter->first;
        cout<<"\n### ss :"<<ss<<endl;
        if(mstr_iter != mstr.end())
        {
                cout<<"\n### find the element:"<<mstr_iter->second<<endl;
        }

        //for(mstr_iter=mstr.begin();mstr_iter!=mstr.end();mstr_iter++)
        for(;mstr_iter!=mstr.end();mstr_iter++)
        {
                if(mstr["name"].compare("zhangliang") == 0)
                        cout<<"\n\033[032m"<<mstr_iter->first<<" : "<<mstr_iter->second<<"\033[0m"<<endl;
                else
                        cout<<"\n\033[032m     :"<<mstr_iter->second<<"\033[0m"<<endl;
        }

}                                                  

3, set

#include <iostream>                                                                                                                   
#include <set>
using namespace std;

int main()
{
        set<char> charValue;
        set<char>::iterator iter;
        for(int i=0;i<10;i++)
        {
                charValue.insert(i+65);
        }
        cout<<"set elements:";
        int max_size;
        max_size = charValue.size();
        for(iter=charValue.begin();iter!=charValue.end();iter++)
        {
                cout<<*iter<<" ";
        }
        cout<<max_size<<endl;
        cout<<endl;

        iter = charValue.begin();
        charValue.erase(++iter);
        cout<<"set elements:";
        for(iter=charValue.begin();iter!=charValue.end();iter++)
        {
                cout<<*iter<<" ";
        }
        cout<<endl;
        cout<<endl;

        int count;
        count = charValue.count('C');
        cout<<"C counts:"<<count<<endl;

        iter = charValue.find('C');
        if(iter == charValue.end())
                cout<<"no found "<<endl;
        else
                cout<<"founded "<<*iter<<endl;
    
        iter = charValue.lower_bound('E');
        cout<<"current *iter:"<<*iter<<endl;
        charValue.clear();
        if(charValue.empty())
                cout<<"empty!"<<endl;
        else
                cout<<"not empty"<<endl;


}

4, deque#include <iostream>                                                                                                                   
#include <deque>
using namespace std;

int main()
{
        deque<char> charValue;
        deque<char> charValue1;
        deque<char>::iterator iter;
        deque<char>::iterator templter;
        for(int i=0;i<10;i++)
        {
                charValue.push_front(i+65);
                charValue.push_back(i+65);
                charValue1.push_front(i+65);
        }
        cout<<"charValue elements:";
        for(int i=0;i<charValue.size();i++)
        {
                cout<<charValue.at(i)<<" ";
        }
        cout<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
        charValue.assign(4,'A');
        cout<<"charValue elements:";
        for(int i=0;i<charValue.size();i++)
        {
                cout<<charValue[i]<<" ";
        }
        cout<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;
    
        iter = charValue.begin();
        charValue.insert(iter,'B');
        iter = charValue.begin();
        charValue.insert(iter,4,'C');
        cout<<" 3 charValue elements:";
        for(iter=charValue.begin();iter!=charValue.end();iter++)
        {
                cout<<*iter<<" ";
        }
        cout<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~"<<endl;


}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值