如何在cplusplus中重用istringstream
how to reuse istringstream in cplusplus
本文关键字:istringstream cplusplus 更新时间:2023-10-16
我想重用istringstream变量。使用构造函数初始化istringstream变量很容易。但是当我尝试使用=
或<<
将其重新分配给新值时,我得到了错误。我正在使用c++11。在vs2015中使用=
似乎没有编译错误。但是gcc 4.8.2(在Centos 6.4 x86_64上的devtools-2)得到错误。
代码如下:
std::string line;
int simmulationtimes; double InitialIDensity;
// deploy configuration file
std::ifstream config(configfile);
if (!config.is_open())
{
return false;
}
std::getline(config, line);
std::istringstream sline(line);
std::string sInitialIDensity;
while(std::getline(sline, sInitialIDensity, '='));
InitialIDensity = std::stod(sInitialIDensity);
std::getline(config, line);
std::string ssimmulationtimes;
sline.str(""); sline.clear();
sline = std::istringstream(line);
while (std::getline(sline, ssimmulationtimes, '='));
simmulationtimes = std::stoi(ssimmulationtimes);
配置文件应该是:
IDensity=0.5
times=5
错误:error: no match for ‘operator=’ (operand types are ‘std::istringstream {aka std::basic_istringstream<char>}’ and ‘std::string {aka std::basic_string<char>}’)
sline = std::istringstream(line);
解决方案(如如何初始化-a-stdstringstream)在stackoverflow上重用stringstream不适合我。关于重用istringstream有什么想法吗?谢谢您的考虑。
使用str()
方法将新的std::string
设置为现有的std::istringstream
。
相关文章:
- 如何使用iStringstream读取和解析文本文件?
- istringstream >>运算符产生意外行为
- 在C++中使用istringstream读取输入时出错
- istringstream,num1 和 num2 在这里发生了什么?
- 来自 istringstream 的五倍输出
- 是否可以在不指定类型的情况下从 istringstream 转到向量?
- 使用 string_view 构造 istringstream 无法编译
- 有没有办法我可以从 istringstream 中读取两个字符
- istringstream将字符串放回输入并再次读取
- 为什么 istringstream 在流的末尾附加'-1'字符?
- 字符串向量到istringstream
- istringstream-function string_to_int不能接受C-STR,为什么
- 基于循环的C ISTRINGSTREAM范围没有开始成员
- 无法使用 istringstream 从.txt文件中读取
- 为什么当我将 std::locale 设置为 "zh_CN.UTF-8" 时 std::istringstream 失败?
- 修改 iStringStream 中的变量数
- NDK r16b std::istringstream 的行为不符合预期
- 如何在 c++ 中使用 istringstream
- 有没有办法重写一个返回指向 istringstream 的指针的函数,而不是返回一个引用?
- 这一行是怎么回事: istringstream is( line );