D
DaLoverhino
I'm trying to get use to using stringstream and I've run across a case
that puzzles me. Perhaps you have some suggestion for the following:
Why in the code below for case 2, intVal is always 1?
// StringToInt.cc
//
//
#include <sstream>
#include <string>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::stringstream;
int main( int argc, char *argv[]) {
vector< std::string > v;
int i0,i1,i2;
v.push_back( "1");
v.push_back( "2");
v.push_back( "3");
{
// This works.
stringstream ss;
ss << v[0] << " ";
ss << v[1] << " ";
ss << v[2] << " ";
ss >> i0 >> i1 >> i2;
cout << i0 << ", " << i1 << ", " << i2 << endl;
}
{
cout << "Case 2" << endl << endl;
stringstream ss;
bool needsComma = false;
int intVal = 0;
for( vector< string >::const_iterator itr = v.begin(); itr !=
v.end(); ++itr) {
ss << *itr;
ss >> intVal;
cout << intVal << endl;
ss.flush();
}
}
return 0;
}
that puzzles me. Perhaps you have some suggestion for the following:
Why in the code below for case 2, intVal is always 1?
// StringToInt.cc
//
//
#include <sstream>
#include <string>
#include <iostream>
#include <vector>
using std::cout;
using std::endl;
using std::vector;
using std::string;
using std::stringstream;
int main( int argc, char *argv[]) {
vector< std::string > v;
int i0,i1,i2;
v.push_back( "1");
v.push_back( "2");
v.push_back( "3");
{
// This works.
stringstream ss;
ss << v[0] << " ";
ss << v[1] << " ";
ss << v[2] << " ";
ss >> i0 >> i1 >> i2;
cout << i0 << ", " << i1 << ", " << i2 << endl;
}
{
cout << "Case 2" << endl << endl;
stringstream ss;
bool needsComma = false;
int intVal = 0;
for( vector< string >::const_iterator itr = v.begin(); itr !=
v.end(); ++itr) {
ss << *itr;
ss >> intVal;
cout << intVal << endl;
ss.flush();
}
}
return 0;
}