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 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

Ask A Question

Stats

  • Questions 239k
  • Answers 239k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Update Upon reading your comments it's clear that you're wanting… May 13, 2026 at 6:58 am
  • Editorial Team
    Editorial Team added an answer IMPORTANT: The question (and my code below) both deal with… May 13, 2026 at 6:58 am
  • Editorial Team
    Editorial Team added an answer Like this: $(':input', document.newNote.contentWindow.document).change(...); Assuming that document.newNote is the IFRAME. May 13, 2026 at 6:58 am

Related Questions

Now, I know this is completely subjective, so please don't flame me. I've never
I am not really sure what to do in this situation. I have some
Due to incomplete understanding of eclipse infrastructure I have one problem. Historically we have
well, I have problems with my queries,i dont know why!! please help i try

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.