8

I have a vector that I am trying to perform a contains function on. I am receiving some sort of casting error and I can't piece together a solution. I am also wanting to know whether or not what I am doing is the appropriate way to check if a vector contains a value.

Here is the code:

#include "stdafx.h"
#include <vector>

static void someFunc(double** Y, int length);
static bool contains(double value, std::vector<double> vec);

int main()
{
    double doubleArray[] = { 1, 2, 3, 4, 5 };
    double *pDoubleArray = doubleArray;
    int size = sizeof doubleArray / sizeof doubleArray[0];

    someFunc(&pDoubleArray, size);

    return 0;
}
static void someFunc(double** Y, int length)
{   
    std::vector<double> vec();

    for(int i = 0; i < 10; i++)
    {
        //error: 'contains' : cannot convert parameter 2 from 'std::vector<_Ty> (__cdecl *)(void)' to 'std::vector<_Ty>'
        if(contains(*(Y[i]), vec)) 
        {
            //do something
        }
    }

}
static bool contains(double value, std::vector<double> vec)
{
    for(int i = 0; i < vec.size(); i++)
    {
        if(vec[i] == value)
        {
            return true;
        }
    }

    return false;
}
0

3 Answers 3

29

When you declare a variable with it's default constructor, you don't put () after it (although it's optional when you use new to allocate space on the free store). So this line:

std::vector<double> vec();

should become

std::vector<double> vec;

If you leave it as you did, it thinks that line is a function prototype of a function called vec taking no parameters and returning a std::vector<double>, which is why you're getting a compiler error.

And yes, your code for finding an item will work (it's called a linear search). Also if you want to, you can use std::find:

if (std::find(vec.begin(), vec.end(), value) != vec.end())
    // found value in vec

If your vector is in sorted order, you can also use binary_search which is much faster than find, and the usage is the same except binary_search returns a bool instead of an iterator (so you don't need to test it against vec.end()). Make sure you include the algorithm header if you use either of these.

3
  • 1
    Awesome, works great! Thanks to others also who had overlapping answers! May 13, 2011 at 23:15
  • FYI, std::find returns the last element if the match isn't found, which is why != vec.end() is there
    – xinthose
    Jan 7, 2016 at 20:26
  • 1
    vec.end() doesn't return the last element, it returns the past-the-end element. Otherwise you wouldn't be able to match the final item.
    – SpBills
    Mar 4, 2022 at 16:32
7
std::vector<double> vec();

Oddly, this does not declare a vector using the default constructor. This declares a function taking no arguments and returning a vector. Try this instead:

std::vector<double> vec;
1
  • This is a good point, but doesn't answer the question. I may have upvoted anyways because it was helpful.
    – JohnAllen
    Apr 17, 2016 at 23:49
4

You can use std::find to check an STL datastructure to contain a certain value.

Not the answer you're looking for? Browse other questions tagged or ask your own question.