string

参见C++ Reference:http://www.cplusplus.com/reference/vector/vector/?kw=vector

typedef basic_string<char> string;

String class
Strings are objects that represent sequences of characters.
[string类对象就是字符串序列]
The standard string class provides support for such objects with an interface similar to that of a standard container of bytes, but adding features specifically designed to operate with strings of single-byte characters.
[标准的string类提供了对以下对象的支持--该对象拥有一个类似于标准字节容器的接口,同时string类增加了一些特别为操作由单字节字符构成的string而设计的特性]
The string class is an instantiation of the basic_string class template that uses char (i.e., bytes) as its character type, with its default char_traits and allocator types (see basic_string for more info on the template).
[string类是basic_string类模板的一个示例,模板参数为char]
Note that this class handles bytes independently of the encoding used: If used to handle sequences of multi-byte or variable-length characters (such as UTF-8), all members of this class (such as length or size), as well as its iterators, will still operate in terms of bytes (not actual encoded characters).
[注意,string类对于字节的操作是独立于所使用的编码的,如果使用string类来操作多字节序列或者变长字符(比如utf-8),则string类的所有成员(比如length()或者size())及其迭代器都仍然是以字节为单位进行处理,而不是以实际的编码字符]

/*
//construct string
string();
string(const string& str);
string(const string& str, size_t pos, size_t len = npos);
string(const char* s); //Copies the null-terminated character sequence (C-string) pointed by s.[用带有null作为字符串结束符的字符串数组来构造string]
string(const char* s, size_t n); //Copies the first n characters from the array of characters pointed by s.
string(size_t n, char c);  //Fills the string with n consecutive copies of character c.
string(InputIterator first, InputIterator last);  //Copies the sequence of characters in the range [first,last), in the same order.
*/
 
#include <iostream>
#include <string>

int main()
{
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 ("A character sequence", 6);
std::string s6 (10, 'x');
std::string s7a (10, 42);      // 42 is the ASCII code for '*'
std::string s7b (s0.begin(), s0.begin()+7);

std::cout << "s1: " << s1 << "
s2: " << s2 << "
s3: " << s3;
std::cout << "
s4: " << s4 << "
s5: " << s5 << "
s6: " << s6;
std::cout << "
s7a: " << s7a << "
s7b: " << s7b << '
';

system("pause");
return 0;
}
/*
//copy
size_t copy(char* s, size_t len, size_t pos = 0) const

Copy sequence of characters from string
Copies a substring of the current value of the string object into the array pointed by s. This substring contains the len characters that start at position pos.
[复制string的一个子串(从pos开始的len个字符)给s指向的数组]
The function does not append a null character at the end of the copied content.
[该函数不会自动添加null作为字符串结束符]
*/

#include <iostream>
#include <string>

int main ()
{
char buffer[20];
std::string str ("Test string...");
std::size_t length = str.copy(buffer,6,5);
buffer[length]='';
std::cout << "buffer contains: " << buffer << '
';

system("pause");
return 0;
}
/*
//reserve
void reserve(size_t n = 0);

Request a change in capacity
Requests that the string capacity be adapted to a planned change in size to a length of up to n characters.
[请求改变string容量为n]
If n is greater than the current string capacity, the function causes the container to increase its capacity to n characters (or greater).
[如果n比当前容量大,则该函数会增加容量为n或者更大]
In all other cases, it is taken as a non-binding request to shrink the string capacity: the container implementation is free to optimize otherwise and leave the string with a capacity greater than n.
[其他情况下,该请求被视为一个减小string容量的非绑定请求:由容器的实现决定或者保持容量不变]
This function has no effect on the string length and cannot alter its content.
[该函数不会影响string的长度及其数据]
*/

#include <iostream>
#include <string>
using namespace std;

int main ()
{
string s = "123";
cout<<s.capacity()<<endl;        //15
s.reserve(20);
cout<<s.capacity()<<endl;        //31
s.reserve(1);
cout<<s.capacity()<<endl;        //31

system("pause");
return 0;
}
/*
//find
size_t find (const string& str, size_t pos = 0) const;
size_t find (const char* s, size_t pos = 0) const;
size_t find (const char* s, size_t pos, size_t n) const;        //n means "Length of sequence of characters to match".
size_t find (char c, size_t pos = 0) const;

Find content in string
Searches the string for the first occurrence of the sequence specified by its arguments.
[前向寻找string中第一次与给定字符串全部匹配的位置]
When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences that include characters before pos.
[搜索的起点为pos,忽略pos前的所有字符]

//rfind
size_t rfind (const string& str, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos = npos) const;
size_t rfind (const char* s, size_t pos, size_t n) const;
size_t rfind (char c, size_t pos = npos) const;

Find last occurrence of content in string
Searches the string for the last occurrence of the sequence specified by its arguments.
[反向寻找string中第一次与给定字符串全部匹配的位置]
When pos is specified, the search only includes sequences of characters that begin at or before position pos, ignoring any possible match beginning after pos.
[搜索的起点为pos,忽略pos后的所有字符]
*/

#include <iostream>
#include <string>
#include <cstddef>

int main ()
{
std::string str ("The sixth sick sheik's sixth sheep's sick.");
std::string key ("sixth");

std::size_t found = str.rfind(key);
if (found!=std::string::npos)
str.replace (found,key.length(),"seventh");

std::cout << str << '
';

system("pause");
return 0;
}
/*
//find_first_not_of
size_t find_first_not_of (const string& str, size_t pos = 0) const;
size_t find_first_not_of (const char* s, size_t pos = 0) const;
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
size_t find_first_not_of (char c, size_t pos = 0) const;

Find absence of character in string
Searches the string for the first character that does not match any of the characters specified in its arguments.
[寻找string中第一个不与给定字符串中的任意字符相匹配的位置]
When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences before that character.
[搜索的起点为pos,忽略pos前的所有字符]


//find_first_of
size_t find_first_of (const string& str, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos = 0) const;
size_t find_first_of (const char* s, size_t pos, size_t n) const;
size_t find_first_of (char c, size_t pos = 0) const;

Find character in string
Searches the string for the first character that matches any of the characters specified in its arguments.
[寻找string中第一个与给定字符串中任意字符相匹配的位置]
When pos is specified, the search only includes characters at or after position pos, ignoring any possible occurrences before pos.
[搜索的起点为pos,忽略pos前的所有字符]
*/

#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

int main ()
{
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);
}

std::cout << str << '
';

system("pause");
return 0;
}
/*
//find_last_not_of
size_t find_last_not_of (const string& str, size_t pos = npos) const;
size_t find_last_not_of (const char* s, size_t pos = npos) const;
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
size_t find_last_not_of (char c, size_t pos = npos) const;

Find non-matching character in string from the end
Searches the string for the last character that does not match any of the characters specified in its arguments.
[反向寻找string中第一个不与给定字符串中任意字符相匹配的位置]
When pos is specified, the search only includes characters at or before position pos, ignoring any possible occurrences after pos.
[搜索的起点为pos,忽略pos后的所有字符]

//find_last_of
size_t find_last_of (const string& str, size_t pos = npos) const;
size_t find_last_of (const char* s, size_t pos = npos) const;
size_t find_last_of (const char* s, size_t pos, size_t n) const;
size_t find_last_of (char c, size_t pos = npos) const;

Find character in string from the end
Searches the string for the last character that matches any of the characters specified in its arguments.
[反向寻找string中第一个与给定字符串中任意字符相匹配的位置]
When pos is specified, the search only includes characters at or before position pos, ignoring any possible occurrences after pos.
[搜索的起点为pos,忽略pos后的所有字符]
*/

#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t

void SplitFilename (const std::string& str)
{
std::cout << "Splitting: " << str << '
';
unsigned found = str.find_last_of("/\");
std::cout << " path: " << str.substr(0,found) << '
';
std::cout << " file: " << str.substr(found+1) << '
';
}

int main ()
{
//find_last_not_of
std::string str ("Please, erase trailing white-spaces         
");
std::string whitespaces (" 	fv

");

std::size_t found = str.find_last_not_of(whitespaces);
if (found!=std::string::npos)
str.erase(found+1);
else
str.clear();            // str is all whitespace

std::cout << '[' << str << "]
";

//find_last_of
std::string str1 ("/usr/bin/man");
std::string str2 ("c:\windows\winhelp.exe");

SplitFilename (str1);
SplitFilename (str2);

system("pause");
return 0;
}
/*
ostream& operator<< (ostream& os, const string& str);
istream& operator>> (istream& is, string& str);        //由于cin会以" 	
"为分隔符,因此如果要读取一行,可用getline

istream& getline (istream& is, string& str, char delim);        //delim默认为'
'
*/
#include <iostream>
#include <string>

int main()
{
std::string name;

std::cout << "Please, enter your full name: ";
std::getline (std::cin,name);
std::cout << "Hello, " << name << "!
";

system("pause");
return 0;
}
/*
void clear();
bool empty();
void swap(string& str);
size_t length();             //同size_t size();
char& at(size_t pos);        //同operator[]


iterator begin();            //iterator is random access iterator type
iterator end();
reverse_iterator rbegin();
reverse_iterator rend();

//append 
string& append (const string& str);
string& append (const string& str, size_t subpos, size_t sublen);
string& append (const char* s);
string& append (const char* s, size_t n);
string& append (size_t n, char c);
string& append (InputIterator first, InputIterator last);

//operator+= -- 同append
string& operator+= (const string& str);
string& operator+= (const char* s);
string& operator+= (char c);

//assign
string& assign (const string& str);
string& assign (const string& str, size_t subpos, size_t sublen);
string& assign (const char* s);
string& assign (const char* s, size_t n);
string& assign (size_t n, char c);
string& assign (InputIterator first, InputIterator last);

//operator= -- 同assign
string& operator= (const string& str);
string& operator= (const char* s);
string& operator= (char c);

//operator+
string operator+ (const string& lhs, const string& rhs);
string operator+ (const string& lhs, const char* rhs);
string operator+ (const char* lhs, const string& rhs);
string operator+ (const string& lhs, char rhs);
string operator+ (char lhs, const string& rhs);

//erase
string& erase (size_t pos = 0, size_t len = npos);
iterator erase (const_iterator p);
iterator erase (const_iterator first, const_iterator last);

//insert
string& insert (size_t pos, const string& str);
string& insert (size_t pos, const string& str, size_t subpos, size_t sublen);
string& insert (size_t pos, const char* s);
string& insert (size_t pos, const char* s, size_t n);
string& insert (size_t pos, size_t n, char c);
iterator insert (const_iterator p, size_t n, char c);
iterator insert (const_iterator p, char c);
iterator insert (iterator p, InputIterator first, InputIterator last);

Insert into string
Inserts additional characters into the string right before the character indicated by pos (or p)
[在pos之前插入字符]

//replace
string& replace (size_t pos,  size_t len,  const string& str);
string& replace (size_t pos,  size_t len,  const string& str, size_t subpos, size_t sublen);
string& replace (size_t pos,  size_t len,  const char* s);
string& replace (size_t pos,  size_t len,  const char* s, size_t n);
string& replace (size_t pos,  size_t len,  size_t n, char c);

string& replace (iterator i1, iterator i2, const string& str);
string& replace (iterator i1, iterator i2, const char* s);
string& replace (iterator i1, iterator i2, const char* s, size_t n);
string& replace (iterator i1, iterator i2, size_t n, char c);
string& replace (iterator i1, iterator i2, InputIterator first, InputIterator last);

//compare
int compare (const string& str) const;
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
int compare (const char* s) const;
int compare (size_t pos, size_t len, const char* s) const;
int compare (size_t pos, size_t len, const char* s, size_t n) const;

//substr
string substr(size_t pos = 0, size_t len = npos) const;

//c_str
const char* c_str() const;        //同const char* data() const;

Get C string equivalent
Returns a pointer to an array that contains a null-terminated sequence of characters (i.e., a C-string) representing the current value of the string object.
[返回string对应的C语言格式的字符串数组]
This array includes the same sequence of characters that make up the value of the string object plus an additional terminating null-character ('') at the end.
[该函数会自动添加一个null作为字符串结束符]

//capacity
size_t capacity() const;

Return size of allocated storage
Returns the size of the storage space currently allocated for the string, expressed in terms of bytes.
[返回当前为string分配的内存空间,单位为字节]
This capacity is not necessarily equal to the string length. It can be equal or greater, with the extra space allowing the object to optimize its operations when new characters are added to the string.
[capacity应该大于等于string的长度,这样当新的字符被添加到string中时,额外的内存空间可以优化操作]
Notice that this capacity does not suppose a limit on the length of the string. When this capacity is exhausted and more is needed, it is automatically expanded by the object (reallocating it storage space). The theoretical limit on the length of a string is given by member max_size.
[capacity不会限制string的长度,当前分配的内存空间耗尽时,string会再分配来自动增加长度]
The capacity of a string can be altered any time the object is modified, even if this modification implies a reduction in size or if the capacity has not been exhausted (this is in contrast with the guarantees given to capacity in vector containers).
[与vector不同的是,只要string类对象被修改(无论其长度是增加还是减小),capacity就可能改变]

注:
//string::npos
static const size_t npos = -1;

Maximum value for size_t
npos is a static member constant value with the greatest possible value for an element of type size_t.
[npos是string类的一个静态常量成员,其值为-1,代表string类元素类型size_t的最大可能取值]
This value, when used as the value for a len (or sublen) parameter in string's member functions, means "until the end of the string".
[当npos作为string类成员函数中len或者sublen的参数时,代表"直到string末尾"]
As a return value, it is usually used to indicate no matches.
[当npos作为返回值,则经常代表没有匹配]
This constant is defined with a value of -1, which because size_t is an unsigned integral type, it is the largest possible representable value for this type.
[该常量之所以是-1的原因在于size_t是一个无符号整型,-1代表了无符号整型的最大可能取值]
*/
原文地址:https://www.cnblogs.com/kevinq/p/4531889.html