Solved

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

Posted on 2012-03-15
8
253 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
Industry Leaders: 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

Technology Partners: 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
c++, dynamic object by json 1 60
How to Correctly derive class from CWinThread in MFC 4 89
Adding items to a C# list incrementally 5 58
C qsort compare function issue 6 36
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
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…

740 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