Thread: Pass vector to a function

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    116

    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
    28,413
    Quote Originally Posted by hqt
    So, my question is: the declare of functionX is especially design just for "vector"?
    Yes.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    116
    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
    28,413
    Indeed, you cannot have an array of references to int. What are you trying to do?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    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%.

    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. 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, 10: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, 02:51 AM
  5. pass a vector as argument
    By anomaly in forum C++ Programming
    Replies: 3
    Last Post: 12-10-2003, 10:45 AM