Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/141.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 搜索集合中项目的一部分_C++_Key_Set_Std Pair - Fatal编程技术网

C++ 搜索集合中项目的一部分

C++ 搜索集合中项目的一部分,c++,key,set,std-pair,C++,Key,Set,Std Pair,是否有方法搜索集合中项目的某一部分?我有一组pairstd::set,希望通过给定的double搜索一个项目。有没有什么方法可以方便地执行此操作,而不是手动创建一对并搜索它?由于集合不是根据搜索条件排序的,因此可以与谓词一起使用,该谓词只检查对的第一个元素。这将向第一个匹配元素返回一个迭代器,并附带有关比较浮点数是否相等的常见警告 double value = 42.; auto it = std::find_if(the_set.begin(), the_set.end(),

是否有方法搜索集合中项目的某一部分?我有一组pair
std::set
,希望通过给定的double搜索一个项目。有没有什么方法可以方便地执行此操作,而不是手动创建一对并搜索它?

由于集合不是根据搜索条件排序的,因此可以与谓词一起使用,该谓词只检查对的
第一个
元素。这将向第一个匹配元素返回一个迭代器,并附带有关比较浮点数是否相等的常见警告

double value = 42.;
auto it = std::find_if(the_set.begin(), the_set.end(), 
                       [&value](const std::pair<double, unsigned>& p) { return p.first==value; }); 
double value=42。;
auto it=std::find_if(the_set.begin(),the_set.end(),
[&value](const std::pair&p){return p.first==value;});

由于集合不是根据搜索条件排序的,因此可以与谓词一起使用,该谓词只检查配对的
第一个
元素。这将向第一个匹配元素返回一个迭代器,并附带有关比较浮点数是否相等的常见警告

double value = 42.;
auto it = std::find_if(the_set.begin(), the_set.end(), 
                       [&value](const std::pair<double, unsigned>& p) { return p.first==value; }); 
double value=42。;
auto it=std::find_if(the_set.begin(),the_set.end(),
[&value](const std::pair&p){return p.first==value;});

我不确定这是否是您想要的:

#include <iostream>
#include <set>
#include <utility>
#include <algorithm>

using namespace std;


struct Finder{
    template<typename Value>
    bool operator()(const Value& first, const Value& v) const{
        return first == v;      
    }
};

template <typename Value>
struct FirstValueValue{
    FirstValueValue(const Value& value): value(value){};
    template<typename Pair>
    bool operator()(const Pair& p) const{
        return p.first == value;
    }
    Value value;
};

int main(int argc, char *argv[]) {
    typedef std::set<std::pair<double,unsigned int> > SetOfPairs;
    SetOfPairs myset;
    myset.insert(std::make_pair(2.0,1));
    myset.insert(std::make_pair(5.7,2));


    Finder finder;
    double v = 2.0;
    for(SetOfPairs::iterator it = myset.begin(); it != myset.end(); it++){

        if( finder(it->first,v) ){
            cout << "found value " << v << std::endl;
        }
    }

    FirstValueValue<double> find_double_two(2.0);

    myset.insert(std::make_pair(2.0,100));
    unsigned int count =    std::count_if(myset.begin(),myset.end(),find_double_two);
    cout << "found " << count << " occurances of " << find_double_two.value;

}
我不知道您的需求是什么,也不知道是否允许使用boost库,但如果您必须大量索引这对库的一部分,您可以研究boost多索引


希望这有帮助。祝你好运

我不确定这是否是您想要的:

#include <iostream>
#include <set>
#include <utility>
#include <algorithm>

using namespace std;


struct Finder{
    template<typename Value>
    bool operator()(const Value& first, const Value& v) const{
        return first == v;      
    }
};

template <typename Value>
struct FirstValueValue{
    FirstValueValue(const Value& value): value(value){};
    template<typename Pair>
    bool operator()(const Pair& p) const{
        return p.first == value;
    }
    Value value;
};

int main(int argc, char *argv[]) {
    typedef std::set<std::pair<double,unsigned int> > SetOfPairs;
    SetOfPairs myset;
    myset.insert(std::make_pair(2.0,1));
    myset.insert(std::make_pair(5.7,2));


    Finder finder;
    double v = 2.0;
    for(SetOfPairs::iterator it = myset.begin(); it != myset.end(); it++){

        if( finder(it->first,v) ){
            cout << "found value " << v << std::endl;
        }
    }

    FirstValueValue<double> find_double_two(2.0);

    myset.insert(std::make_pair(2.0,100));
    unsigned int count =    std::count_if(myset.begin(),myset.end(),find_double_two);
    cout << "found " << count << " occurances of " << find_double_two.value;

}
我不知道您的需求是什么,也不知道是否允许使用boost库,但如果您必须大量索引这对库的一部分,您可以研究boost多索引


希望这有帮助。祝你好运

假设您的集合使用标准比较运算符定义集合内的排序,并且假设
double
元素在对中位于第一位,则集合内的排序顺序主要由对中的
double
元素定义(只有共享
double
元素的对才会在订购时考虑第二个元素)

因此,您需要做的唯一一件事就是定义一个比较运算符,用于在两个方向上比较具有单双精度的对(注意,我在几个地方使用了C++11语法):

如果使用优化编译,则
doublecmp()
的实例化是不可操作的

您将发现一个完全工作的代码示例

为什么这样做?

假设您的集合声明为
set
,则使用默认的比较运算符
less
,这与标准的
运算符相同,因为您的集合使用标准比较运算符定义集合内的顺序,并且假设
double
元素在对中位于第一位,则排序集合内的顺序主要由配对的
double
元素定义(并且只有共享
double
元素的配对才会考虑第二个元素的顺序)

因此,您需要做的唯一一件事就是定义一个比较运算符,用于在两个方向上比较具有单双精度的对(注意,我在几个地方使用了C++11语法):

如果使用优化编译,则
doublecmp()
的实例化是不可操作的

您将发现一个完全工作的代码示例

为什么这样做?

考虑到你的集合被声明为
set
,你正在使用默认的比较运算符
less
,它与标准的
运算符+1相同,非常好。我甚至没有想到
equal\u range
会起作用(注意事项除外)。+1,非常好。我甚至没有想到
equal\u range
会起作用(注意事项除外)。
std::equal_range(begin(s),end(s),d,doublecmp());
pair<it_t,it_t> find_range(set_t &s, double d, double epsilon)
{
   return {std::lower_bound(begin(s),end(s),d - epsilon,doublecmp()),
           std::upper_bound(begin(s),end(s),d + epsilon,doublecmp())};
}