C++ c++;数组值匹配变量

C++ c++;数组值匹配变量,c++,C++,我想看看一个值是否等于数组中的任何值。 像这样 Beginning of function(here I give Val differnt values with for loops...) for (i=0;i<array_size;i++) if (Val==array[i]) do something else do something else if non of the above values

我想看看一个值是否等于数组中的任何值。 像这样

Beginning of function(here I give Val differnt values with for loops...)

       for (i=0;i<array_size;i++)
            if (Val==array[i])
            do something

    else 
    do something else if non of the above values matched val...
函数的开头(这里我用for循环给Val不同的值…)

对于(i=0;i使用标志指示是否至少满足一次条件:

bool hasMatch = false;
for (i=0;i< array_size;i++) {
        if (Val==array[i]) {
            //       do something
            hasMatch = true;
        }
}
if( !hasMatch ) {
  // do whatever else
}
bool hasMatch=false;
对于(i=0;i

这将为每个匹配元素调用“do something”。如果要为第一个匹配元素调用它,请仅在“do something”之后使用
break;

使用标志指示条件是否至少满足一次:

bool hasMatch = false;
for (i=0;i< array_size;i++) {
        if (Val==array[i]) {
            //       do something
            hasMatch = true;
        }
}
if( !hasMatch ) {
  // do whatever else
}
bool hasMatch=false;
对于(i=0;i

这将为每个匹配元素调用“do something”。如果要为第一个匹配元素调用它,请仅在“do something”之后使用
break;

不完全确定要执行的操作。如果找到匹配项,然后从函数返回,则始终可以执行操作

for (int i=0; i < array_size; i++) {
    if (Val==array[i]) {
        // do something
        return;
    }
}

// if we end up here, there was no match
// do something else ..
for(int i=0;i

或者,您可以设置一个布尔值并打破循环,有很多方法。选择一种:)

不完全确定您要的是什么。如果找到匹配项,然后从函数返回,则始终可以执行自己的操作

for (int i=0; i < array_size; i++) {
    if (Val==array[i]) {
        // do something
        return;
    }
}

// if we end up here, there was no match
// do something else ..
for(int i=0;i

或者,您可以设置一个布尔值并打破循环,有很多方法。选择一个:)

您可以使用查找功能

int find(int value, int* array, int size) {
    int i;
    for (i=0; i<size; i++) {
        if (array[i]==value) {
           return i;
        }
    }
    return -1;
}

您可以使用find函数

int find(int value, int* array, int size) {
    int i;
    for (i=0; i<size; i++) {
        if (array[i]==value) {
           return i;
        }
    }
    return -1;
}

对不起,打扰一下。我有点感觉,你要问史密斯。其他的据我所知,您有一组有序的值,并且希望从数组中的该集合中找到第一个可能的值

如果使用C++标准库中的算法。STL中的算法可能更适合您的编译器(例如,它们可能支持并行搜索)

下面是一段改编自CPPReference.com的代码。您不限于int值

int nums[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int* nend = &nums[10];

int targets[] = { 9, 4, 7 };
int* tend=&targets[3];

using namespace std;
int* result = find_first_of( &nums[0], nend, &targets[0], tend );

if( result == nend )
  cout << "Did not find any of { 9, 4, 7 }" << endl;
else
  cout << "Found a matching target: " << *result << endl;
intnums[]={0,1,2,3,4,5,6,7,8,9,10};
int*nend=&nums[10];
int目标[]={9,4,7};
int*tend=&目标[3];
使用名称空间std;
int*result=find_first_of(&nums[0],nend,&targets[0],tend);
如果(结果==nend)

对不起,打扰一下。我有点感觉,你要问史密斯。其他的据我所知,您有一组有序的值,并且希望从数组中的该集合中找到第一个可能的值

如果使用C++标准库中的算法。STL中的算法可能更适合您的编译器(例如,它们可能支持并行搜索)

下面是一段改编自CPPReference.com的代码。您不限于int值

int nums[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int* nend = &nums[10];

int targets[] = { 9, 4, 7 };
int* tend=&targets[3];

using namespace std;
int* result = find_first_of( &nums[0], nend, &targets[0], tend );

if( result == nend )
  cout << "Did not find any of { 9, 4, 7 }" << endl;
else
  cout << "Found a matching target: " << *result << endl;
intnums[]={0,1,2,3,4,5,6,7,8,9,10};
int*nend=&nums[10];
int目标[]={9,4,7};
int*tend=&目标[3];
使用名称空间std;
int*result=find_first_of(&nums[0],nend,&targets[0],tend);
如果(结果==nend)

cout对于大多数查找操作,STL都有一个算法。在数组中查找值可以使用。。。查找

const char values[] = "abcdefg";
const char* values_end = values + _countof( values );

const bool dPresent = 
 std::find_if( values, values_end , 'd' ) != values_end ;

if( dPresent ) {
    ...
}

对于大多数查找操作,STL都有一个算法。在数组中查找值可以使用。。。查找

const char values[] = "abcdefg";
const char* values_end = values + _countof( values );

const bool dPresent = 
 std::find_if( values, values_end , 'd' ) != values_end ;

if( dPresent ) {
    ...
}
请查看您能为您做些什么。
有一整节是关于算法的:

if (std::find(array,array+array_size,Val) != array+array_size)
{
    // Found it.
}
else
{
    // Did not find it.
}
请查看您能为您做些什么。
有一整节是关于算法的:

if (std::find(array,array+array_size,Val) != array+array_size)
{
    // Found it.
}
else
{
    // Did not find it.
}

非常感谢这基本上就是我想要的。。。我只是想不用额外的变量就可以做到这一点。如果你需要为每个匹配元素调用操作,你需要额外的变量来保持在你继续循环时匹配已经发生的事实。谢谢我使用了这个片段,我使用了一个普通变量而不是bool。。。如果有匹配,就给它一个值。。。我现在开始工作,并尝试用C++解决剩下的1000亿个问题(=什么是“正常变量””在这种情况下比bool更好?不,可能不是,但我只是在尝试bool时犯了一些错误,因为我不能100%确定最终我将如何使用这个函数,所以我选择以我得到它的方式尽可能快地尝试它…非常感谢,这基本上就是我在寻找的…我只是认为我可以不用它使用额外的变量…如果需要为每个匹配元素调用操作,则需要额外的变量来保持在继续循环时发生了匹配的事实。感谢我使用了此代码段exept,我使用了一个普通变量而不是bool…并在有匹配的情况下给它一个值…我现在让它工作并尝试用C++解决我的1000亿个问题的其余部分(=什么是“正常变量””在这个例子中,我不知道,但我可能做了一些错误,当我尝试了BoL,因为我还不确定我将如何使用这个函数,我最终选择尽可能快地尝试它,我用它的方式来工作…或者,因为它是一个C++问题,使用+ +查找函数,因为它是C++问题,使用+ +查找函数。这看起来很有趣。在我的代码中,我实际上有多个目标,所以我明天会尝试…现在是回家放松的时候了…欢呼这看起来很有趣。在我的代码中,我实际上有多个目标,所以我明天会尝试…现在是回家放松的时候了…欢呼