如何检查向量中是否包含某个值? C++
我有一个向量,我试图对其执行包含函数。我收到某种类型的转换错误,但无法拼凑出解决方案。我还想知道我正在做的事情是否是检查向量是否包含值的适当方法。
这是代码:
#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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您使用默认构造函数声明变量时,不要在其后面放置
()
(尽管当您使用new
在空闲存储上分配空间时它是可选的)。所以这一行:应该变成
如果你保留它,它认为该行是一个名为
vec
的函数的函数原型,不带任何参数并返回std::vector;
,这就是您收到编译器错误的原因。是的,您用于查找项目的代码将起作用(称为线性搜索)。另外,如果您愿意,您可以使用
std::find
:如果您的向量按排序顺序排列,您还可以使用
binary_search
,它比find 快得多
,并且用法是相同的除了binary_search
返回一个bool
而不是迭代器(所以你不需要测试它反对vec.end())。如果您使用其中任何一个,请确保包含algorithm
标头。When you declare a variable with it's default constructor, you don't put
()
after it (although it's optional when you usenew
to allocate space on the free store). So this line:should become
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 astd::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 your vector is in sorted order, you can also use
binary_search
which is much faster thanfind
, and the usage is the same exceptbinary_search
returns abool
instead of an iterator (so you don't need to test it againstvec.end()
). Make sure you include thealgorithm
header if you use either of these.奇怪的是,这没有使用默认构造函数声明一个
向量
。这声明了一个不带参数并返回向量
的函数。试试这个:Oddly, this does not declare a
vector
using the default constructor. This declares a function taking no arguments and returning avector
. Try this instead:您可以使用 std::find 来检查 STL 数据结构是否包含特定值。
You can use std::find to check an STL datastructure to contain a certain value.