Array or Vector as augment

This is a discussion on Array or Vector as augment within the C++ Programming forums, part of the General Programming Boards category; I am curious if there is a way to pass an entire array or vector off to a function. I ...

  1. #1
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229

    Array or Vector as augment

    I am curious if there is a way to pass an entire array or vector off to a function. I think that I read a little about it on the pointers or array tutorial, but I can't find it anymore.

    Thanks,
    David

  2. #2
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Pass an array as a pointer and pass a vector just the way you'd pass anything else.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  3. #3
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Ok, thanks.

    One more thing. When you pass a vector, do you need to specify what type it is, like <int> or <char>

  4. #4
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Yes, you do. Or you can use a template and ignore the problem.
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    It is usually best to pass a vector by reference so the contents are not copied. If you don't plan on changing the vector in the function, then you should pass by const reference. So generally vectors are passed like this:
    Code:
    void foo(const std::vector<int>& my_vector)

  6. #6
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Thanks, thats what I was looking for.

  7. #7
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Ok, I can not get this thing to work for some reason.

    If I use this:
    Code:
    const std::vector<int>& my_vector
    The function is expecting an address, right?

    Also it won't assign the address of the vector to my pointer, I'm using this:
    Code:
    int *taken_ref;
    taken_ref = & taken_pos;

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    No, if you use my version, the function is taking a reference, not an address ('&' has a different meaning in different uses). You just pass the vector as is (like any other variable) and the reference will be created automatically.

  9. #9
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Well, that worked although I don't understand why. If it wouldn't too much trouble could you explain it?

    Thanks

  10. #10
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    Do you know about references yet? Reading up on those would be a good start. Basically, a reference is like an alias. When you create a reference, you initialize it with another object, so in this code:
    Code:
    int i = 3;
    int& r = i;
    you initialized r with i. Now both r and i refer to the same value, the value in i was not copied at all. From this point on in the program, you can do exactly the same thing with r that you can do with i, and vice versa. Anything you do to r applies to i as well. So these two lines of code will have the exact same effect:
    Code:
    i = 5; // both i and r are now 5
    Code:
    r = 5; // both i and r are now 5
    or you can look at it this way:
    Code:
    int i = 1;
    int& r = i;
    ++i; // value is now 2.
    ++r; // value is now 3.
    ++i; // value is now 4.
    ++r; // value is now 5.
    std::cout << "i: " << i << '\n'; // i: 5
    std::cout << "r: " << r << '\n'; // r: 5
    So when you make your function parameter a reference, the same thing occurs. The reference variable is initialized with the other variable (just as r is initialized with i above), and whatever you do to the reference variable applies to the original as well without any copies being made. Note that this is why the const can be important, because it protects you from accidentally changing the original vector.

  11. #11
    Registered User IdioticCreation's Avatar
    Join Date
    Nov 2006
    Location
    Lurking about
    Posts
    229
    Ok, I understand. I was just getting references confused with pointers. Thanks for taking your time to explain.

  12. #12
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Quote Originally Posted by Daved
    It is usually best to pass a vector by reference so the contents are not copied. If you don't plan on changing the vector in the function, then you should pass by const reference. So generally vectors are passed like this:
    Code:
    void foo(const std::vector<int>& my_vector)
    As a small aside to people just learning references and pointers, although most people call this a const reference, that's kind of a bad name as it's more correctly "a reference to const". For references, there is only one kind, so this nomenclature isn't ambiguous, but with pointers, a "const pointer" and "pointer to const" are very different.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

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