Recently I've been working on writing snippets for my finals. One of the common task is to divide a string (std::string) into words. In some cases these strings can contain integers.
I wrote a snippet:
#include <sstream>
#include <iostream>
#include <vector>
using namespace std;
int main(void)
{
string str="23 005 123";
vector<int>myints;
istringstream iss(str);
string word;
while(iss>>word)
{
int a;
istringstream(word)>>a;
myints.push_back(a);
}
for (vector<int>::iterator it=myints.begin();it!=myints.end();++it)
cout<<*it<<" ";
}
It works, although there's a problem. I get 5 from str instead of 005. It seems that VC++ shrinks all the zeroes. How can avoid it using only C++ functions (not strtok from string.h/cstring)?
I get it both on MS VC++2008 and gcc.
Thanks!