- std::ifstream读整个文件的方法:
std::string file_name = "..."; // your file name
std::ifstream ifs(file_name, std::ios::in);
if( !ifs.good() )
{
std::cout << __FILE__ << __LINE__ << " error: file_name = " << file_name << std::endl;
return;
}
// 0 17ms
std::string content;
std::getline(ifs, content, '\0');
ifs.close();
// 1 18ms
// std::stringstream buffer;
// buffer << ifs.rdbuf();
// std::string content = buffer.str();
// ifs.close();
// 2 13ms
// std::string content((std::istreambuf_iterator<char>(ifs)), std::istreambuf_iterator<char>());
// ifs.close();
- std::string
2.1 替换字符串的方法:
void
CommonUtil::replace_string(std::string& str_content, const std::string& str_src, const std::string& str_dst)
{
std::string::size_type pos = 0;
std::string::size_type srclen = str_src.size();
std::string::size_type dstlen = str_dst.size();
while( (pos = str_content.find(str_src, pos)) != std::string::npos )
{
str_content.replace( pos, srclen, str_dst );
pos += dstlen;
}
}
- std::filesystem
3.1 std::filesystem::path crash at ~析构
添加
LIBS += -lstdc++fs