1.字符,字符串,字符数组的输入
(1).char单个字符符输入
char s; scanf("%c",&s); //scanf函数输入
cin>>s; //cin流输入
cin.get(s); //cin.get()函数输入
s=getchar(); //不能写成getchar(s);
(2).char数组输入
char s[10]; scanf("%s",s); //字符数组名可以表示地址,/* 注:scanf函数不能输入有空格的字符串*/
cin>>s; //遇到空格,回车,table会结束,不用输入个数
///
cin.getline(m,5); //必须输入个数,同时还有第三个参数,结束字符(没写的话一般是' '或' ')
cin.getline(m,5,a) //以输入a为结束字符,
cin.get(m,5); //同上,但没有结束字符
// 上面两种方法 均可接收空格
//输入:asdnsasd
//输出:asdn
//接收的5个元素,最后一个为' ';
///
gets(); //任意数目的一行可带空格的字符串;
(3).string类型
string s; cin>>s; //无法输入空格 getline(cin,s); //任意数目
2.string用法总结(用看待stl容器的方式,看待string类型)
1.元素访问
string s="233333"; cout<<s[2]<<endl; //下表访问
for(string::iterator it=s.begin() ; it != s.end() ; it++){
cout<<*it<<endl;
}
2.运算
string类型元素可运用 +,=,+=
string s1="abc",s2="def"; s1=s1+s2; s1+=s2;
string类型元素可直接使用 ==,!=,>,<,>=,<=比较大小,依照字典序
string s1="adc",s2="adc",s3="dlc"; if( s1 == s2 ) cout<<"233"<<endl; if( s1 > s3 ) cout<<"2333"<<endl; if( s2 <= s3 ) cout<<"2333"<<endl;
3.插入——insert()
insert(position , string)
4.用于作为find函数匹配失配时的返回值验证——string::npos
if( a.find(s1) == string::npos ) cout<<"No"<<endl;
注:其余stl容器用 if( a.find(n) == a.end() )//find函数如果没有找到返回容器的最后一个元素的迭代器
5.查找
(1).find(s1)
find(s1,pos)
(2).