vector
map
#include<cstdio>
#include<string>
#include<cstring>
#include<iostream>
#include<map>
using namespace std;
int main()
{
map<string,int>mp;
mp["Lee"] =1;
mp["Wang"]=2;
mp["Kang"]=3;
cout<<mp["Lee"]<<endl;
for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++)
{
//first-key second-value
//map will ordered by first-key
cout<<it->first<<" "<<it->second<<endl;
}
cout<<mp.size()<<endl;
map<string,int>::iterator it=mp.find("Lee");
cout<<"Delete:"<<it->first<<"-"<<it->second<<endl;
mp.erase(it);//delete "Lee"
//C++11 can use auto to replace map<string,int>::iterator
for(auto it=mp.begin();it!=mp.end();it++)
{
//first-key second-value
//map will ordered by first-key
cout<<it->first<<" "<<it->second<<endl;
}
mp.clear();
cout<<mp.size();
}
set
iterator /ɪtə'reɪtə/ 迭代器
#include<cstdio>
#include<iostream>
#include<set>
using namespace std;
int main()
{
set<int>s;
s.insert(1);
s.insert(2);
s.insert(3);
s.insert(4);
for(set<int>::iterator it=s.begin();it!=s.end();it++)
{
cout<< *it <<" ";
}
cout<<endl;
//如果没找到 *it 为set中最后一个元素
//如果找到则为 要find的元素
set<int>::iterator it=s.find(2);
cout<< *it <<endl;
s.erase(2);
cout<<"s.size()=="<<s.size();
// s.clear();
// s.erase(s.begin(),s.end());
//C++11 auto
for(auto it=s.begin();it!=s.end();it++)
{
cout<< (*it) <<" ";
}
cout<<endl;
}
string
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<sstream>
using namespace std;
int main()
{
//string 返回子串位置
string str="It is a 78 full tree";
if(str.find("full")!=string::npos)cout<<str.find("full")<<endl;
if(str.find("full")!=-1)cout<<str.find("full")<<endl;
//string 读取字符串中的 int
// #include<sstream>
istringstream istr(str);
string temp;
int integer;
istr>>temp>>temp>>temp>>integer>>temp>>temp;
cout<<integer<<endl;
//带空格string的输入与输出
string blank;
getline(cin,blank);
cout<<blank<<endl;
//reverse #include<algorithm>
reverse(blank.begin(),blank.end());
cout<<blank<<endl;
//string 初始化 输出 "hello world"
string strInit = "hello world";
cout<<strInit<<endl;
//string 拼接 输出 "hello-world-"
string stra="hello-";
string strb="world-";
string strJoint=stra+strb;
cout<<strJoint<<endl;
//string 比大小-字典序
if(stra>=strb)cout<<"stra>=strb"<<endl;
if(stra!=strb)cout<<"stra!=strb"<<endl;
//string 长度
string strLength="string length";
cout<<strLength.length()<<endl;
//string 插入字符串 输出"abcdefghijklmn"
string beInserted="abchijklmn";
string insert="defg";
beInserted.insert(3,insert);
cout<<beInserted<<endl;
//string 获取子串 substr(pos,len)
string whole="i love china";
string sub=whole.substr(7,5);
cout<<sub<<endl;
//string 转换为 int string to int stoi()
string a="10101000";
int b=stoi(a);
cout<<b<<endl;
//string int转换为string to_string()
int aa=98989;
string bb=to_string(aa);
cout<<bb<<endl;
}
字符数组
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#define MAX 1000
using namespace std;
int main()
{
//sscanf and sprintf
char a[50],b[50];double temp;
scanf("%s",a);
sscanf(a,"%lf",&temp);
sprintf(b,"%0.2f",temp);
printf("%s",b);
// a=aaa b=0.00
// a=2.3.4 b=2.30
//a=7.123 b=7.12
//blank & enter will stop it
char str[MAX];
scanf("%s",str);
printf("%s",str);
//absorb enter
getchar();cout<<endl;
//input a line's string
char str1[MAX];
gets(str1);
puts(str1);
cout<<strlen(str1)<<endl;
}