输入输出;
1.
cin.getline();属于iostream
char m[20]; cin.getline(m,5);//输入名称和长度; cout<<m<<endl;
2.getline()属于string;
string s; getline(cin,s); cout<<str<<endl;
3.读掉换行
str="\n"; getline(cin,str);
string
1.c_str()
1. const char* c; string s="1234"; c = s.c_str(); cout<<c; 2. string s = "Hello World!"; printf("%s", s.c_str()); //输出 "Hello World!"
2.c语言里面 ,所以要转换为数组,atoi(),atof()为double
跳过前面的空格字符串,直到遇上数字或正负号才开始做转换,而再遇到非数字或字符串‘\0’时才结束转换,并将结果返回 const char* c; string s; cin>>s; c=s.c_str(); int n=atoi(c); cout<<n<<endl; 输入"-100"输出-100;
string
1.find()
string s="ifisfs"; if(s.find("is")!=string::npos) cout<<s.find("is");//输出2
2.erase()
string str = "welcome to my blog"; //s.erase(pos,n) 把字符串s从pos开始的n个字符删除 str.erase(11,3); // str = "welcome to blog" str.erase(2) 删除2以后的字符; 左闭右开
3.
string str = "The apple thinks apple is delicious"; //s.substr(pos,n) 得到字符串s位置为pos后面的n个字符组成的串 string s1 = str.substr(4,5); // s1 = "apple" //s.substr(pos) 得到字符串s从pos到结尾的串 string s2 = str.substr(17); // s2 = "apple is delicious" 左闭右开