Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 631567
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:58:47+00:00 2026-05-13T19:58:47+00:00

I am trying to conert a vector into a stream and back. While the

  • 0

I am trying to conert a vector into a stream and back.
While the first part works without issues, the last line of the following code put std::ios_base is an error state. Have you got any idea why it is so?
Apparently myVecOut is equal to myVec after the code executes….

    std::vector<double> myVec( 3 );
    std::stringstream temp;
    std::copy(myVec.begin(), myVec.end(), std::ostream_iterator<double>(temp, " "));
    std::cout << temp.str() << std::endl;
    std::vector<double> myVecOut;
    std::copy(std::istream_iterator<double>(temp), std::istream_iterator<double>(), std::back_inserter(myVecOut));
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T19:58:47+00:00Added an answer on May 13, 2026 at 7:58 pm

    It’s set into fail state, because copy does not know how many items to read. It reads as long as the stream is in a .good() non-.fail() state. While skipping the last space, it hits the end, and sets both eof (because it tried to read beyond the end) and fail (because it could not read the double it wanted to).

    Call .clear() afterwards to clear those error bits.


    The difference between the iterators comparing non-equal while .good() is true and while .fail() is false is that istream_iterator will still advance even if the stream is in an eof state. The following further examines it:

    std::stringstream a;
    a << "1.1 2.2";
    std::copy(std::istream_iterator<double>(a), std::istream_iterator<double>(),
              std::ostream_iterator<double>(std::cout, " ")); 
    

    Now, after we read “2.2”, the eof state is set (because it tried to read beyond “2”). But the begin and end iterator won’t yet compare equal, because the fail state is not set, and thus .fail() does not return true (while .good() would have returned false, because it considers .eof() in addition). Only after the next read, when it could not read another double anymore, the fail state is set, and then the iterators compare equal, and the loop within std::copy exits.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 304k
  • Answers 304k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I would start by grabbing the nhibernate source, the 2.1.x… May 13, 2026 at 8:56 pm
  • Editorial Team
    Editorial Team added an answer You buy RegexBuddy and use its built in debug feature.… May 13, 2026 at 8:56 pm
  • Editorial Team
    Editorial Team added an answer If MY_TAG can not be nested, try this to get… May 13, 2026 at 8:56 pm

Related Questions

I'm in a bit of a fiddle in that I don't know why my
I'm trying to generate a vector graphic from an area in a bitmap image,
I have a class that encrypts and decrypts a string. It uses rijndael .
I am working with a survey dataset. It has two string vectors , start
I am currently struggling with a side project I hope you can help me

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.