STL::string

Iterators

begin:

end:

rbegin:

rend:

cbegin:

cend:

crbegin:

crend:

Capacity

size:

length:

max_size:

resize:

capacity: 返回实际分配的存储空间的大小,一般大于等于 size 。这样能优化插入等需要重新分配存储空间的操作。

reserve:

clear:

empty:

shrink_to_fit(c++11):

Element access

operator [ ]:

at:

back(c++11):

front(c++11):

Modifiers

assign:

operator +=:

append:

push_back:

insert:

erase:

replace:

swap:

pop_back(c++11):

String operations

c_str:

 1 // strings and c-strings
 2 #include <iostream>
 3 #include <cstring>
 4 #include <string>
 5 
 6 int main ()
 7 {
 8   std::string str ("Please split this sentence into tokens");
 9 
10   char * cstr = new char [str.length()+1];
11   std::strcpy (cstr, str.c_str());
12 
13   // cstr now contains a c-string copy of str
14 
15   char * p = std::strtok (cstr," ");
16   while (p!=0)
17   {
18     std::cout << p << '\n';
19     p = std::strtok(NULL," ");
20   }
21 
22   delete[] cstr;
23   return 0;
24 }
View Code

data: 返回指向一个字符串的指针。 类似于c_str 中的 字符串首指针。copy:   复制一个子串到一个数组中去,但是这个函数并不会在复制的字符串的最后面加上一个 ‘\0’ 字符。

 1 // string::copy
 2 #include <iostream>
 3 #include <string>
 4 
 5 int main ()
 6 {
 7   char buffer[20];
 8   std::string str ("Test string...");
 9   std::size_t length = str.copy(buffer,6,5);
10   buffer[length]='\0'; //note this point
11   std::cout << "buffer contains: " << buffer << '\n';
12   return 0;
13 }
View Code

find: 找到第一个完全匹配的字符串,返回模式串中匹配子串的第一个元素的位置,如果失配,则返回 std::string::npos(-1)。

rfind: 和上面的功能一样,不同点是从又往左查找。

find_first_of: 单个字符匹配,返回匹配的位置。如若匹配不到,返回 -1;

find_last_of: 功能同上,不同点是从后往前找的。

find_first_not_of: 

find_last_not_of:

substr: 返回一个字符串对象,该对象的内容是原字符串匹配的子串的拷贝。string substr (size_t pos=0,size_t len = npos) const。

compare:

猜你喜欢

转载自www.cnblogs.com/zpcoding/p/10344517.html