Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a value is contained in a vector? C++

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;
}
like image 767
LunchMarble Avatar asked May 13 '11 23:05

LunchMarble


People also ask

How do you check if a value exists in a vector?

%in% operator can be used in R Programming Language, to check for the presence of an element inside a vector. It returns a boolean output, evaluating to TRUE if the element is present, else returns false.

How do you check whether an element is present in a vector or not in C++?

To check whether an elements exists in a vector or not – we use find() function. find() function takes 3 arguments. Syntax: find(InputIterator first, InputIterator last, const T& element);

How do you check if a string is present in vector?

The contains() method of Java Vector class is used to check the vector which is in use contains the specified element or not. It returns true if this vector contains the specified element, otherwise returns false.

How do you know if a vector element is empty?

vector::empty() The empty() function is used to check if the vector container is empty or not.


3 Answers

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.

like image 64
Seth Carnegie Avatar answered Oct 08 '22 19:10

Seth Carnegie


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;
like image 38
Robᵩ Avatar answered Oct 08 '22 18:10

Robᵩ


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

like image 45
Christian Rau Avatar answered Oct 08 '22 19:10

Christian Rau