Thread: Question about vectors

  1. #1
    Registered User
    Join Date
    Jun 2013
    Posts
    33

    Question about vectors

    Is it possible to assign a bidimensional array to a vector? I need to meshgrid 2 unidimensional array to obtain 2 bidimensional array. I can work just with vector. but when I have to meshgrid I don t know how it works.
    Example:

    Code:
    vector<double>x(cols);
    vector<double>y(rows);
    vector<double>X(rows*cols);
    vector<double>Y(rows*cols);
    
    for(int i=0;i<cols;i++){
      for(int z=0;z<rows;z++){
        X[i][z]=x[i];  //error 
        Y[i][z]=y[z];  //error
      }
    }
    Using arrays I can do it, but I need to do with vectors. Thanks to help evenctually

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yes, vectors can be nested inside vectors.
    Code:
    #include <vector>
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main ( ) {
    	const int rows = 10;
    	const int cols = 20;
    	vector< vector<double> > matrix( rows, vector<double>(cols,42) );
    	for ( int r = 0 ; r < rows; r++ ) {
    		for ( int c = 0 ; c < cols ; c++ ) {
    			cout << matrix[r][c] << " ";
    		}
    		cout << endl;
    	}
    }
    The only real catch is that > > needs a space, to prevent it being confused with >> (which has other connotations).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    So if I understood well in my case:

    Code:
    vector<double>x(cols);
    vector<double>y(rows);
    vector< vector><double> > X(rows,cols);
    vector< vector><double> > Y(rows,cols);
    
    for(inti=0;i<cols;i++){  for(int z=0;z<rows;z++){
        X[i][z]=x[i];  
        Y[i][z]=y[z];  
       }
    }
    
    Is it true? thanks

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    vector< vector<double> > X(rows, vector<double>(cols) );
    vector< vector<double> > Y(rows, vector<double>(cols) );
    Kurt

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Salem View Post
    The only real catch is that > > needs a space, to prevent it being confused with >> (which has other connotations).
    Correction: It needs a space if you are using C++98/03. With C++11 and forward, it's optional.

    Also, you should iterate over rows first, then columns. It can make a lot of difference for tight loops, and it's such an easy optimization that it's really viable to do all the time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    Is it possible to pass a vector of vector to a function?
    Code:
    void pre_filter_Computations(vector< vector<double> > radius(rows,  vector<double>(cols) ),vector< vector<double> > theta(rows, vector<double>(cols) ),int cols,int rows);
    
    Mat logGabor(vector< vector<double> >  filter(rows, vector<double>(cols) ),Mat imfft,double r_o, double theta_o,double sigma_theta,vector< vector<double> >radius(rows, vector<double>(cols) ),vector < vector<double> >theta(rows,vector<double>(cols) ),int cols,int rows,double sigma_r,int *padSize);
    

    I have some errors:

    Multiple markers at this line :
    -declared here
    -expected ',' or '...' before '(' token
    Last edited by Elvio Esposito; 06-28-2013 at 03:25 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Yes, it is. But you will have to learn how to declare function prototypes and how to pass arguments correctly first. And read about references while you're at it. Copying vectors can be expensive.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jun 2013
    Posts
    33
    The problem is in my University we just studied C language. I m working to a thesis ' project and I work with image-processing and OpenCV so i have to use C++ in some parts of the works. I understood how to pass it to a function , but now i have en error invoking the function. If r is the vector of vectors:

    funtion(r,cols,rows);

    I have error. I just put the name of the variable.

  9. #9
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    I'm going to guess that the code you posted an hour ago is the source of the problem:

    Code:
    void pre_filter_Computations(vector< vector<double> > radius(rows,  vector<double>(cols) ),vector< vector<double> > theta(rows, vector<double>(cols) ),int cols,int rows);
    
    Mat logGabor(vector< vector<double> >  filter(rows, vector<double>(cols) ),Mat imfft,double r_o, double theta_o,double sigma_theta,vector< vector<double> >radius(rows, vector<double>(cols) ),vector < vector<double> >theta(rows,vector<double>(cols) ),int cols,int rows,double sigma_r,int *padSize);
    These are not not well written prototypes because there are too many details. Let's forget about vector for a moment:
    Code:
     void pre_filter_Computations(array radius, array theta, int cols, int rows);
    
    Mat logGabor (array filter, Mat imfft, double r_o, double theta_o, double sigma_theta, int cols, int rows, double sigma_r, int *padSize);
    I took special care to emphasize the types of your parameters. When you write a parameter list, there only has to be enough information to identify the type. That means that you want to include the class name and the template instantiation, like this: "vector<vector<double> > paramName". If the type is constant, volatile, mutable, or whatever, you will modify the type accordingly. The rest of it is too much detail.

    When you call your function, you will want to set up objects to act as arguments. Then you include their names, in order according to the parameter list, where you call.

    I would also like to emphasize:
    • that vectors can give you column- and row-size information
    • it might be best to stick to one matrix type
    • if you have a matrix type, it is likely optimized for mathematical programming
    • and that vector is not the best math matrix
    Last edited by whiteflags; 06-28-2013 at 04:53 PM.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Show relevant snippets of code and the complete error. Otherwise we can only guess.
    But you really should spend some time learning C++.
    Anyway, here is how you would pass a vector to a function:

    auto foo(const std::vector<std::vector<T>>& myvector) -> void
    // Or void foo(const std::vector<std::vector<T>>& myvector) if you are using an older compiler

    std::vector<std::vector<T>> myvector;
    // Put stuff in vector
    foo(myvector);

    Remove "const" if you are intending the function to modify it. T can be any type - int, double, your custom class, etc.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Elysia View Post
    T can be any type - int, double, your custom class, etc.
    T can be almost any type. Generally speaking, the type needs to support value semantics.

    A custom class with a private copy constructor, for example, doesn't work too well within a vector.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question about Vectors
    By ataman in forum C++ Programming
    Replies: 4
    Last Post: 06-02-2008, 12:36 PM
  2. 2d vectors question
    By jw232 in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2008, 06:31 PM
  3. Vectors question
    By tezcatlipooca in forum C++ Programming
    Replies: 17
    Last Post: 12-20-2006, 04:49 AM
  4. vectors question
    By rxg00u in forum C++ Programming
    Replies: 5
    Last Post: 04-13-2002, 08:28 AM
  5. vectors question
    By rxg00u in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2002, 03:48 AM

Tags for this Thread