C++ string特性

std::string::push_back
void push_back (char c);
Append character to string
把char加到string的後面(C++String和Char由於兼容(C語言)問題,並不想其他語言一樣字符和字符串相統一)
std::string::append
Append to string
Extends the string by appending additional characters at the end of its current value:
把string加到string的後面

#include<string>
using std::string
string S;
S = "abcd";
S.append("e");
S.push_back('f');

std::string::assign
Assign content to string
Assigns a new value to the string, replacing its current contents.

#include<string>
std::string S("1234");//S:"1234"
S.assign("hahah999");//S:"hahah999"
S.assigh("hahah999", 4, 2);//S:"h9"

std::string::insert
Insert into string
Inserts additional characters into the string right before the character indicated by pos (or p):
string::insert()對string和char均適用

#include<string>
std::string S("123456");
S.insert(3, "hahah");//S:"123hahah456"

std::string::erase
Erase characters from string
Erases part of the string, reducing its length:

#include<string>
String S("123456");
S.erase();//S.empty: True
S.append("abcd");
S.erase(1,2);//S: "ad"

std::string::replace
Replace portion of string
Replaces the portion of the string that begins at character pos and spans len characters (or the part of the string in the range between [i1,i2)) by new contents:

#include<string>
std::string S("123456");
S.replace(1,2,"abcd", 2);//string
S.replace(1,2,,2,'e')//char

std::string::replace
Another string object, whose value is swapped with that of this string.
並不是很常用

#include<string>
std::string S("123456");
S.swap("7890");//S = "7890";

C++11std::string::pop_back()
Delete last character
Erases the last character of the string, effectively reducing its length by one.
爲了std中vector等用法統一

#include<string>
std::string S("123456");
S.pop_back();

std::string::size
std::string::length
std::string::max_size
std::string::resize
std::string::capacity
std::string::reserve
std::string::clear
std::string::empty
C++11 std::string::shrink_to_fit:
Requests the string to reduce its capacity to fit its size.

The request is non-binding, and the container implementation is free to optimize otherwise and leave the string with a capacity greater than its size.

This function has no effect on the string length and cannot alter its content.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章