1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| #include <iostream> #include <vector> #include <list> #include <deque> #include <string> using std::vector; using std::list; using std::deque; using std::string; using std::cin; using std::cout; using std::endl;
int main() { string s1; string s2(5, 'a'); string s3(s2); string s4(s3.begin(), s3.begin() + s3.size() / 2); s1 = "nioh"; string s6(s1, 2); string s7(s1, 0, 2); string s8(s1, 0, 8);
s2.erase(s2.size() - 5, 5); s2.insert(s2.size(), 5, '!');
char *cp = "nihaoa! xiaohui"; string s; s.assign(cp, 7); s.insert(s.size(), cp + 7); cout << "after insert s: " << s << endl;
s = "hello world"; string ss2 = s.substr(6, 5); cout << "ss2: " << ss2 << endl; cout << "ss2.append('!') = " << ss2.append("!") << endl; cout << "s.replace(6,5,'xiaohui') = " << s.replace(6, 5, "xiaohui") << endl;
string name("Xiaohui"); string::size_type pos1 = name.find("hui");
string numerics("0123456789"); string name2("r2d2"); string::size_type pos = name2.find_first_of(numerics); cout << "fount at index : " << pos << " element is " << name2[pos] << endl;
string name3("49p123"); pos = name3.find_first_not_of(numerics); cout << "find at index : " << pos << "element is " << name3[pos] << endl;
string sp1("aab"); string sp2("ab"); cout << "sp1.compare(sp2) = " << sp1.compare(sp2) << endl; cout << "sp1.compare(1,2,sp2) = " << sp1.compare(1, 2, sp2) << endl; cout << " sp1.compare(1, 2, sp2, 0, 2) = " << sp1.compare(1, 2, sp2, 0, 2) << endl; return 0; }
|