Pass vector to a function

This is a discussion on Pass vector to a function within the C++ Programming forums, part of the General Programming Boards category; I'm reading how to use STL in C++. And I have read one code: Code: void functionX(vector<int>&a){ ...... //some line ...

  1. #1
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115

    Question Pass vector to a function

    I'm reading how to use STL in C++. And I have read one code:
    Code:
    void functionX(vector<int>&a){
    ......
    //some line of code
    }
    int main(){
         vector<int> b(10);
         functionX(b);
         int c[10];
         functionX(c); //error
         return(0);
    }
    the above code mean functionX will use vector<a> to doing something (and it will not copy another vector-->efficiently purpose) But if I use for array c will meet error. So, my question is: the declare of functionX is especially design just for "vector"?

    thanks
    Last edited by hqt; 09-14-2011 at 04:04 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,410
    Quote Originally Posted by hqt
    So, my question is: the declare of functionX is especially design just for "vector"?
    Yes.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    Oh, I'm sorry so much, my question isn't clearer and... seem to be very stupid (because functionX just only and only for vector, of course!!!). But, now, I have answer this question by myself: this type of declare just use for container in STL of C++.
    Code:
    void X(stack<int>&Q){
    //some lines of code;
    }
    voidY(set<int>&A){
    //some lines of code
    }
    
    
    //BUT CANNOT USE FOR ARRAY:
    voidZ(int &A[10]){
    //some line of code;
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,410
    Indeed, you cannot have an array of references to int. What are you trying to do?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,200
    An array of int also cannot be implicitly converted to a vector<int>. So c (and array of 10 int) cannot be passed directly to functionX(), which expects a vector<int>.
    Right 98% of the time, and don't care about the other 3%.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how to declare and pass the vector pointer in c++
    By kapil1089thekin in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2010, 03:09 PM
  2. how to pass a vector to a function
    By chintugavali in forum C++ Programming
    Replies: 8
    Last Post: 12-20-2007, 09:28 PM
  3. How can I define and pass a pointer to a vector object?
    By asmileguo in forum C++ Programming
    Replies: 3
    Last Post: 09-07-2006, 11:19 AM
  4. how would i pass on a vector pointer parameter
    By bugmenot in forum C++ Programming
    Replies: 5
    Last Post: 12-16-2005, 01:51 AM
  5. pass a vector as argument
    By anomaly in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2003, 09:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21