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 6982351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:16:36+00:00 2026-05-27T18:16:36+00:00

How to use C++ to get all the STDIN and parse it? For example,

  • 0

How to use C++ to get all the STDIN and parse it?

For example, my input is

2
1 4
3
5 6 7

I want to use C++ to read the STDIN using cin and store the each line in an array. So, it will be an vector/array of array of integers.

Thanks!

  • 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-27T18:16:37+00:00Added an answer on May 27, 2026 at 6:16 pm

    Since this isn’t tagged as homework, here’s a small example of reading from stdin using std::vectors and std::stringstreams. I added an extra part at the end also for iterating through the vectors and printing out the values. Give the console an EOF (ctrl + d for *nix, ctrl + z for Windows) to stop it from reading in input.

    #include <iostream>
    #include <vector>
    #include <sstream>
    
    int main(void)
    {
       std::vector< std::vector<int> > vecLines;
    
       // read in every line of stdin   
       std::string line;
       while ( getline(std::cin, line) )
       {
          int num;
          std::vector<int> ints;
          std::istringstream ss(line); // create a stringstream from the string
    
          // extract all the numbers from that line
          while (ss >> num)
             ints.push_back(num);
    
          // add the vector of ints to the vector of vectors         
          vecLines.push_back(ints);      
       }
    
       std::cout << "\nValues:" << std::endl;
       // print the vectors - iterate through the vector of vectors   
       for ( std::vector< std::vector<int> >::iterator it_vecs = vecLines.begin();
             it_vecs != vecLines.end(); ++it_vecs )
       {
          // iterate through the vector of ints and print the ints
          for ( std::vector<int>::iterator it_ints = (*it_vecs).begin();
             it_ints < (*it_vecs).end(); ++it_ints )
          {
             std::cout << *it_ints << " ";
          }
    
          std::cout << std::endl; // new line after each vector has been printed
       }
    
       return 0;
    }
    

    Input/Output:

    2
    1 4
    3
    5 6 7
    
    Values:
    2 
    1 4 
    3 
    5 6 7 
    

    EDIT: Added a couple more comments to the code. Also note that an empty vectors of ints can be added to vecLines (from an empty line of input), that’s intentional so that the output is the same as the input.

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

Sidebar

Related Questions

How can I get all differences, not just one? I want to use the
I want all my pages to use have some viewdata. i need to get
I'm trying to use the getElementsByTagName(a) method to get all the elements under a
In this table how would I use YQL/Xpath to get all data members of
I use CGWindowListCopyWindowInfo to get a list of all windows. It gives me the
You can use command lsof to get file descriptors for all running processes, but
How do you get a list of all the tables and use that list
I always seem to use Get when working with data (strongly typed or otherwise)
Obviously I could create a Calendar object with the date and use get(DAY) on
I want to process all links but external ones from the whole web site.

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.