Performance 有效搜索std::set中用于排序的元素以外的元素

Performance 有效搜索std::set中用于排序的元素以外的元素,performance,search,iterator,set,Performance,Search,Iterator,Set,考虑下面的set实现。在这里,我根据fScore参数订购了这套设备。如果要在“NodeData”中搜索具有特定“id”的元素,该怎么办。 我知道我可以使用“find”在集合中用O(logn)搜索“fScore”的任何元素。 有没有比线性搜索(在下面实现)更有效的搜索“id”(时间更短)的方法 #包括 #包括 #包括 #包括 #包括 #包括 结构节点数据{ int-id; int父代; 双fScore、gScore、hScore; std::载体nScores; 节点数据(常数int&idIn=0

考虑下面的set实现。在这里,我根据fScore参数订购了这套设备。如果要在“NodeData”中搜索具有特定“id”的元素,该怎么办。 我知道我可以使用“find”在集合中用O(logn)搜索“fScore”的任何元素。 有没有比线性搜索(在下面实现)更有效的搜索“id”(时间更短)的方法

#包括
#包括
#包括
#包括
#包括
#包括
结构节点数据{
int-id;
int父代;
双fScore、gScore、hScore;
std::载体nScores;
节点数据(常数int&idIn=0,
常量int和parentIn=-1,
常数双和鳍=1,
常量双精度和gIn=1,
常量double&hIn=1):id(idIn),父项(parentIn),
fScore(fIn)、gScore(gIn)、hScore(hIn)
{
}

BoOL运算符< P>你的<代码>设置经常改变吗?如果不是的话,你可以考虑建立一个“索引”-<代码> unOrdEdEdMult/<代码>。
维护这样的“索引”是有成本的你应该看看它是否比重载搜索速度更大。

不使用不同的/附加的数据结构,你就无法实现这一点。如果你使用C++,并且你可以使用一个库,你可以在

< p>中找到这个功能。

#include <iostream>
#include <algorithm>
#include <iterator>
#include <stdlib.h>
#include <vector>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>

struct NodeData{
    int id;
    int parent;
    double fScore, gScore, hScore;
    std::vector<double> nScores;

    NodeData(const int& idIn = 0,
             const int& parentIn = -1,
             const double& fIn = 1,
             const double& gIn = 1,
             const double& hIn = 1):id(idIn), parent(parentIn), 
             fScore(fIn), gScore(gIn), hScore(hIn)
    {
    }
    
    bool operator<(const NodeData& rhs) const {
        return fScore < rhs.fScore;
    }
};

class test
{
        public:
        typedef boost::multi_index_container<
            NodeData,
            boost::multi_index::indexed_by<
                boost::multi_index::ordered_unique<
                    boost::multi_index::identity<NodeData>
                >,
                boost::multi_index::ordered_unique<
                    boost::multi_index::member<NodeData, int, &NodeData::id>
                >
            >
        > NodeListType;
        NodeListType NodeList;

};

int main()
{
    test q;
    for(int i=1;i<=5;i++)
    {
        NodeData n1 = {i,1,double(i),1,1};
        q.NodeList.insert(n1);
    }
    test::NodeListType::iterator it;
    //search for node with fScore 1 - cost O(logn)
    it = q.NodeList.find(1);    
    if(it != q.NodeList.end()){
    std::cout<<"node with fScore 1 found. id = "<<it->id<<std::endl;
    }
    else{   
    std::cout<<"node not found = "<<std::endl;
    }
    //searching for id=3 on second index - cost O(logn) 
    int searchId = 3;
    test::NodeListType::nth_index<1>::type::iterator it1 = q.NodeList.get<1>().find(searchId);
    if(it1 != q.NodeList.get<1>().end()){
        std::cout <<"found node with id = "<<it1->id<<std::endl;    
    }   
}
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
结构节点数据{
int-id;
int父代;
双fScore、gScore、hScore;
std::载体nScores;
节点数据(常数int&idIn=0,
常量int和parentIn=-1,
常数双和鳍=1,
常量双精度和gIn=1,
常量double&hIn=1):id(idIn),父项(parentIn),
fScore(fIn)、gScore(gIn)、hScore(hIn)
{
}
布尔操作员,
boost::多索引::有序唯一<
boost::multi_index::member
>
>
>NodeListType;
节点列表类型节点列表;
};
int main()
{
测试q;

for(int i=1;我同意这个建议。我将此集合用于优先级队列。我无法拥有另一个数据结构,因为该集合在推送和弹出操作期间会发生很大变化。
#include <iostream>
#include <algorithm>
#include <iterator>
#include <stdlib.h>
#include <vector>
#include <boost/multi_index_container.hpp>
#include <boost/multi_index/identity.hpp>
#include <boost/multi_index/member.hpp>
#include <boost/multi_index/ordered_index.hpp>

struct NodeData{
    int id;
    int parent;
    double fScore, gScore, hScore;
    std::vector<double> nScores;

    NodeData(const int& idIn = 0,
             const int& parentIn = -1,
             const double& fIn = 1,
             const double& gIn = 1,
             const double& hIn = 1):id(idIn), parent(parentIn), 
             fScore(fIn), gScore(gIn), hScore(hIn)
    {
    }
    
    bool operator<(const NodeData& rhs) const {
        return fScore < rhs.fScore;
    }
};

class test
{
        public:
        typedef boost::multi_index_container<
            NodeData,
            boost::multi_index::indexed_by<
                boost::multi_index::ordered_unique<
                    boost::multi_index::identity<NodeData>
                >,
                boost::multi_index::ordered_unique<
                    boost::multi_index::member<NodeData, int, &NodeData::id>
                >
            >
        > NodeListType;
        NodeListType NodeList;

};

int main()
{
    test q;
    for(int i=1;i<=5;i++)
    {
        NodeData n1 = {i,1,double(i),1,1};
        q.NodeList.insert(n1);
    }
    test::NodeListType::iterator it;
    //search for node with fScore 1 - cost O(logn)
    it = q.NodeList.find(1);    
    if(it != q.NodeList.end()){
    std::cout<<"node with fScore 1 found. id = "<<it->id<<std::endl;
    }
    else{   
    std::cout<<"node not found = "<<std::endl;
    }
    //searching for id=3 on second index - cost O(logn) 
    int searchId = 3;
    test::NodeListType::nth_index<1>::type::iterator it1 = q.NodeList.get<1>().find(searchId);
    if(it1 != q.NodeList.get<1>().end()){
        std::cout <<"found node with id = "<<it1->id<<std::endl;    
    }   
}