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;
}
%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.
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);
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.
vector::empty() The empty() function is used to check if the vector container is empty or not.
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.
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;
You can use std::find to check an STL datastructure to contain a certain value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With