Solved

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

Posted on 2012-03-15
8
250 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
memory leak detection 9 71
Quicksort a dynamic deque 33 66
Cross compile release version of c++ program for linux 2 131
Add values of each row in an array 3 50
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

863 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

24 Experts available now in Live!

Get 1:1 Help Now