Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2012-03-15
8
Medium Priority
?
258 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

722 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