Solved

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

Posted on 2012-03-15
8
254 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add values of each row in an array 3 81
Outlook 13 102
Coding for the first time 9 103
print bytes of an integer 6 48
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
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.

752 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