STL string用法汇总

#include <string>

find

std::size_t start_pos = str.find(from);
if(start_pos == std::string::npos)
return false;

str.find_last_of(from);

replace

str.replace(“stringa”, “stringb”);

std::replace(str.begin(), str.end(), ‘a’, ‘b’);  // #include <algorithm>

std::stringstream && std::istringstream && std::ostringstream // #include <sstream>

stringstream is both an ostringstream and an istringstream: you can << and >> both ways, in and out.

With istringstream, you can only go out with >>, and you cannot go in with <<.

With ostringstream, you can only go in with <<, and you cannot go out with >>.

std::stringstream

std::stringstream stream;

stream << “string” << std::string(“string”) << int << double << std::endl;

std::stringstream to std::string

std::string str = stream.str();

std::stringstream clear

stream.str(std::string()) // clear the stream. The clear() member function is inherited from ios and is used to clear the error state of the stream, e.g. if a file stream has the error state set to eofbit (end-of-file), then calling clear() will set the error state back to goodbit (no error).

Add a Comment

电子邮件地址不会被公开。 必填项已用*标注

12 − 5 =