Skip to content

[pull] master from parallel101:master #7

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 2 commits into from
Jul 16, 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
14 changes: 14 additions & 0 deletions 15/06/a.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 << "第0个字符: " << s[0] << endl;
cout << "第3个字符: " << s[3] << endl;
cout << "第16个字符: " << s[16] << endl;
cout << "第0个字符: " << s.at(0) << endl;
cout << "第3个字符: " << s.at(3) << endl;
cout << "第16个字符: " << s.at(16) << endl;
}
11 changes: 11 additions & 0 deletions 15/06/a2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <string>
#include <string_view>
#include <iostream>

using namespace std;

int main() {
string s = "hello";
cout << s.size() << endl;
cout << s.length() << endl;
}
20 changes: 20 additions & 0 deletions 15/06/f.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <string>
#include <iostream>

using namespace std;

size_t count(string const &str, string const &sub) {
size_t n = 0, pos = 0;
while (true) {
pos = str.find(sub, pos);
if (pos == str.npos)
break;
++n;
pos += sub.size();
}
return n;
}

int main() {
cout << count("helloworld,bellreally"s, "ll"s) << endl;
}
10 changes: 10 additions & 0 deletions 15/06/f2.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
string s = "helloworld"s;
int n = s.find_first_of("onl");
cout << n << endl;
}
28 changes: 28 additions & 0 deletions 15/06/f3.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <string>
#include <vector>
#include <iostream>

using namespace std;

vector<string> split(string s) {
vector<string> ret;
size_t pos = 0;
while (true) {
size_t newpos = s.find_first_of(" \t\v\f\n\r", pos);
if (newpos == s.npos) {
ret.push_back(s.substr(pos, newpos));
break;
}
ret.push_back(s.substr(pos, newpos - pos));
pos = newpos + 1;
}
return ret;
}

int main() {
string s = "hello world pyb teacher good job"s;
vector<string> v = split(s);
for (auto const &vi: v) {
cout << vi << endl;
}
}
11 changes: 11 additions & 0 deletions 15/06/f4.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <string>
#include <vector>
#include <iostream>

using namespace std;

int main() {
string s = "hello world, pyb teacher? good job!"s;
int n = s.find_first_not_of(" abcdefghijklmnopqrstuvwxyz");
cout << n << endl;
}
10 changes: 10 additions & 0 deletions 15/06/g.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
string s = "make pyb happy"s;
s.replace(5, 3, "zhxx"); // 变成 make zhxx happy
cout << s << endl;
}
21 changes: 21 additions & 0 deletions 15/06/h.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <string>
#include <iostream>

using namespace std;

void replace_all(string &s, string const &from, string const &to) {
size_t pos = 0;
while (true) {
pos = s.find(from, pos);
if (pos == s.npos)
break;
s.replace(pos, from.size(), to);
pos += to.size();
}
}

int main() {
string s = "make pyb happy, as pyb said, cihou pyb"s;
replace_all(s, "pyb", "zhxx");
cout << s << endl;
}
24 changes: 24 additions & 0 deletions 15/06/i.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <string>
#include <iostream>

using namespace std;

int main() {
string s;

s = "hello, "s;
s.append("world"s); // "hello, world"
cout << s << endl;

s = "hello, "s;
s.append("world"); // "hello, world"
cout << s << endl;

s = "hello, "s;
s.append("world"s, 3); // "hello, ld"
cout << s << endl;

s = "hello, "s;
s.append("world", 3); // "hello, wor"
cout << s << endl;
}
10 changes: 10 additions & 0 deletions 15/06/j.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 = "HELLO"s;
s.insert(2, "world");
cout << s << endl;
}
10 changes: 10 additions & 0 deletions 15/06/k.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 = "HELLO"s;
s = s.substr(0, 2) + "world"s + s.substr(2);
cout << s << endl;
}
15 changes: 15 additions & 0 deletions 15/06/l.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <string>
#include <string_view>
#include <iostream>

using namespace std;

int main() {
auto s = "HELLO"s;
string_view sv = s;
string news;
news += sv.substr(0, 2);
news += "world"sv;
news += sv.substr(2);
cout << news << endl;
}
14 changes: 14 additions & 0 deletions 15/07/c.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <string>
#include <string_view>
#include <iostream>

using namespace std;

int main() {
string s1 = "hello";
string_view sv1 = s1;
s1[0] = 'M';
cout << sv1 << endl; // 不会失效
s1 = "helloworld";
cout << sv1 << endl; // 失效!
}
12 changes: 12 additions & 0 deletions 15/07/d.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include <string>
#include <string_view>
#include <iostream>

using namespace std;

int main() {
string s = "hello";
string_view sv = s;
sv = sv.substr(1, 3);
cout << sv << endl;
}
28 changes: 28 additions & 0 deletions 15/08/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <string>
#include <vector>
#include <string_view>
#include <iostream>

using namespace std;

struct Empty {
};

struct NonEmpty {
char c;
};

struct DerivedEmpty : Empty {
int i;
};

struct DerivedNonEmpty : NonEmpty {
int i;
};

int main() {
cout << sizeof(Empty) << endl; // 1 = 1(empty)
cout << sizeof(NonEmpty) << endl; // 1 = 1(char c)
cout << sizeof(DerivedEmpty) << endl; // 4 = 0(empty) + 4(int i)
cout << sizeof(DerivedNonEmpty) << endl; // 8 = 1(char c) + 3(padding) + 4(int i)
}
55 changes: 55 additions & 0 deletions 15/09/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <string>
#include <iostream>
#include <iomanip>
#include <cstring>

using namespace std;

int main() {
cout << "\x7a\u00df\u6c34\U0001f34c" << endl;
{
cout << "char: ";
string s = "\x7a\u00df\u6c34\U0001f34c";
for (size_t i = 0; i < s.size(); i++) {
cout << setw(2) << setfill('0') << hex;
cout << hex << (uint32_t)(uint8_t)s[i] << ' ';
}
cout << endl;
}
{
cout << "wchar_t: ";
wstring s = L"\x7a\u00df\u6c34\U0001f34c";
for (size_t i = 0; i < s.size(); i++) {
cout << setw(sizeof(wchar_t) * 2) << setfill('0') << hex;
cout << (uint32_t)s[i] << ' ';
}
cout << endl;
}
//{
//cout << "char8_t: ";
//u8string s = u8"\x7a\u00df\u6c34\U0001f34c";
//for (size_t i = 0; i < s.size(); i++) {
//cout << setw(2) << setfill('0') << hex;
//cout << (uint16_t)s[i] << ' ';
//}
//cout << endl;
//}
{
cout << "char16_t: ";
u16string s = u"\x7a\u00df\u6c34\U0001f34c";
for (size_t i = 0; i < s.size(); i++) {
cout << setw(4) << setfill('0') << hex;
cout << (uint16_t)s[i] << ' ';
}
cout << endl;
}
{
cout << "char32_t: ";
u32string s = U"\x7a\u00df\u6c34\U0001f34c";
for (size_t i = 0; i < s.size(); i++) {
cout << setw(8) << setfill('0') << hex;
cout << (uint32_t)s[i] << ' ';
}
cout << endl;
}
}
71 changes: 71 additions & 0 deletions 15/09/b.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <string>
#include <string_view>
#include <stdexcept>
#include <iostream>
#include <iomanip>
#include <locale>

using namespace std;

template <class Internal = wchar_t>
string convcodec(string_view src, locale const &srcLoc, locale const &dstLoc) {
auto strCodecvtError = [] (codecvt_base::result res) -> string {
if (res == codecvt_base::result::noconv)
return "noconv";
if (res == codecvt_base::result::error)
return "error";
if (res == codecvt_base::result::partial)
return "partial";
return "ok";
};

string dst;
basic_string<Internal> tmp;
mbstate_t mbstate = {};

auto &srcFacet = use_facet<codecvt<Internal, char, mbstate_t>>(srcLoc);
tmp.resize(src.size());

const char *src_begin = src.data();
const char *src_end = src.data() + src.size();
const char *src_next;
Internal *tmp_begin = tmp.data();
Internal *tmp_end = tmp.data() + tmp.size();
Internal *tmp_next;
auto resIn = srcFacet.in(mbstate, src_begin, src_end, src_next, tmp_begin, tmp_end, tmp_next);
if (resIn != codecvt_base::result::ok)
throw runtime_error("cannot decode input sequence: " + strCodecvtError(resIn));
size_t tmp_size = tmp_next - tmp_begin;
tmp_end = tmp_next;

auto &dstFacet = use_facet<codecvt<Internal, char, mbstate_t>>(dstLoc);
dst.resize(tmp_size * dstFacet.max_length());

char *dst_begin = dst.data();
char *dst_end = dst.data() + dst.size();
char *dst_next;
const Internal *tmp_next_next;
auto resOut = dstFacet.out(mbstate, tmp_begin, tmp_end, tmp_next_next, dst_begin, dst_end, dst_next);
if (resOut != codecvt_base::result::ok)
throw runtime_error("cannot encode output sequence: " + strCodecvtError(resOut));
size_t dst_size = dst_next - dst_begin;

dst.resize(dst_size);
return dst;
}

void showstr(string_view s) {
for (char c: s) {
cout << hex << setfill('0') << setw(2);
cout << (uint32_t)(uint8_t)c << ' ';
}
cout << s << endl;
}

int main() {
string s = "你好";
//string s = "你好\U0001f34c"; // error: cannot encode U+1F34C
showstr(s);
s = convcodec(s, locale("en_US.UTF-8"), locale("zh_CN.GBK"));
showstr(s);
}
Binary file modified 15/slides.pptx
Binary file not shown.