c++ STL 程式碼練習
阿新 • • 發佈:2019-01-01
#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;
}
}
#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;
}
}