thready
asked on
ostringstream - not working?? What's wrong with my code?
Hi Experts,
I have the code below. If I have 100 items in my vector, I'm only getting back 29 of them when i look at the result of oss.str().... is there something wrong with this code? Thank you!
I have the code below. If I have 100 items in my vector, I'm only getting back 29 of them when i look at the result of oss.str().... is there something wrong with this code? Thank you!
std::vector<std::string> Result
vector<float> VEC = GetVectorOfFloatsThatGives100Items();
ostringstream oss;
oss.clear();
for(unsigned int i = 0; i < VEC.size(); i++)
{
oss << VEC[i];
if(i + 1 < VEC.size())
oss << "|";
}
oss.flush();
Result.push_back(oss.str());
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
haha, it's more typing. but you're definitely about to tell me a very wise and experienced answer of why it's better to use the iterator. Aren't you jkr? :)
I don't really have a wiser explanation ready other than it's the way that 'feels' better - it's a habit for me to access STL containers via their iterators, simply because it is the most common way to do that for all containers. Yet I was curuois if that made a difference or yielded the same results.
ASKER
lol. it feels better? :). You've taught me a lot jkr. I'm almost disappointed! I was sure you were gonna come up with some very subtle and interesting scenario where with unicode or some com container opting to wrap my code in a cloud over a quantumly encrypted connection.... ok maybe I'm exaggerating a bit... but you know what I mean... :)
ASKER
definitely feels better. gotta admit
Well, if it also works ;o)
std::vector<std::string> Result
vector<float> VEC = GetVectorOfFloatsThatGives 100Items() ;
ostringstream oss;
std::copy(VEC.begin(), VEC.end(), ostream_iterator<float>(os s, "|"))
Result.push_back(oss.str() );
Result.back().resize(Resul t.back().s ize()-1); // remove training "|"
http://www.cplusplus.com/reference/std/iterator/ostream_iterator/
vector<float> VEC = GetVectorOfFloatsThatGives
ostringstream oss;
std::copy(VEC.begin(), VEC.end(), ostream_iterator<float>(os
Result.push_back(oss.str()
Result.back().resize(Resul
http://www.cplusplus.com/reference/std/iterator/ostream_iterator/
How do you output the result ?