-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_test.cpp
More file actions
72 lines (65 loc) · 1.65 KB
/
Copy pathstring_test.cpp
File metadata and controls
72 lines (65 loc) · 1.65 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
/*
参考文档
https://juejin.cn/post/7086694183825571870
*/
#include <iostream>
#include <cstring>
using namespace std;
int main(int argc, char** argv) {
//一、原始字符串char[]
char a[10] = "123";
cout << a << endl;
//可以溢出的输入
// cin >> a;
// cout << a << endl;
// cout << sizeof(a) << endl;
//不溢出输入
// char b[10] = {0};
// cin.get(b, sizeof(b));
// cout << b << endl;
// cout << sizeof(b) << endl;
//不溢出输入
// char c[10] = {0};
// cin.getline(c, sizeof(c));
// cout << c << endl;
// cout << sizeof(c) << endl;
//cstring
char d[10] = "abcdefg";
char e[10] = {0};
char f[20] = {0};
cout << d << endl;
cout << strlen(d) << endl;//字符串长度
strcpy(e, d);//字符拷贝
cout << e << endl;
strcat(f, d);//字符串连接
strcat(f, e);
cout << f << endl;
cout << strstr(f, "ga") - f<< endl;//字符串搜索 地址相减,获取偏移量
//二、td::string
string s = "xyz";
// cin >> s;//字符串输入
cout << s << endl;
cout << s.length() << endl;//字符串长度
string s1 = s;//字符拷贝
cout << s1 << endl;
//字符串比较
cout << bool(s1 == s) << endl;
cout << s1.compare(s) << endl;
string s2 = "abc";
string s3 = "c";
cout << bool(s2 == s3) << endl;
cout << bool(s2 > s3) << endl;
cout << bool(s2 < s3) << endl;
cout << s2.compare(s3) << endl;
cout << s1.find("z") << endl;//字符串搜索
cout << s + s1 << endl;//字符串拼接
//三、char[] 和 string 转化
string ss = "123456";
char aa[100] = {0};
strcpy(aa, ss.c_str());
cout << aa << endl;
aa[0] = '9';
ss = aa;
cout << ss <<endl;
return 0;
}