标签:ring 检查 -- 成员 wap resize cout 复杂 form
string s1("Hello");
string s2("Hello", 3); //s2: Hel
string s3(s1, 2); //s3: llo
string s4(s1, 2, 2); //s4: ll
string s5(5, ‘a‘); //s5: aaaaa
string s6({‘a‘, ‘b‘, ‘c‘}); //s6: abc
s1="Goodbye"
s1.size(), s1.length() //7
s1.capacity();
s1.reserve(100);
s1.reserve(5); //s1: Goodbye, sizs()==7,capacity()>=7 不改变内容和size,只改变容量
s1.shrink_to_fit(); //改变容量到承载内容
s1.resize(9); //s1: Goodbye\0\0
s1.resize(12, ‘x‘); //s1: Goodbye\0\0xxx
s1.resize(3); //s1: Goo
s1.at()会检查元素是否存在
s1[]不会检查
front, back, push_back, pop_back, begin, end
string s2 = "Dragon Land";
s1.assign(s2); // s1 = s2;
s1.assign(s2, 2, 4); // s1: ago
s1.assign("Wisdom");
s1.assign("Wisdom", 3); // s1: Wis
s1.assign(s2, 3); // Error
s1.assign(3, ‘x‘); // s1: xxx
s1.assign({‘a‘, ‘b‘, ‘c‘}); // s1: abc
s1.append(" def"); // s1: abc def
s1.insert(2, "mountain", 4); // s1: abmounc def
s1.replace(2, 5, s2, 3, 3); // s1: abgon def
s1.erase(1, 4); // s1: a def
s2.substr(2, 4); // agon
s1 = "abc";
s1.c_str(); // "abc\0"
s1.data(); // "abc" c++11中结尾也有\0
s1.swap(s2);
s1 = "abcdefg";
char buf[20];
size_t len = s1.copy(buf, 3); // buf:abc len == 3
len = s1.copy(buf, 4, 2); // buf:cdef len == 4
s1 = "If a job is worth doing, it‘s worth doing well";
size_t found = s1.find("doing"); // found == 18
found = s1.find("doing", 20); // found == 36
found = s1.find("doing well", 0); // found == 36
found = s1.find("doing well", 0, 5); // found == 18
found = s1.find_first_of("doing"); // found == 6
found = s1.find_first_of("doing", 10); // found == 13
found = s1.find_first_of("doing", 10, 1); // found == 18
found = s1.find_last_of("doing"); // found == 40
found = s1.find_first_not_of("doing"); // found == 0
found = s1.find_last_not_of("doing"); // found == 45
s1.compare(s2);
if (s1 > s2) {} //当size很大时,expensive
s1.compare(3, 2, s2);
string ss = s1 + s2; //当size很大时,expensive
cout << s1 << endl;
cin >> s1;
getline(cin, s1); //‘\n‘
getline(cin, s1, ‘;‘); //‘;‘
//数字转成string
s1 = to_string(8);
s1 = to_string(2.3e7); // s1: 23000000.000000
s1 = to_string(0xa4); // s1: 164
s1 = to_string(034); // s1: 28
//string转数字
s1 = "190";
int i = stoi(s1); //i == 190
s1 = "190 monkey";
size_t pos;
int i = stoi(s1, &pos); //i == 190 pos == 3
s1 = "a monkey";
int i = stoi(s1, &pos); // exception of invalid_argument
int i = stoi(s1, &pos, 16); // i == 10
//stol, stod, stof, etc 以上函数只做简单string转换
//复杂格式使用stringstream
//lexical_cast() //简单string转换
s1 = "Variety is the spice of life.";
int num = count(s1.begin(), s1.end(), ‘e‘); //4
num = count_if(s1.begin(), s1.end(), [](char c) {return (c <= ‘e‘ && c >= ‘a‘);}); //6
s1 = "Goodness is better than beauty.";
string::iterator itr = search_n(s1.begin(), s1.begin()+20, 2, ‘s‘); // itr -> first ‘s‘
s1.erase(itr, itr+5); //ss is
s1.insert(itr, 3, ‘x‘); //Goodnexxxss
s1.replace(itr, itr+3, 3, ‘y‘); //Goodneyyyis replace substring
is_permutation(s1.begin(), s1.end(), s2.begin()); //检查s1是否是s2的排列
replace(s1.begin(), s1.end(), ‘e‘, ‘‘); //replace characters
transform(s1.begin(), s1.end(), s2.begin(),
[](char c) {
if (c < ‘n‘)
return ‘a‘;
else
return ‘z‘;
});
s1 = "abcdefg";
rotate(s1.begin(), s1.begin()+3, s1.end()); //s1: defgabc
u16string s9; //string char16_t
u32string s8; //string char32_t
wstring s0; //string wchar_t
to_wstring();
标签:ring 检查 -- 成员 wap resize cout 复杂 form
原文地址:https://www.cnblogs.com/logchen/p/10204000.html