构造函数:
std::string s0 ("Initial string");
// constructors used in the same order as described above:
std::string s1;
std::string s2 (s0);
std::string s3 (s0, 8, 3);//注意位置信息使用的是常量,不是迭代器。
std::string s4 ("A character sequence");
std::string s5 ("Another character sequence", 12);
std::string s6a (10, 'x');
std::string s6b (10, 42); // 42 is the ASCII code for '*'
std::string s7 (s0.begin(), s0.begin()+7);
s1: s2: Initial string s3: str s4: A character sequence s5: Another char s6a: xxxxxxxxxx s6b: ********** s7: Initial
赋值运算符重载(operator=):
string (1) | string& operator= (const string& str); |
---|---|
c-string (2) | string& operator= (const char* s); |
character (3) | string& operator= (char c); |
initializer list (4) | string& operator= (initializer_list<char> il); |
move (5) | string& operator= (string&& str) noexcept; |
std::string str1, str2, str3;
str1 = "Test string: "; // c-string
str2 = 'x'; // single character
str3 = str1 + str2; // string
迭代器:
跟vector的迭代器一样,反向和常量迭代器。
容量:
size,length,max_size,resize,capacity,reserve,clear,empty,shrink_to_fit;
与vector相比多了一个length方法,与size完全一样。
元素访问:
operator[], at, back, front
注意str.back() 和 str.front 是左值,可以在等号左边,也可以在右边。
修改:
operator+= (注意与非成员函数operator+的区别)
std::string name ("John");
std::string family ("Smith");
name += " K. "; // c-string
name += family; // string
name += '\n'; // character
append (通过尾部追加额外的字符来扩展原string)
std::string str;
std::string str2="Writing ";
std::string str3="print 10 and then 5 more";
// used in the same order as described above:
str.append(str2); // "Writing "
str.append(str3,6,3); // "10 "
str.append("dots are cool",5); // "dots "
str.append("here: "); // "here: "
str.append(10u,'.'); // ".........."
str.append(str3.begin()+8,str3.end()); // " and then 5 more"
str.append<int>(5,0x2E); // "....."
std::cout << str << '\n';
Writing 10 dots here: .......... and then 5 more.....
push_back 尾部添加一个字符,只接受char类型。pop_back删除字符串最后一个char.
void push_back (char c);
void pop_back();
assign 分配,通常是先定义空string A,然后通过assign将A分配为某一字符串。
insert 函数较多,输入参数可以是pos位置信息,也可以是迭代器;返回参数可以是迭代器,也可以是string。
string (1) | string& insert (size_t pos, const string& str); |
---|---|
substring (2) | string& insert (size_t pos, const string& str, size_t subpos, size_t sublen); |
c-string (3) | string& insert (size_t pos, const char* s); |
buffer (4) | string& insert (size_t pos, const char* s, size_t n); |
fill (5) | string& insert (size_t pos, size_t n, char c); iterator insert (const_iterator p, size_t n, char c); |
single character (6) | iterator insert (const_iterator p, char c); |
range (7) | template <class InputIterator> iterator insert (iterator p, InputIterator first, InputIterator last); |
initializer list (8) | string& insert (const_iterator p, initializer_list<char> il); |
std::string str="to be question";
std::string str2="the ";
std::string str3="or not to be";
std::string::iterator it;
// used in the same order as described above:
str.insert(6,str2); // to be (the )question
str.insert(6,str3,3,4); // to be (not )the question
str.insert(10,"that is cool",8); // to be not (that is )the question
str.insert(10,"to be "); // to be not (to be )that is the question
str.insert(15,1,':'); // to be not to be(:) that is the question
it = str.insert(str.begin()+5,','); // to be(,) not to be: that is the question
str.insert (str.end(),3,'.'); // to be, not to be: that is the question(...)
str.insert (it+2,str3.begin(),str3.begin()+3); // (or )
std::cout << str << '\n';
erase 接受迭代器参数才返回迭代器参数,接受pos位置参数则返回修改后string。
sequence (1) | string& erase (size_t pos = 0, size_t len = npos); |
---|---|
character (2) | iterator erase (const_iterator p); |
range (3) | iterator erase (const_iterator first, const_iterator last); |
std::string str ("This is an example sentence.");
std::cout << str << '\n';
// "This is an example sentence."
str.erase (10,8); // ^^^^^^^^
std::cout << str << '\n';
// "This is an sentence."
str.erase (str.begin()+9); // ^
std::cout << str << '\n';
// "This is a sentence."
str.erase (str.begin()+5, str.end()-9); // ^^^^^
std::cout << str << '\n';
replace 替换(返回值都是string&)
std::string base="this is a test string.";
std::string str2="n example";
std::string str3="sample phrase";
std::string str4="useful.";
// replace signatures used in the same order as described above:
// Using positions: 0123456789*123456789*12345
std::string str=base; // "this is a test string."
str.replace(9,5,str2); // "this is an example string." (1)
str.replace(19,6,str3,7,6); // "this is an example phrase." (2)
str.replace(8,10,"just a"); // "this is just a phrase." (3)
str.replace(8,6,"a shorty",7); // "this is a short phrase." (4)
str.replace(22,1,3,'!'); // "this is a short phrase!!!" (5)
// Using iterators: 0123456789*123456789*
str.replace(str.begin(),str.end()-3,str3); // "sample phrase!!!" (1)
str.replace(str.begin(),str.begin()+6,"replace"); // "replace phrase!!!" (3)
str.replace(str.begin()+8,str.begin()+14,"is coolness",7); // "replace is cool!!!" (4)
str.replace(str.begin()+12,str.end()-4,4,'o'); // "replace is cooool!!!" (5)
str.replace(str.begin()+11,str.end(),str4.begin(),str4.end());// "replace is useful." (6)
swap 交换
str1.swap(str2);
字符串操作:
c_str 提供string到C类型字符串的转换
const char* c_str() const noexcept;
copy 将当前string的部分拷贝到目的位置。
char buffer[20];
std::string str ("Test string...");
std::size_t length = str.copy(buffer,6,5);
buffer[length]='\0';
std::cout << "buffer contains: " << buffer << '\n';
find 查找字符串或字符,并返回查找到的位置。注意std::string::npos(默认-1)这个常量表示未找到。rfind反向查找。
std::string str ("There are two needles in this haystack with needles.");
std::string str2 ("needle");
// different member versions of find in the same order as above:
std::size_t found = str.find(str2);
if (found!=std::string::npos)
std::cout << "first 'needle' found at: " << found << '\n';
found=str.find("needles are small",found+1,6);
if (found!=std::string::npos)
std::cout << "second 'needle' found at: " << found << '\n';
found=str.find("haystack");
if (found!=std::string::npos)
std::cout << "'haystack' also found at: " << found << '\n';
found=str.find('.');
if (found!=std::string::npos)
std::cout << "Period found at: " << found << '\n';
// let's replace the first needle:
str.replace(str.find(str2),str2.length(),"preposition");
std::cout << str << '\n';
find_first_of 接受string或char,找到string第一个字符,其匹配输入参数中任意一个char.
find_last_of 找到最后一个匹配的位置。
find_first_not_of
find_last_not_of
std::string str ("Please, replace the vowels in this sentence by asterisks.");
std::size_t found = str.find_first_of("aeiou");//匹配其中任意一个
while (found!=std::string::npos)
{
str[found]='*';
found=str.find_first_of("aeiou",found+1);
}
Pl**s*, r*pl*c* th* v*w*ls *n th*s s*nt*nc* by *st*r*sks. |
substr 取出string的字串。接受的是pos位置参数。第一个表示位置,第二个人表示长度,都有默认参数。
string substr (size_t pos = 0, size_t len = npos) const;
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos); // get from "live" to the end
compare 比较string与其参数字符序列是不是相等。返回值是int 判断是否等于0.
std::string str1 ("green apple");
std::string str2 ("red apple");
if (str1.compare(str2) != 0)
std::cout << str1 << " is not " << str2 << '\n';
if (str1.compare(6,5,"apple") == 0)
std::cout << "still, " << str1 << " is an apple\n";
if (str2.compare(str2.size()-5,5,"apple") == 0)
std::cout << "and " << str2 << " is also an apple\n";
if (str1.compare(6,5,str2,4,5) == 0)
std::cout << "therefore, both are apples\n";
非成员函数:
operator+
std::string firstlevel ("com");
std::string secondlevel ("cplusplus");
std::string scheme ("http://");
std::string hostname;
std::string url;
hostname = "www." + secondlevel + '.' + firstlevel;
url = scheme + hostname;
== != < <= > >=
std::string foo = "alpha";
std::string bar = "beta";
if (foo==bar) std::cout << "foo and bar are equal\n";
if (foo!=bar) std::cout << "foo and bar are not equal\n";
if (foo< bar) std::cout << "foo is less than bar\n";
if (foo> bar) std::cout << "foo is greater than bar\n";
if (foo<=bar) std::cout << "foo is less than or equal to bar\n";
if (foo>=bar) std::cout << "foo is greater than or equal to bar\n";
swap 交换
swap(str1, str2);
<< >> 输入输出重载
getline 注意区分cin.get(name, 20) 和cin.getline(name, 20)丢弃换行符。
std::string name;
std::cout << "Please, enter your full name: ";
std::getline (std::cin,name);
std::cout << "Hello, " << name << "!\n";