C++ c++;:用单个函数调用替换多个索引相等性测试

C++ c++;:用单个函数调用替换多个索引相等性测试,c++,C++,在c++/c++11中,替换表单多次比较的正确方法是什么: if(isIndexToFind==index1 || isIndexToFind==index2 ...) if(isIn(indexToFind,index1,index2,...)) 用一些不那么凌乱的形式: if(isIndexToFind==index1 || isIndexToFind==index2 ...) if(isIn(indexToFind,index1,index2,...)) 对于不同数量的参数inde

在c++/c++11中,替换表单多次比较的正确方法是什么:

if(isIndexToFind==index1 || isIndexToFind==index2 ...)
if(isIn(indexToFind,index1,index2,...))
用一些不那么凌乱的形式:

if(isIndexToFind==index1 || isIndexToFind==index2 ...)
if(isIn(indexToFind,index1,index2,...))
对于不同数量的参数index1、index2。。。这段代码属于数值计算,所以我希望有一些与直接比较一样有效的东西


也许有意思的是,index1和index2都是常量静态值,因此,基于可变模板的解决方案可能会引起兴趣?

您可以使用std::任意一种

v.push_back(index1);
v.push_back(index2);

if (std::any_of(v.cbegin(), v.cend(), [](int& index){return index==isIndexToFind;}))
如果您真的想使用您提到的表单,那么可变模板也可以使用。比如:

#include <iostream>
#include <vector>

template<typename T>
bool isIn(T indexToFind,T first_index) { return indexToFind==first_index; } 

template<typename T,typename ... Tail>
bool isIn(T indexToFind,T first_index, Tail... remaining_indices)
{
    if (indexToFind==first_index)
        return true;
    else
        return isIn(indexToFind,remaining_indices...);
}

const static int index1{23};
const static int index2{34};
const static int index3{88};
const static int index4{24};
const static int index5{21};

int main()
{
    bool found = isIn(621,index1,index2,index3,index4,index5);

    if (found) {
        std::cout << "Index was found\n";
    }
    else {
        std::cout << "Index was not found\n";
    }

    return 0;
}
#包括
#包括
模板
bool isIn(T indexToFind,T first_index){return indexToFind==first_index;}
模板
bool-isIn(T-indextofId、T-first-u-index、Tail…剩余的索引)
{
if(indextofId==第一个索引)
返回true;
其他的
返回isIn(IndextofId,剩余的指数…);
}
常量static int index1{23};
常量静态int index2{34};
常量static int index3{88};
常量static int index4{24};
常量static int index5{21};
int main()
{
bool found=isIn(621,index1,index2,index3,index4,index5);
如果(找到){

std::cout您可以使用std::任何类型的

v.push_back(index1);
v.push_back(index2);

if (std::any_of(v.cbegin(), v.cend(), [](int& index){return index==isIndexToFind;}))
如果您真的想使用您提到的表单,那么可变模板也可以使用。例如:

#include <iostream>
#include <vector>

template<typename T>
bool isIn(T indexToFind,T first_index) { return indexToFind==first_index; } 

template<typename T,typename ... Tail>
bool isIn(T indexToFind,T first_index, Tail... remaining_indices)
{
    if (indexToFind==first_index)
        return true;
    else
        return isIn(indexToFind,remaining_indices...);
}

const static int index1{23};
const static int index2{34};
const static int index3{88};
const static int index4{24};
const static int index5{21};

int main()
{
    bool found = isIn(621,index1,index2,index3,index4,index5);

    if (found) {
        std::cout << "Index was found\n";
    }
    else {
        std::cout << "Index was not found\n";
    }

    return 0;
}
#包括
#包括
模板
bool isIn(T indexToFind,T first_index){return indexToFind==first_index;}
模板
bool-isIn(T-indextofId、T-first-u-index、Tail…剩余的索引)
{
if(indextofId==第一个索引)
返回true;
其他的
返回isIn(IndextofId,剩余的指数…);
}
常量static int index1{23};
常量静态int index2{34};
常量static int index3{88};
常量static int index4{24};
常量static int index5{21};
int main()
{
bool found=isIn(621,index1,index2,index3,index4,index5);
如果(找到){

std::cout对于类似的内容,通常使用数组作为一种查找表

假设您的索引是
int

std::array<int, N> indicesTable{{index1, index2, ...}};
bool in = std::find(std::begin(indicesTable), std::end(indicesTable), isIndexToFind) != std::end(indicesTable);
if (in)
   // ...

通常使用数组作为类似这样的查找表

假设您的索引是
int

std::array<int, N> indicesTable{{index1, index2, ...}};
bool in = std::find(std::begin(indicesTable), std::end(indicesTable), isIndexToFind) != std::end(indicesTable);
if (in)
   // ...

你可以这样写

#include <iostream>
#include <algorithm>
#include <initializer_list>

template <class T>
bool one_of( const T &value, std::initializer_list<T> lst )
{
    return std::any_of( lst.begin(), lst.end(), [&]( const T &x ) { return value == x; } );
}    

int main()
{
    std::cout << one_of( 1, { 2, 3, 1, 5 } ) << std::endl;
    std::cout << one_of( 4, { 2, 3, 1, 5 } ) << std::endl;
}    
#包括
#包括
#包括
模板
bool一个(常数T和值,标准::初始值设定项列表lst)
{
return std::(lst.begin(),lst.end(),[&](const T&x){return value==x;})中的任意_;
}    
int main()
{

std::cout你可以这样写

#include <iostream>
#include <algorithm>
#include <initializer_list>

template <class T>
bool one_of( const T &value, std::initializer_list<T> lst )
{
    return std::any_of( lst.begin(), lst.end(), [&]( const T &x ) { return value == x; } );
}    

int main()
{
    std::cout << one_of( 1, { 2, 3, 1, 5 } ) << std::endl;
    std::cout << one_of( 4, { 2, 3, 1, 5 } ) << std::endl;
}    
#包括
#包括
#包括
模板
bool一个(常数T和值,标准::初始值设定项列表lst)
{
return std::(lst.begin(),lst.end(),[&](const T&x){return value==x;})中的任意_;
}    
int main()
{

std::cout由于到目前为止,所有答案都具有线性搜索复杂性(
数组
向量
,等等),因此我建议使用
std::set
,它具有对数复杂度用于查找。例如:

#include <iostream>
#include <set>
int main()
    {
    std::set<int> set{ 5, 6, 1, 2 };
    std::cout << (set.count(6) > 0) << std::endl;
    std::cout << (set.count(3) > 0) << std::endl;
    return 0;
    }
#包括
#包括
int main()
{
std::集合{5,6,1,2};

std::cout 0)由于到目前为止,所有答案都具有线性搜索复杂性(
数组
向量
,等等),因此我建议使用具有对数复杂性的
std::set
进行查找。例如:

#include <iostream>
#include <set>
int main()
    {
    std::set<int> set{ 5, 6, 1, 2 };
    std::cout << (set.count(6) > 0) << std::endl;
    std::cout << (set.count(3) > 0) << std::endl;
    return 0;
    }
#包括
#包括
int main()
{
std::集合{5,6,1,2};


std::cout 0)比较中涉及的类型可能是有用的信息。integer(有符号/无符号)、char(有符号/无符号)等@AndyG。unsigned int似乎适用于方法比较中涉及的类型可能是有用的信息。integer(有符号/无符号)、char(有符号/无符号)等。@AndyG.Unsigned int似乎对meI很好!您认为,这个解决方案的性能如何?我问这个问题,因为在编译之前,所有索引都是已知的,所以我考虑的是基于模板的解决方案?@user1835313它是线性的,取决于列表中的项数。但是列表转换呢?@user1835313这取决于对象是否是复合的。我喜欢这样!在您看来,这个解决方案的性能如何?我问这个问题,因为在编译之前,所有索引都是已知的,所以我考虑的是基于模板的解决方案?@user1835313它是线性的,取决于列表中的项数。但是列表转换呢?@user1835313这取决于对象是否是复合的。构建集合是O(N log(N)),然后进行O(log(N))查找。Vlad的答案可以由编译器优化为O(N)@NathanOliver如果只用于一次检查,是的,你是对的,但是,如果在同一个对象上执行多次查找,那么
std::set
会更快。此外,由于OP声明,他需要查找的变量集是
静态常量,这意味着他可以创建一个单个
std::使用这些变量设置
实例,并继续搜索。从长远来看,这会更快。正确。我假设这是一个一劳永逸的情况。构建集合是O(N log(N)),然后您有一个O(log(N))查找。Vlad的答案可以由编译器优化为O(N)@NathanOliver如果只用于一次检查,是的,你是对的,但是,如果在同一个对象上执行多次查找,那么
std::set
会更快。此外,由于OP声明,他需要查找的变量集是
静态常量,这意味着他可以创建一个single
std::使用这些变量设置
实例,并继续搜索。从长远来看,这会更快。正确。我假设这是一种一劳永逸的情况。关于模板,我的观点是,在编译函数之前已知index1,index2,…可以“硬编码”对于这些值,是吗?啊,好的,我想你的意思是把这些常数放在变量模板中?是的,我想