-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.cpp
82 lines (56 loc) · 1.87 KB
/
example.cpp
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
#include <deque>
#include <iostream>
#include <unordered_map>
#include <vector>
#include "py_string.hpp"
int main(int argc, char const *argv[])
{
using namespace std;
// string multiplication
py::string multiplication = "hello world!";
std::cout << multiplication * 2 << std::endl;
// hello world!hello world!
// Subscript operator
py::string Subscript_operator = "0123456789";
cout << Subscript_operator[0] << endl;
cout << Subscript_operator[-1] << endl; // last element
cout << Subscript_operator[-3] << endl; // third element from the back (7)
// string slice
py::string slice = "0123456789";
cout << slice.slice(0, 20, 2) << endl;
cout << slice.slice(0, 20, -2) << endl;
cout << slice[{ 4, py::nullopt }] << endl;
// string format
py::string format = "{} is {} than {}";
cout << format.format(3, "bigger", 1) << endl;
cout << format.format(2, "smaller", 4) << endl;
py::string format2 = "{0} is {1} as {0}";
cout << format2.format(2, "same") << endl;
// string replace
py::string replace = "1+2-3";
cout << replace.pyreplace("-", "=");
// 1+2=3
// string split
py::string split = "12,3,4-2,2,3";
std::deque<py::string> slist; // std::vector is also acceptable
split.split(slist, ",");
for (auto &&s : slist) {
cout << s << endl;
}
// Check ASCII character string
py::string ascii = "Hello World!";
cout << std::boolalpha << ascii.isascii() << endl;
py::string not_ascii = "ハローワールド!";
cout << std::boolalpha << not_ascii.isascii() << endl;
// translate
py::string tstr = "text abc so cool.";
auto table = py::string::maketrans("abcd", "ABCD", ".");
std::unordered_map<char, py::string> _t;
_t['.'] = "!!";
_t['s'] = "";
_t['o'] = "O";
auto table2 = py::string::maketrans(_t);
cout << tstr.translate(table) << endl;
cout << tstr.translate(table2) << endl;
return 0;
}