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

The Archive Base Latest Questions

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

I have a text file with on every line one or more integers, seperated

  • 0

I have a text file with on every line one or more integers, seperated by a space. How can I in an elegant way read this with C++? If I would not care about the lines I could use cin >>, but it matters on which line integers are.

Example input:

1213 153 15 155
84 866 89 48
12
12 12 58
12
  • 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:34:07+00:00Added an answer on May 13, 2026 at 7:34 pm

    It depends on whether you want to do it in a line by line basis or as a full set. For the whole file into a vector of integers:

    int main() {
       std::vector<int> v( std::istream_iterator<int>(std::cin), 
                           std::istream_iterator<int>() );
    }
    

    If you want to deal in a line per line basis:

    int main()
    {
       std::string line;
       std::vector< std::vector<int> > all_integers;
       while ( getline( std::cin, line ) ) {
          std::istringstream is( line );
          all_integers.push_back( 
                std::vector<int>( std::istream_iterator<int>(is),
                                  std::istream_iterator<int>() ) );
       }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 100Mb text file and I need to check every line for special
I have a text file that contains 113 line and 10 columns. Every line
I have text file with something like first line line nr 2 line three
I have text file with some text information and i need to split this
I have a text file that is available on one computer only and I
I have a text file of this format: L O A D C A
I have text file, names.txt, with every row containing a last name: Smith Johnson
I'm trying to read a text file and store each line in a node
I have a txt file with 16 lines of integers. Each line contains 5
I have text file with some stuff that i would like to put into

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.