std::strings("12345"); atoi(s.c_str()) // convert string into integer char buffer[100]; s.copy(buffer, 100); // copy at most 100 characters of s into buffer s.copy(buffer, 100, 2); // copy at most 100 characters of s into buffer starting with the third character of s
大小与容量
size() and length() 等价,返回当前string有多少字符。**empty()**用于检查是否为空,快。
std::strings("abcd"); s.compare("abcd"); // returns 0 s.compare("dcba"); // returns a value < 0(s is less) s.compare("ab"); // returns a value > 0(s is greater) s.compare(0,2,s,2,2); // returns a value < 0("ab" is less than "cd") s.compare(1,2,"bcx",2); // returns 0("bc" is equal to "bc")
进行修改的操作
赋值
operator=, assign()
1 2 3 4 5 6 7 8 9 10 11
conststd::stringastring("othello"); std::string s; s = astring; // assign "othello" s = "two\nlines"; // assign a C-string s = ' '; // assign a single character s.assign(astring); // assign "othello" s.assign(astring,1,3); // assign "the" s.assign(astring,2,std::string::npos); // assign "hello" s.assign("two\nlines"); // assign a C-string (equivaluent to operator=) s.assign("nico",5); // assign the character array: 'n''i''c''o''\0' s.assign(5, 'x'); // assign 5 characters: 'x''x''x''x''x'
交换
string成员函数swap()保证常数时间复杂度。
将string置空
1 2 3 4
std::string s; s = ""; // assign the empty string s.clear(); // clear contents s.erase(); // erase all characters
插入与删除字符
append
1 2 3 4 5 6 7 8 9 10 11 12 13
conststd::stringastring("othello"); std::string s; s += astring; // append "othello" s += "two\nlines"; // append C-string s += '\n'; // append single character s += {'o', 'k'}; // append an initializer list of character (since C++11) s.append(astring); // append "othello" s.append(astring, 1, 3); // append "the" s.append(astring, 2, std::string::npos); // append "hello" s.append("two\nlines"); // append C-string s.append("nico", 5); // append character array: 'n''i''c''o''\0' s.append(5, 'x'); // append 5 characters: 'x''x''x''x''x' s.push_back('\n'); // append single character
#include<iostream> #include<string> usingnamespacestd; intmain(int argc, char* argv[]) { string filename, basename, extname, tmpname; conststringsuffix("tmp"); // for each command-line argument (which is an ordinary C-string) for (int i = 1; i < argc; ++i) { // process argument as filename filename = argv[i]; // search period in filename string::size_type idx = filename.find('.'); if (idx == string::npos) { // filename does not contain any period tmpname = filename + '.' + suffix; }else { // split filename into base name and extension // - base name contains all characters before the period // - extension contains all characters after the period basename = filename.substr(0, idx); extname = filename.substr(idx+1); if (extname.empty()) { // contains period but no extension: append tmp tmpname = filename; tmpname += suffix; }elseif (extname == suffix) { // replace extension tmp with xxx tmpname = filename; tmpname.replace(idx+1, extname.size(), "xxx"); }else { // replace any extension with tmp tmpname = filename; tmpname.replace(idx+1, string::npos, suffix); } } // print filename and temporary name cout << filename << " => " << tmpname << endl; } system("Pause"); }
#include<iostream> #include<string> usingnamespacestd; intmain(int argc, char* argv[]) { conststringdelims(" \t,.;"); string line; // for every line read successfully while (getline(cin, line)) { string::size_type begIdx, endIdx; // search beginning of the first word begIdx = line.find_first_not_of(delims); // while beginning of a word found while (begIdx != string::npos) { // search end of the actual word endIdx = line.find_first_of(delims, begIdx); if (endIdx == string::npos) { // end of word is end of line endIdx = line.length(); } // print characters in reverse order for (int i = endIdx - 1; i >= static_cast<int>(begIdx); --i) { cout << line[i]; } cout << ' '; // search beginning of the next word begIdx = line.find_first_not_of(delims, endIdx); } cout << endl; } system("Pause"); }