Skip to content

[pull] master from parallel101:master #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions 15/04/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
int n = 42;
auto s = to_string(n) + " yuan"s;
cout << s << endl;
}
10 changes: 10 additions & 0 deletions 15/04/b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
auto s = "42 yuan"s;
int n = stoi(s);
cout << n << endl;
}
14 changes: 14 additions & 0 deletions 15/04/c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
string s = "42yuan"s;
size_t pos;
int n = stoi(s, &pos);
cout << "原始字符串: " << s << endl;
cout << "数字部分从第" << pos << "个字符结束" << endl;
cout << "数字是" << n << endl;
cout << "剩余的部分是" << s.substr(pos) << endl;
}
12 changes: 12 additions & 0 deletions 15/04/d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
string s = "4399cc"s;
cout << "原始字符串" << s << endl;
cout << "作为十六进制" << stoi(s, nullptr, 16) << endl; // 0x4399cc
cout << "作为十进制" << stoi(s, nullptr, 10) << endl; // 4399
cout << "作为八进制" << stoi(s, nullptr, 8) << endl; // 043
}
11 changes: 11 additions & 0 deletions 15/04/e.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
string s = "happy 10th birthday"s;
cout << "字符串: " << s << endl;
int n = stoi(s);
cout << "数字是: " << n << endl;
}
12 changes: 12 additions & 0 deletions 15/04/f.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <string>
#include <iostream>
#include <functional>

using namespace std;

int main() {
string s = "+03.14e-03"s;
cout << "字符串: " << s << endl;
float f = stof(s);
cout << "浮点数: " << f << endl;
}
9 changes: 9 additions & 0 deletions 15/05/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <string>
#include <iostream>
#include <iomanip>

using namespace std;

int main() {
cout << "十六进制:" << hex << 42 << endl;
}
13 changes: 13 additions & 0 deletions 15/05/b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

int main() {
stringstream ss;
ss << "十六进制:" << hex << 42;
string s = ss.str();
cout << s << endl;
}
17 changes: 17 additions & 0 deletions 15/05/c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <string>
#include <iostream>
#include <sstream>
#include <iomanip>

using namespace std;

int main() {
string s = "42yuan"s;
stringstream ss(s);
int num;
ss >> num;
string unit;
ss >> unit;
cout << "数字:" << num << endl;
cout << "单位:" << unit << endl;
}
13 changes: 13 additions & 0 deletions 15/06/b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
string s = "helloworld"s;
cout << "从第2个字符开始长为5的子字符串: " << s.substr(2, 4) << endl;
cout << "从第2个字符开始长为99的子字符串: " << s.substr(2, 99) << endl;
cout << "从第2个字符开始直到末尾的子字符串: " << s.substr(2) << endl;
cout << "从头开始长为5的子字符串: " << s.substr(0, 4) << endl;
cout << "从第100个字符开始长为5的子字符串: " << s.substr(100, 5) << endl;
}
14 changes: 14 additions & 0 deletions 15/06/c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
string s = "helloworld"s;
cout << "寻找h的结果:" << s.find('h') << endl;
cout << "寻找e的结果:" << s.find('e') << endl;
cout << "寻找l的结果:" << s.find('l') << endl;
cout << "寻找o的结果:" << s.find('o') << endl;
cout << "寻找H的结果:" << s.find('H') << endl;
cout << "(size_t) -1:" << (size_t)-1 << endl;
}
11 changes: 11 additions & 0 deletions 15/06/d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
string s = "helloworld"s;
cout << boolalpha;
cout << "h: " << (s.find('h') != string::npos) << endl;
cout << "z: " << (s.find('z') != string::npos) << endl;
}
12 changes: 12 additions & 0 deletions 15/06/e.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
string s = "helloworld"s;
cout << boolalpha;
cout << "lo: " << s.find("lo") << endl;
cout << "wo: " << s.find("wo") << endl;
cout << "ma: " << s.find("ma") << endl;
}
9 changes: 9 additions & 0 deletions 15/07/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include <iostream>
#include <string>

using namespace std;

int main() {
cout << string("hello\0cpp") << endl;
cout << string("hello\0cpp", 9) << endl;
}
22 changes: 22 additions & 0 deletions 15/07/b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include <string>
#include <string_view>
#include <iostream>

using namespace std;

int main() {
string s1 = "hello";
string s2 = s1; // 深拷贝
string_view sv1 = s1; // 弱引用
string_view sv2 = sv1; // 浅拷贝
cout << "s1=" << s1 << endl;
cout << "s2=" << s2 << endl;
cout << "sv1=" << sv1 << endl;
cout << "sv2=" << sv2 << endl;
s1[0] = 'B';
cout << "修改s1后" << endl;
cout << "s1=" << s1 << endl;
cout << "s2=" << s2 << endl;
cout << "sv1=" << sv1 << endl;
cout << "sv2=" << sv2 << endl;
}
Binary file modified 15/slides.pptx
Binary file not shown.