Luo Gu solution to a problem from https://www.luogu.org/blog/co2021/solution-p1032
1. Read spaces
getline (CIN, A); // can be read into the spaces
2.c_str ()
int main() { char c[20]; string s = "12345456"; strcpy_s(c, s.c_str()); cout << c << endl; cout << c[3]; //printf("%s", s.c_str()); getchar(); getchar(); return 0; }
int main() { string s = "12345456"; const char *c = s.c_str(); cout << c << endl; cout << c[3]; //printf("%s", s.c_str()); getchar(); getchar(); return 0; }
3. Insert insert
str.insert (k, str2), inserted in str2 k-th position.
4. Remove erase
str.erase (k, num) delete the k-th character begins num characters.
5. Alternatively replace
str.replace (k, num, str2); starting from the k-th character two characters replaced str2 string.
String S = " 12,345,456 " ; s.replace does ( 0 , 2 , " AS " ); COUT << S; // result as345456
6.find
str.find (str2) // Returns the string str position of the first occurrence of str2
str.find (str2, k) // k start looking from the position in str str2 appear
rfind is looking backwards, for example: the output of Example 7
int main() { string s = "1200345456"; int k=s.rfind("45"); cout << k << endl; cout << s; //printf("%s", s.c_str()); getchar(); getchar(); return 0; }
find_first_of (c, k) // front looking back
find_last_of (c, k) // looking from back to front from start to find k, find the character c, find the return, the match fails to return npos
find_first_not_of()
str.find_last_not_of (str2, k) search for a character in str2 not in str, and returns the first location found.
7.substr()
substr (begin, end) // end if omitted, it means to the end
str="sdnsaddfl amsdlk";
str.substr (0,3); // index is from 0 to 3 i.e. sdn
8.