Nick Dong Nick Dong - 1 year ago 70
C++ Question

how to reuse istringstream in cplusplus

I want to reuse istringstream variable. It is easy to initial istringstream variable using construction function. But when I try to re-assign it to a new value using

, I got error. I am using c++11. It seems no compile error in vs 2015 using
. But gcc 4.8.2 (devtools-2 on Centos 6.4 x86_64) get error.

Codes like below:

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);

configuration file should be:



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);

Solutions (like how-to-initialize-a-stdstringstream) on stackoverflow about reusing stringstream not work for me. Any ideas about reusing istringstream? Thanks for your consideration.

Answer Source