93

I'm trying to send a vector as an argument to a function and i can't figure out how to make it work. Tried a bunch of different ways but they all give different error messages. I only include part of the code, since it's only this part that doesn't work. (the vector "random" is filled with random, but sorted, values between 0 and 200)

Updated the code:

#include <iostream>     
#include <ctime>        
#include <algorithm>    
#include <vector>       

using namespace std;

int binarySearch(int first, int last, int search4, vector<int>& random);

int main()
{
    vector<int> random(100);

    int search4, found;
    int first = 0;
    int last = 99;

    found = binarySearch(first, last, search4, random);

    system("pause");    
    return(0);      
}

int binarySearch(int first, int last, int search4, vector<int>& random)
{
    do
    {
        int mid = (first + last) / 2;  
        if (search4 > random[mid]) 
            first = mid + 1;  
        else if (search4 < random[mid]) 
            last = mid - 1; 
        else
            return mid;     
    } while (first <= last); 

    return -(first + 1);
}
Dagrooms
  • 1,507
  • 2
  • 16
  • 42
Joe
  • 955
  • 1
  • 7
  • 6
  • 1
    What do you mean by doesn't work? Please post errors. – Dat Chu Mar 16 '11 at 23:38
  • 1
    On the updates code: The parameters first and last are **values** from the vector, not indexes. You also never set a value to search for (search4)! – Bo Persson Mar 17 '11 at 10:32
  • 1
    Using `using namespace std;` is a bad idea. [Why?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Tomask Dec 28 '15 at 13:35
  • please note that the expected error in the question has been corrected.. making it meaningless.. – kingsjester Feb 07 '23 at 13:43

7 Answers7

136

It depends on if you want to pass the vector as a reference or as a pointer (I am disregarding the option of passing it by value as clearly undesirable).

As a reference:

int binarySearch(int first, int last, int search4, vector<int>& random);

vector<int> random(100);
// ...
found = binarySearch(first, last, search4, random);

As a pointer:

int binarySearch(int first, int last, int search4, vector<int>* random);

vector<int> random(100);
// ...
found = binarySearch(first, last, search4, &random);

Inside binarySearch, you will need to use . or -> to access the members of random correspondingly.

Issues with your current code

  1. binarySearch expects a vector<int>*, but you pass in a vector<int> (missing a & before random)
  2. You do not dereference the pointer inside binarySearch before using it (for example, random[mid] should be (*random)[mid]
  3. You are missing using namespace std; after the <include>s
  4. The values you assign to first and last are wrong (should be 0 and 99 instead of random[0] and random[99]
Jon
  • 428,835
  • 81
  • 738
  • 806
  • "The values you assign to first and last are wrong (should be 0 and 99 instead of random[0] and random[99]" - but i want "first" to be the first value in random and "last" to be the last one. I do not want them to be the values 0 and 99. – Joe Mar 17 '11 at 00:36
  • Joe: `first` and `last` are *indexes*, not values. You are confused as to what they represent. – Jon Mar 17 '11 at 01:24
  • Then how do i give `Last` the last value of the vector? – Joe Mar 17 '11 at 10:02
  • oh, i'm sorry, i see my misstake now. Thank you! – Joe Mar 17 '11 at 23:17
  • @Jon I have a small doubt after reading your second point, Why we can not dereference that pointer which collects the address of array when we pass it to a function. – Prince Vijay Pratap Sep 04 '15 at 21:14
  • Can you explain why "the option of passing it by value is clearly undesirable" ? If I just want to print the element in it , will it be faster ? –  Jun 29 '17 at 12:54
  • 1
    @lecaruyer: Briefly, passing by value means that the contents of the vector must be copy-constructible, and a copy of everything will actually be created. The effects of this could in practice range from negligible, to very problematic from a performance and/or resource point of view, to code that does not even compile. There's no reason to do it unless you really do want a second copy of the stuff. – Jon Jun 29 '17 at 19:29
8

You'll have to pass the pointer to the vector, not the vector itself. Note the additional '&' here:

found = binarySearch(first, last, search4, &random);
Mario
  • 35,726
  • 5
  • 62
  • 78
2

You're passing in a pointer *random but you're using it like a reference &random

The pointer (what you have) says "This is the address in memory that contains the address of random"

The reference says "This is the address of random"

corsiKa
  • 81,495
  • 25
  • 153
  • 204
2

Anytime you're tempted to pass a collection (or pointer or reference to one) to a function, ask yourself whether you couldn't pass a couple of iterators instead. Chances are that by doing so, you'll make your function more versatile (e.g., make it trivial to work with data in another type of container when/if needed).

In this case, of course, there's not much point since the standard library already has perfectly good binary searching, but when/if you write something that's not already there, being able to use it on different types of containers is often quite handy.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
1
found = binarySearch(first, last, search4, &random);

Notice the &.

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
0

You're using the argument as a reference but actually it's a pointer. Change vector<int>* to vector<int>&. And you should really set search4 to something before using it.

Puppy
  • 144,682
  • 38
  • 256
  • 465
0

If you use random instead of * random your code not give any error

Javad Yousefi
  • 2,250
  • 4
  • 35
  • 52