如何检查一个值是否包含在一个向量中?C++
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5998576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I check if a value is contained in a vector? C++
提问by MintGrowth
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;
}
回答by Seth Carnegie
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:
当你用它的默认构造函数声明一个变量时,你不要把()
它放在后面(尽管当你用来new
在空闲存储上分配空间时它是可选的)。所以这一行:
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.
如果您保持原样,它会认为该行是一个函数的函数原型,该函数称为vec
不带参数并返回 a std::vector<double>
,这就是您收到编译器错误的原因。
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
:
是的,您用于查找项目的代码将起作用(称为线性搜索)。此外,如果您愿意,可以使用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 exceptbinary_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.
如果您的向量按排序顺序,您还可以使用binary_search
which 比 快得多find
,并且用法相同,除了binary_search
返回 abool
而不是迭代器(因此您不需要针对 进行测试vec.end()
)。algorithm
如果您使用其中任何一个,请确保包含标题。
回答by Rob?
std::vector<double> vec();
Oddly, this does notdeclare a vector
using the default constructor. This declares a function taking no arguments and returning a vector
. Try this instead:
奇怪的是,这并没有vector
使用默认构造函数声明 a 。这声明了一个不带参数的函数并返回一个vector
. 试试这个:
std::vector<double> vec;