size_t copy ( char* s, size_t n, size_t pos = 0) const;
Copies a sequence of characters from the string content to the array pointed bys. This sequence of characters is made of the characters in the string that start at character positionpos and spann characters from there.
The function does not append a null character after the content copied. To retrieve a temporary c-string value from a string object, a specific member function exists:c_str.
将原字符串内容从Pos位置开始的n个字符复制能拷贝给字符数组s。
举例:
string str1 = "Hello World";
char ch1[20] = "";
str1.copy(ch1, 5, 6);
cout<<ch1<<endl;
//打印结果为 World
const char* data() const;
Get string data
Returns a pointer to an array of characters with the same content as the string.
Notice that no terminating null character is appended (see member c_str for such a functionality).
The returned array points to an internal location which should not be modified directly in the program. Its contents are guaranteed to remain unchanged only until the next call to a non-constant member function of the string object.
用法同c_str()方法
举例:
string str4 = "Test string";
const char * cstr1 = str4.c_str();
const char * cstr2 = str4.data();
if((str4.compare(cstr1)==0)&&(str4.compare(cstr2)==0))
cout<<"both strings have the same content."<<endl;
//打印结果为 both strings have the same content.
bool empty ( ) const;
Returns whether the string is empty, i.e. whether itssize is0.
This function does not modify the content of the string in any way. To clear the content of the string, memberclear can be used.
如果字符串为空,则返回true,否则返回false
举例:
string str4 = "Test string";
str4.clear();
if(str4.empty())
cout<<"empty"<<endl;
//打印结果为empty
iterator end();const_iterator end() const;
Returns an iterator referring to the next element after the last character in the string.
返回字符串的结尾地址。类似begin()方法返回的是字符串的首地址。