Thread: Paste a reference vector to Function

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

    Question Paste a reference vector to Function

    I have tested and see that in C++, C++ doesn't auto paste a reference vector to function like array. So, I use a pointer, but it has some problem for below code:
    Code:
    //Purpose: change value of vector in function
    void change(vector<int>*v){
        *v[0]=8; //ERROR HERE
         v[0]=8; //ERROR TOO
    }
    int main(){
        vector<int>v;
        v.push_back(1);
        change(&v);
        cout<<v[0]<<endl;
        return 0;
    }
    So, who can help me how to paste a reference vector to function, please.

    thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    a) if you passed it by reference, you would save yourself a whole bunch of messy syntax.

    b) Try (*v)[0]=8;
    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
    Aug 2011
    Posts
    116
    Oh, thanks so much. So, this point maybe the weak point of container in C++, that C++ doesn't auto to do that for programmer

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Why are you passing by pointer instead of by reference?

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    void change(vector<int>& v)
    {
       v[0]=8; 
    }
     
    int main()
    {
       vector<int>v;
       v.push_back(1);
       change(v);
       cout<<v[0]<<endl;
       return 0;
    }
    Jim
    Last edited by jimblumberg; 12-17-2011 at 11:28 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by hqt View Post
    Oh, thanks so much. So, this point maybe the weak point of container in C++, that C++ doesn't auto to do that for programmer
    No, arrays are broken in the language since they are the ones that are the exception. Passing an array to a function will actually pass a pointer to its first element, and that without even having to use the address-of operator which you always have to do otherwise!
    In fact, using the address-of operator produces a pointer to the array, which is an entirely different concept, and makes things even worse.

    Containers fixed this problem. You simply have to pass them by reference, as you would any normal variable.
    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
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I'm still trying to work out what "auto paste a reference vector to function" means.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by iMalc View Post
    I'm still trying to work out what "auto paste a reference vector to function" means.
    Google image search illustrates it...with some interesting results.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    What picture are you trying to imply is a good fit?
    My interpretation is that it simply means passing an array to a function. Since the syntax would be

    void foo(int array[])

    and passing would simply be

    foo(array);

    There is a high probability that this is indeed what the OP meant.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error while passing a reference of a vector to a function.
    By manasij7479 in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2011, 06:36 AM
  2. Passing Vector as a Reference
    By gbman88 in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2011, 02:30 AM
  3. Copy and Paste function in console apps.
    By Hakins90 in forum C Programming
    Replies: 5
    Last Post: 12-27-2007, 05:07 AM
  4. Problem converting std::vector into a reference
    By drrngrvy in forum C++ Programming
    Replies: 18
    Last Post: 10-12-2006, 09:31 PM
  5. Set Margins & Auto Paste Function Para :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 04-03-2002, 12:32 AM