参见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]='