1.3 string insert, replace, erase 了解了string 的操作符,查找函数和substr,其实就已经了解了string的80%的操作了。insert函数, replace函数和erase函数在使用起来相对简单。下面以一个例子来说明其应用。 string只是提供了按照位置和区间的replace函数,而不能用一个string字串来替换指定
string(“hello”) + string(“world”) == string(“helloworld”) string 符合 vector 的接口,例如 begin/end/size/resize…… string 有一系列成员函数,例如 find/replace/substr…… string 可以通过 s.c_str() 重新转换回古板的 const char *。 string 在离开作用域时自动释放内存 (RAII),不用手动 free。
typedef basic_string<char>string; 字符串是表示字符序列的对象。 标准string类使用类似于字节标准容器的接口提供对此类对象的支持,但是添加了专门用于操作单字节字符(single-byte characters)的字符串的特性。 string类是basic_string类模板的实例化,该模板使用char作为其字符类型,并具有默认的char_traits和allocator类型。
<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 ("Another character sequence", 12);
replaces or obtains a copy of the associated character string (public member function of std::basic_stringbuf) 代码语言:txt 复制 © cppreference.com 在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。 http://en.cppreference.com/w/cpp/io/basic[医]弦流/str ...
As a reminder... One of my low-priority todo items is to replace character arrays with std::string to help ensure that there are no overflows we have overlooked. BulletCatcher
basic_string::replacestd::basic_string::replace_with_rangestd::basic_string::reservestd::basic_string::resizestd::basic_string::resize_and_overwritestd::basic_string::rfindstd::basic_string::shrink_to_fitstd::basic_string::sizestd::basic_string::starts_withstd::basic_string::substrstd::...
s.erase( 0, 1 ); // erase the first character s.erase( s.size() - 1 ); // erase the last character } 复杂性在字符串的大小上仍然是线性的。您不能在 O(1) 时间内从std::string的开头插入或删除。如果用空格替换字符是可以接受的,那么就这样做。
Expand or shrink the capacity of the string Element access [], at()Accesses the character at a particular index Modification =, assign() +=, append(), push_back() insert() clear() erase() replace() resize() swap()Assigns a new value to the string ...
opens a file and configures it as the associated character sequence (public member function of std::basic_filebuf) set_rdbuf replaces the rdbuf without clearing its error state (protected member function) (constructor) constructs the object (public member function of std::basic_ostream) ...