Like Tree4Likes
  • 1 Post By Salem
  • 2 Post By jimblumberg
  • 1 Post By Elysia

Paste a reference vector to Function

This is a discussion on Paste a reference vector to Function within the C++ Programming forums, part of the General Programming Boards category; I have tested and see that in C++, C++ doesn't auto paste a reference vector to function like array. So, ...

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

    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
    32,824
    a) if you passed it by reference, you would save yourself a whole bunch of messy syntax.

    b) Try (*v)[0]=8;
    hqt likes this.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    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
    2,917
    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.
    Elysia and hqt like this.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    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.
    hqt likes this.
    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,308
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,526
    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.
    Manasij Mukherjee | gcc-4.9.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,012
    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, 10: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

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