Thread: load matrix into a vector of vectors

  1. #1
    Registered User
    Join Date
    Jun 2006
    Posts
    28

    load matrix into a vector of vectors

    hi,

    is there an efficient for to insert the elements of a 2d array into a vector of vectors?
    I have
    Code:
    	vector< vector <int> >  Points
    
    	for (i = 0; i < n; i++ )
    	  	Points.push_back(vector<int>(d));
    
            int Matrix[n][d];
    
            //populate matrix....
    
            //How to load the data in the Matrix into the vector of v. Points?
            // is there something better than
            //iteratation through:
            Points[i][j] = Matrix[i][j]
    thanks for the help

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You could use iterators, but that would be basicly the same thing.

    You might be able to find a vector constructor to simplify things; from http://www.fredosaurus.com/notes-cpp...er-vector.html
    Constructors and destructors
    vector<T> v; L
    Creates an empty vector of T's.
    vector<T> v(n);
    Creates vector of n default values.
    vector<T> v(n, e); L
    Creates vector of n copies of e.
    vector<T> v(beg, end); L
    Creates vector with elements copied from range beg..end.
    v.~vector<T>(); L
    Destroys all elems and frees memory.
    My suggestion is to replace the code that "populates matrix" with code that populates Points, if possible.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C - access violation
    By uber in forum C Programming
    Replies: 2
    Last Post: 07-08-2009, 01:30 PM
  2. Matrix Help
    By HelpmeMark in forum C++ Programming
    Replies: 27
    Last Post: 03-06-2008, 05:57 PM
  3. What is a matrix's purpose in OpenGL
    By jimboob in forum Game Programming
    Replies: 5
    Last Post: 11-14-2004, 12:19 AM
  4. Matrix and vector operations on computers
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 05-11-2004, 06:36 AM