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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T23:56:04+00:00 2026-05-12T23:56:04+00:00

I dont know why the entire matrix gets stored in the first row itself.

  • 0

I dont know why the entire matrix gets stored in the first row itself. The loop does actually get called N times if there are N rows.

and this is matrix.dat

5
1 2 3
1 2 0 100
3 4 0
5 6 -1
0 9 10 11

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

int main() {

    std::vector<std::vector<int> > matrix;
    std::ifstream infile("matrix.dat");
    int num_rows;
    infile>>num_rows;

    //If there are 5 rows in the matrix, this loops DOES run 5 times.
    for(int i=0;i<num_rows;i++){
            matrix.push_back(std::vector<int>());
            std::copy(
                            std::istream_iterator<int>(infile),
                            std::istream_iterator<int>(),
                            std::back_inserter(matrix[i])
                            );
    }

    // Printing the size of matrix. This correctly prints the value of num_rows
    std::cout<<matrix.size()<<std::endl;

    // Printing just 1st row, but that contains the entire matrix.
    // Seems like copy always happens to matrix[0] only.

    for(int j=0;j<matrix[0].size();j++)
        std::cout<<matrix[0][j]<<" ";

}
  • 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-12T23:56:04+00:00Added an answer on May 12, 2026 at 11:56 pm
    #include <vector>
    #include <fstream>
    #include <string>
    #include <sstream>
    #include <algorithm>
    
    int main()
    {
    
        std::vector< std::vector< int > > matrix;
        std::ifstream infile( "matrix.dat" );
    
        std::string s;
        while( std::getline( infile, s ) )
        {
            std::string token;
            std::vector< int > tokenisedLine;
            std::istringstream line(s);
            while( std::getline( line, token, ' ' ) )
                tokenisedLine.push_back( atoi( token.c_str() ) );
            matrix.push_back( tokenisedLine );
        }
    
        return 0;
    }
    

    This code should do what you are after, however it is a little slow, with the copying and creation of all the temporary objects. but for small files like your example this will be fine.

    It compiled and worked for me using your test data.

    as you can see it uses getline twice the first time is splitting lines based on the \n char, then we use it again using the space char. Therefore you need to use spaces to separate elements when using this code.

    Then once we have the token as a string we use atoi to convert it to an int.

    HTH.

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

Sidebar

Related Questions

I don't know if the DBIx::Class ORM is substantial enough to justify an entire
I dont know what happend but I cant register any dlls anymore. It seems
I dont know if my sessions are too big. Is there a way to
I dont know where my error is in this mysql query $sql = INSERT
I dont know whether I am reiventing the wheel. I need to design an
I dont know how to use wireframesketcher in eclipse. http://wireframesketcher.com/install.html I've installed it, and
i dont know, why the button are disappear after the toolbar set to hide
I dont know how to acces my method of my class ProjectNode, that is
I dont know if it is asked (I couldn't find any). I want to
I dont know how to describe it well, but i will try. Ok, i

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.