forked from landbroken/BasicKnowledge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring.cpp
More file actions
241 lines (215 loc) · 5.14 KB
/
Copy pathstring.cpp
File metadata and controls
241 lines (215 loc) · 5.14 KB
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
// string.cpp : 定义控制台应用程序的入口点。
// 标准C++中string类,必须要#include<string>和using namespace std;
// 然后就可以用string/wstring了,它们两分别对应着char和wchar_t
// 主要参考文献
// 1、标准C++中的string类的用法总结(转)
// http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html
#include "stdafx.h"
#include<string>
#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
/*
初始化变量demo
*/
void InitialDemo()
{
string str1(5, 'c');//初始化为5个'c'
string str2("Now is the time...");
string str3(str2, 11, 4);//从str2的第11个字符开始,读取4个字符
string str4;
str4.append(3, 'h');//增加3个'h'
char chr[6];
memset(chr, '\0', sizeof(chr));
chr[0] = '2';
string str5(chr);
cout << endl << "InitialDemo:" << endl;
cout << str1 << endl;
if(str1[5]== '\0')
{
cout << "越界输出:str1[5]=\0" << endl;//空
}
else
{
cout << "越界输出:str1[5]=" << str1[5] << endl;//空
}
//cout << "越界输出:str1[6]=" << str1[6] << endl;//异常
cout << str2 << endl;
cout << str3 << endl;
cout << str4 << endl;
cout << str5 << endl;
}
/*
赋值和替换demo
*/
void AssignReplaceDemo()
{
cout << endl << "AssignReplaceDemo:" << endl;
string str1, str2 = "War and Peace";
str1.assign(str2, 4, 3);
//结果为and
cout << str1 << endl;
string s = "They say he carved it himself...from a BIGGER spoon";
string s2 = "find your soul-mate, Homer.";
//s[1] = "1";不能用这种形式
//replace(off,number,new string)
//结果为They find your soul-mate, Homer.from a BIGGER spoon
s.replace(5, s2.length(), s2);
cout << s << endl;
}
string ReverseStringDemo(string s)
{
int left = 0, right = s.size() - 1;
while (left < right)
{
char t = s[left];
s[left++] = s[right];
s[right--] = t;
}
return s;
}
string ReverseStringDemo2(string s) {
int left = 0, right = s.size() - 1;
while (left < right) {
swap(s[left++], s[right--]);
}
return s;
}
/*
string转为char*类型
char*转为string类型
*/
void Change2CharDemo()
{
cout << endl << "Change2CharDemo:" << endl;
string str1 = "War and Peace";
const char* chr = str1.c_str();
cout << chr << endl;
//定义成char chr2[10]是一样的
char* chr2 = "to_string";
string str2 = chr2;
cout << str2 << endl;
}
/*
string转为Int类型
int转为string类型
*/
void Change2IntDemo()
{
cout << endl << "Change2IntDemo" << endl;
//stoi:string转int
string str1 = "-20";
string str2 = "5";
int ret = stoi(str1) + stoi(str2);
cout << "string to int: -20+5=" << ret << " " << endl;
//to_string:数值转string
int a = -3, b = 37;
string a_plus_b = to_string(a + b);
cout << "int to string: -3+37=" << a_plus_b << " " << endl;
//strtol:string转long。_Radix表示进制。
//stol:string转long,并做进制转换。Base=0时使用默认的十进制
string str3 = "-0x12";
string str4 = "0x76E";//1902
char *offset;
cout << "string to int: -0x12=" << strtol(str3.c_str(),&offset,0) << " " << endl;
cout << "string to int: 0x76E=" << strtol(str4.c_str(), &offset, 0) << " " << endl;
cout << "string to int: 0x76E=" << stol(str4, nullptr,0) << " " << endl;
string str5 = "A4";
char *offset5;
int istr5 = strtol(str5.c_str(), &offset5, 10);
cout << "string to int: A4=" << istr5 << " " << endl;//0
}
void SubStringDemo()
{
string str = "20+-22i";
int size = str.size();
int plus = str.find("+");
string real = str.substr(0, plus);//off,count
string imaginary = str.substr(plus + 1, size - plus - 2);
cout << "substring: " << real << " " << imaginary << endl;
}
void FindDemo()
{
string str;
cout << "FindDemo: please cin words and find spaces "<<endl;
getline(cin, str);
//hello world=5
//helloworld=-1=string
int begin = str.find(' ');
int end = str.find_last_of(' ');//hello world =5
string last(str, end + 1, str.size() - end);
cout << last<<" "<<last.size() << endl;;
}
void AddDemo()
{
cout << endl << "AddDemo" << endl;
string str = "";
str += "5";
cout << str << endl;
str.append("11");
cout << str << endl;
str.append(5,'0');
cout << str << endl;
}
void CompareDemo()
{
cout << endl << "CompareDemo:" << endl;
string str1("aaa");
string str2("aab");
string str3("aaaa");
bool ret = str1 < str2;
cout << ret << endl;
ret = str1 < str3;
cout << ret << endl;
ret = str2 < str3;
cout << ret << endl;
}
void WStringDemo()
{
wstring wstr1;
wstr1 = L"我ABC的string";
wstring wstr2(L"我A的XX啊");
int s1 = wstr1.size();
}
/*
用空格分隔字符串示例
STL没有原生的split函数
*/
void SplitDemo() {
cout << endl << "begin SplitDemo" << endl;
string str = "hello world! this is a string.";
vector<string> str_arr;
string temp;
int offset = 0;
int newOffset = 0;
//获取空格位置,没有空格时返回-1
while (offset<str.size()&& newOffset>=0)
{
newOffset = str.find(' ', offset);
temp = str.substr(offset, newOffset - offset);
str_arr.push_back(temp);
offset = newOffset + 1;
}
cout << "origin string = " << str << endl;
cout << "split arr size = " << str_arr.size() << endl;
for (int i = 0;i < str_arr.size();i++) {
cout << "str_arr[" << i << "] = " << str_arr[i] << endl;
}
cout << "end SplitDemo" << endl;
}
int main()
{
SplitDemo();
WStringDemo();
InitialDemo();
AssignReplaceDemo();
Change2CharDemo();
Change2IntDemo();
SubStringDemo();
FindDemo();
AddDemo();
CompareDemo();
system("pause");
return 0;
}