什么是从std::basic_istream
中提取特定数量字符并将其存储在std::string
中的好方法?从std :: basic_istream复制特定数量的字符到std :: string
在下面的程序中,我使用char[]
最终获得result
,但我想,以避免POD类型和保证的东西更安全,更易于维护:
#include <sstream>
#include <string>
#include <iostream>
#include <exception>
int main()
{
std::stringstream inss{std::string{R"(some/path/to/a/file/is/stored/in/50/chars Other data starts here.)"}};
char arr[50]{};
if (!inss.read(arr,50))
throw std::runtime_error("Could not read enough characters.\n");
//std::string result{arr}; // Will probably copy past the end of arr
std::string result{arr,arr+50};
std::cout << "Path is: " << result << '\n';
std::cout << "stringstream still has: " << inss.str() << '\n';
return 0;
}
替代方案:
- 转换整个流向前一个字符串:
std::string{inss.c_str()}
- 这似乎是浪费,因为它无线ld制作整个流的副本。
- 写模板函数接受
char[]
- 这仍然使用中间POD阵列。
- 使用
std::basic_istream::get
在循环中读取所需的字符数与std::basic_string::push_back
- 循环似乎有点笨拙起来,但它避免了阵列。
可能的重复:[如何从字符串流中取出字符而不进行复制?](http://stackoverflow.com/questions/28663075/how-to-get-characters-out -of-stringstream-without-copy) – NathanOliver
你保证文件名是50个字符或更少? –
@QPaysTaxes文件格式具有分配给文件名的前50个字符。所以只要文件格式保持不变,就可以保证。风险在于文件格式发生变化,然后代码必须更改。 – wally