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

  • SEARCH
  • Home
  • 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 6597323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:11:41+00:00 2026-05-25T18:11:41+00:00

I am new to C++, sorry if this is a silly question. I cannot

  • 0

I am new to C++, sorry if this is a silly question. I cannot seem to figure out why this does not work. It copies into the first vector, and seems to skip past the second copy call.

#include <iostream>
#include <vector>
#include <iterator>

using namespace std;

int main ()
{
    vector<int> first;
    vector<int> second;

    copy(istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(first));
    cin.clear();
    copy(istream_iterator<int>(cin),istream_iterator<int>(),back_inserter(second)); 
    return 0;
}

I want to use the copy function to read istream_iterator input into any number of vectors(one call to copy per vector). In other words: I want to be able to enter “1 2 3 4 5 ctrl+d” into the console and have 1,2,3,4,5 entered into the first vector. Then enter “6 7 8 9 10 ctrl+d” into the console and have 6,7,8,9,10 entered into the second vector.

The problem is that after I enter some input into the first vector and press control+d the istream_iterator for cin remains equal to istream_iterator(), regardless of cin’s fail state. This causes every subsequent call to “copy” to fail (because istream_iteratorcin is already equal to istream_iterator() which the program interprets as eof).
So my question is: What do I need to do to “reset” the iterator along with the cin stream? cin.clear() is indeed clearing all the fail bits. However the istream_iterator(cin) is still equal to istream_iterator() regardless. From what I understand, istream_iterators that are bound to a stream should only be equal to the default istream_iterator value when the stream is in a fail state. What am I missing?

  • 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-25T18:11:41+00:00Added an answer on May 25, 2026 at 6:11 pm

    The istream_iterator is an input iterator, which means you can only dereference each iterator value once. You are literally reading from a stream, and there’s no seeking or going back. So once you hit the end-of-stream, there’s nothing more to input and the second range is empty.

    Why not just say vector<int> second(first); to make a copy?


    Update: After you clarified the question, here’s a new answer: You’re misunderstanding how stdin works. There is only one input. Ctrl-D isn’t anything inherent to C++; rather, it is a convention of your platform, and your platform will terminate the input buffer when you signal Ctrl-D. After that, the input “file” is finished, and no further data can be written to it.

    Your approach is a bit unorthodox, though. Usually, you would just read line by line, separated by Enter, and tokenize each line. Using string streams, you get very similar code:

    std::string line;
    std::vector<int> first, second;
    
    // Read line 1
    if (std::getline(std::cin, line))
    {
      std::istringstream iss(line);
      std::copy(std::istream_iterator<int>(iss), std::istream_iterator<int>(), std::back_inserter(first));
    }
    else { /* error */ }
    
    // Read line 2
    if (std::getline(std::cin, line))
    {
      std::istringstream iss(line);
      std::copy(std::istream_iterator<int>(iss), std::istream_iterator<int>(), std::back_inserter(second));
    }
    else { /* error */ }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok sorry this might seem like a dumb question but I cannot figure this
I am new to Microsoft.MVC, so sorry if this is a silly question. I
First of all, I'm fairly new to Java, so sorry if this question is
Sorry for the silly question, but I cannot seem to find an answer on
Sorry if this question is somewhat subjective. I am new to 'could store', 'distributed
I'm really new to VSTO so sorry if this is a newbie question. I'm
I'm new to jquery, and sorry if this question is asked before (could find
sorry if this is a simple question but i am new to java and
This might seem like a silly question, but after asking some questions on stackoverflow
Sorry if this is a stupid question but new to this so need some

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.