#include <iostream> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) { string s; s = "cat dog cat"; //查找第一个字符'c',返回下标值 cout << s.find('c') << endl; //查找第一个子串"c",返回下标值 cout << s.find("c") << endl; //查找第一个子串"cat",返回下标值 cout << s.find("cat") << endl; //查找第一个子串"dog",返回下标值 cout << s.find("dog") << endl; //查找第一个子串"pig",查不到则返回4294967295 cout << s.find("pig") << endl; if(s.find("pig") == string::npos) cout << "没有找到" << endl; return 0; }
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) { string s; s = "cat dog cat"; //使用比较符号">","<","==" cout << (s == "cat dog cat") << endl; //s比"abc"字符串大,返回1 cout << s.compare("abc") << endl; //异常:s比"cat dog c"字符串大,返回2,发现返回值是剩下2个值不一致则返回2. cout << s.compare("cat dog c") << endl; //s比"dog"小,返回-1 cout << s.compare("dog") << endl; //s与"cat dog cat"相等,返回0 cout << s.compare("cat dog cat") << endl; return 0; }
#include <iostream> #include <string> #include <algorithm> using namespace std; int main(int argc, char* argv[]) { string s; s = "123456789"; reverse(s.begin(), s.end()); cout << s << endl; return 0; }
#include <iostream> #include <string> #include <algorithm> using namespace std; int main(int argc, char* argv[]) { vector<string> str; str.push_back("Jack"); str.push_back("Mike"); str.push_back("Tom"); for(int i = 0; i < str.size(); i++) cout << str[i] << endl; cout << str[0][0] << endl; cout << str[1][0] << endl; cout << str[2].size() << endl; return 0; }
#include <iostream> #include <string> using namespace std; int main(int argc, char* argv[]) { string s; s = "1234059"; int sum = 0; for(int i = 0; i < s.length(); i++) { if(s[i] == '0') sum += 0; else if(s[i] == '1') sum += 1; else if(s[i] == '2') sum += 2; else if(s[i] == '3') sum += 3; else if(s[i] == '4') sum += 4; else if(s[i] == '5') sum += 5; else if(s[i] == '6') sum += 6; else if(s[i] == '7') sum += 7; else if(s[i] == '8') sum += 8; else if(s[i] == '9') sum += 9; } cout << sum << endl; return 0; }
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) { string s; char ss[100]; //输入字符串到字符数组中 scanf("%s", &ss); //字符数组赋值字符串对象 s = ss; //用pringf输出字符串对象,要采用c_str()方法 printf(s.c_str()); cout << endl; //用printf输出字符数组 printf("%s", ss); cout << endl; return 0; }
#include <iostream> #include <stdio.h> #include <string> using namespace std; int main(int argc, char* argv[]) { string s1, s2, s3; char sa[100], sb[100], sc[100]; //将字符串分离成数字,分隔符为空格 sscanf("abc 123 pc", "%s %s %s", sa, sb, sc); s1 = sa; s2 = sb; s3 = sc; cout << s1 << " " << s2 << " " << s3 << endl; //将字符串分离成数字,分隔符为空格 //当用到数字的时候,跟scanf一样,它需要传指针地址 int a, b, c; sscanf("1 2 3", "%d %d %d", &a, &b, &c); cout << a << " " << b << " " << c << endl; //当字符串分离成数字,分隔符为","和"$" //当用到数字的时候,跟scanf一样,它要传指针地址 int x, y, z; sscanf("4,5$6", "%d,%d$%d", &x, &y, &z); cout << x << " " << y << " " << z << endl; return 0; }
#include <iostream> #include <sstream> #include <string> #include <stdio.h> using namespace std; //将数值转化为string的第二种方法:C++方法 string convertToString(double x) { ostringstream o; if(o << x) return o.str(); return "error"; } double convertFromString(const string &s) { istringstream i(s); double x; if(i >> x) return x; return 0.0; } int main(int argc, char* argv[]) { //将数值转化为string的第一种方法:C方法 char b[10]; string a; sprintf(b, "%d", 1975); a = b; cout << a << endl; //将数值转化为string的第二种方法:C++方法 string cc = convertToString(1976); cout << cc << endl; //将string转化为数值的第一种方法:C++方法 string dd = "2006"; int p = convertFromString(dd) + 2; cout << p << endl; return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
C++ Primer(第五版)学习笔记_5_标准模板库string(2)
原文地址:http://blog.csdn.net/keyyuanxin/article/details/46831185