Solved

ostringstream - not working??  What's wrong with my code?

Posted on 2012-03-15
8
249 Views
Last Modified: 2012-03-16
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!

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

Open in new window

0
Comment
Question by:thready
8 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 37727462
If you print VEC.size(), what do you get ?

How do you output the result ?
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 37727642
What do you get if you use

std::vector<std::string> Result
vector<float> VEC = GetVectorOfFloatsThatGives100Items();

ostringstream oss;
oss.clear();
for(vector<float>::const_iterator i = VEC.begin(); i != VEC.end(); i++)
{
	oss << *i;
	if(i + 1 != VEC.end())
		oss << "|";
}
oss.flush();
Result.push_back(oss.str());
                                  

Open in new window


instead?
0
 
LVL 1

Author Comment

by:thready
ID: 37727874
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?  :)
0
 
LVL 86

Expert Comment

by:jkr
ID: 37727890
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.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 1

Author Comment

by:thready
ID: 37727903
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... :)
0
 
LVL 1

Author Closing Comment

by:thready
ID: 37727904
definitely feels better.  gotta admit
0
 
LVL 86

Expert Comment

by:jkr
ID: 37727928
Well, if it also works ;o)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 37731899
std::vector<std::string> Result
vector<float> VEC = GetVectorOfFloatsThatGives100Items();

ostringstream oss;
std::copy(VEC.begin(), VEC.end(), ostream_iterator<float>(oss, "|"))
Result.push_back(oss.str());
Result.back().resize(Result.back().size()-1); // remove training "|"

http://www.cplusplus.com/reference/std/iterator/ostream_iterator/
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now