#include<string> using std::string;
string s1;//默认构造函数s1为空串 string s2(s1);//将s2初始化为s1的一个副本 string s3("value");//将s3初始化为一个字符串字面值副本 string s4(n,‘c‘);//将s4初始化为字符‘c‘的n个副本
string s; cin>>s; cout<<s<<endl;
int main() { string word; while(cin>>word) { cout<<word<<endl; } return; }
int main() { string line; // read line at time until end-of-file while(getline(cin, line)) cout << line << endl; return0; }
s.empty();//s为空返回true s.size();//返回字符个数 s[n];//不必多言
#include<cctype> isalnum(c)//字母或数字 true isalpha(c)//字母 iscntrl(c)//控制字符 //太多了,不列了.....
string str; cin >> str; str.find("ab");//返回字符串ab在str的位置 str.find("ab",2);//在str[2]~str[n-1]范围内查找并返回字符串ab在str的位置 str.rfind("ab",2);//在str[0]~str[2]范围内查找并返回字符串ab在str的位置 //first系列函数 str.find_first_of("apple");//返回apple中任何一个字符首次在str的位置 str.find_first_of("apple",2);//返回apple中任何一个字符首次在str[2]~str[n-1]范围中出现的位置 str.find_first_not_of("apple");//返回除apple以外的任何一个字符在str中首次出现的位置。 str.find_first_not_of("apple",2);//返回除apple中任何一个字符首次在str[2]~str[n-1]范围中出现的位置 //last系列函数 str.find_last_of("apple");//返回 apple 中任何一个字符最后一次在 str 中出现的位置 str.find_last_of("apple",2);//返回 apple 中任何一个字符最后一次在 str[0]~str[2] 范围中出现的位置 str.find_last_not_of("apple");//返回除 apple 以外的任何一个字符在 str 中最后一次出现的位置 str.find_last_not_of("apple",2);//返回除 apple 以外的任何一个字符在 str[0]~str[2] 范围中最后一次出现的位置 //以上函数如果没有找到,均返回string::npos cout << string::npos;
str.substr(3);//返回[3]及以后的子串 str.substr(2,4);//返回str[2]~str[2+4-1]子串(即从[2]开始4个字符组成的字符串)
str.replace(2,4,"sz");//返回将[2]~[2+4-1]的内容替换为"sz"后的新字符串 str.replace(2,4,"abcd",3);//返回把[2]~[2+4-1]的内容替换为"abcd"的前3个字符后的新字符串
str.insert(2,"sz");//从 [2] 位置开始添加字符串 "sz",并返回形成的新字符串 str.insert(2,"abcd",3);//从 [2] 位置开始添加字符串 "abcd" 的前 3 个字符,并返回形成的新字符串 str.insert(2,"abcd",1,3);//从 [2] 位置开始添加字符串 "abcd" 的前 [2]~[2+(3-1)] 个字符,并返回形成的新字符串
str.push_back(‘a‘);//在 str 末尾添加字符‘a‘ str.append("abc");//在 str 末尾添加字符串"abc"
str.erase(3);//删除 [3] 及以后的字符,并返回新字符串 str.erase(3,5);//删除从 [3] 开始的 5 个字符,并返回新字符串
str1.swap(str2);//把 str1 与 str2 交换
string str= to_string(num);
原文:http://www.cnblogs.com/pentium-neverstop/p/5323013.html