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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T23:35:21+00:00 2026-05-26T23:35:21+00:00

I want to read graph adjacency information from a text file and store it

  • 0

I want to read graph adjacency information from a text file and store it into a vector.

  • the file has arbitrary number of lines

  • each line has arbitrary number of integers ended with ‘\n’

for example,

First line:
0 1 4
Second line:
1 0 4 3 2
Thrid line:
2 1 3
Fourth line:
3 1 2 4
Fifth line:
4 0 1 3

If I use getline() to read one line at a time, how do I parse the line (as each line has variable number of integers)?

Any suggestions?

  • 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-26T23:35:22+00:00Added an answer on May 26, 2026 at 11:35 pm

    The standard line reading idiom:

    #include <fstream>
    #include <sstream>
    #include <string>
    #include <vector>
    
    
    std::ifstream infile("thefile.txt");
    std::string line;
    
    while (std::getline(infile, line))
    {
      std::istringstream iss(line);
      int n;
      std::vector<int> v;
    
      while (iss >> n)
      {
        v.push_back(n);
      }
    
      // do something useful with v
    }
    

    Here’s a one-line version using a for loop. We need an auxiliary construction (credits to @Luc Danton!) that does the opposite of std::move:

    namespace std
    {
      template <typename T> T & stay(T && t) { return t; }
    }
    
    int main()
    {
      std::vector<std::vector<int>> vv;
    
      for (std::string line;
           std::getline(std::cin, line);
           vv.push_back(std::vector<int>(std::istream_iterator<int>(std::stay(std::istringstream(line))),
                                         std::istream_iterator<int>())
                        )
           ) { }
    
      std::cout << vv << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to read each line from a text file and store them in
I want to read line n1->n2 from file foo.c into the current buffer. I
I have a text file. I want read that file. But In that if
I want to read and write from serial using events/interrupts. Currently, I have it
I want to read an xml file, apply a transform, then write to another
I want to read UTF-8 input in Perl, no matter if it comes from
I want to plot the pitch of a sound into a graph. Currently I
I am representing my graph as a adjacency list. I want to know how
I want to be able to occasionally programmatically grab some data from the graph
Got a issue with Facebook Graph API. I want to read page notifications via

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.