MCPcopy Create free account
hub / github.com/EricPengShuai/Interview / main

Function main

STL/string.cpp:47–122  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

45
46
47int main()
48{
49 /*
50 string相关:初始化字符串为空 && 其他初始化方法
51 string s;
52 string s1(s);
53 string s2(4,'c');
54 */
55
56 string s = "woshihsui";
57
58 // string find方法查看某个字符或者字符串
59 if (s.find("shi") != s.npos) {
60 cout << "\"shi\" first appear in s: " << s.find("shi") << endl;
61 } else {
62 cout << "can't find \"shi\" " << endl;
63 }
64 if (s.find('s') != s.npos) {
65 cout << "\'s\' first apper in s: " << s.find('s') << endl;
66 } else {
67 cout << "'s' not in s" << endl;
68 }
69
70 // substr方法
71 cout << "s[2:]" << s.substr(2) << endl;
72 cout << "s[:2]" << s.substr(0, 2) << endl;
73
74 // trim 函数例子
75 string sp = " blue the sky ";
76 cout << "left trim:" << LTrim(sp).length() << endl;
77 cout << "right trim:" << RTrim(sp).length() << endl;
78 cout << "trim:" << Trim(sp).length() << endl;
79
80
81 /*
82 erase函数的原型如下:
83 (1)string& erase ( size_t pos = 0, size_t n = npos );
84 (2)iterator erase ( iterator position );
85 (3)iterator erase ( iterator first, iterator last );
86 也就是说有三种用法:
87 (1)erase(pos,n); 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
88 (2)erase(position);删除position处的一个字符(position是个string类型的迭代器)
89 (3)erase(first,last);删除从first到last之间的字符(first和last都是迭代器)
90 */
91 s.erase(s.begin());
92 cout << s << endl;
93
94
95 /*
96 <sstream> 定义了三个类:
97 istringstream、ostringstream 和 stringstream,分别用来进行流的输入、输出和输入输出操作.
98 参考: https://urlify.cn/vMrYzm
99 */
100 stringstream sstream;
101 // 输入到stringstream
102 sstream << "first string," << " second string";
103 cout << "strResult is: " << sstream.str() << endl;
104

Callers

nothing calls this directly

Calls 3

LTrimFunction · 0.85
RTrimFunction · 0.85
TrimFunction · 0.85

Tested by

no test coverage detected