【问题标题】:stl priority_queue of C++ with structstl priority_queue of C++ with struct
【发布时间】:2022-08-03 18:55:32
【问题描述】:

我们如何将 STL priority_queue 用于 struct ? 任何插图pushing 和 popping ,其中 struct 有多种数据类型?
说:struct thing { int a; char b;} glass[10];
现在我如何使用'int a'将这个结构放在priority_queue上进行排序?

【问题讨论】:

    标签: c++ struct priority-queue


    【解决方案1】:

    这是对 your original question, which you deleted 的略微修改的答案,原因不明。原始文件包含足够的信息让您弄清楚这一点,但这里是这样的:提供一个使用 int 进行比较的小于比较。

    您需要做的就是提供一个仿函数,该仿函数通过严格的弱排序实现小于比较,或者为您的类提供一个小于运算符来实现相同的操作。该结构满足要求:

    struct thing
    {
        int a;
        char b;
        bool operator<(const thing& rhs) const
        {
            return a < rhs.a;
        }
    };
    

    然后

    std::priority_queue<thing> q;
    thing stuff = {42, 'x'};
    q.push(stuff);
    q.push(thing{4242, 'y'}); // C++11 only
    q.emplace(424242, 'z'); // C++11 only    
    thing otherStuff = q.top();
    q.pop();
    

    【讨论】:

    • 谢谢 ^_^ & 最后一件事:我将如何将 say (3,a) 推送到队列 direclty?我不知道如何把 (3,a) 放到 'thing stuff=...' 。
    • 在 c++11 中,您可以说 q.push(thing{42, 'x'})q.emplace(42, 'x')。如果你没有 C++11 支持,你需要给thing一个构造函数。
    • 参数必须是 const 引用吗?为什么我们不能简单地做 bool operator<(thing rhs) ?
    • q.emplace(424242, 'z'); // C++11 only 行是错误的,它会一直抛出错误直到 c++20。为此,结构需要有一个显式构造函数。
    【解决方案2】:

    thing 重载 &lt; 运算符:

    struct thing
    {
        int a;
        char b;
    
        bool operator<(const thing &o) const
        {
            return a < o.a;
        }
    };
    
    priority_queue<thing> pq;
    
    thing t1, t2, t3;
    
    // ...
    
    pq.push(t1);
    pq.push(t2);
    
    // ...
    
    t3 = pq.top();
    pq.pop();
    

    【讨论】:

      【解决方案3】:

      您需要实现一个比较函数或重载运算符来告诉优先级队列您想要对自定义数据进行排序的顺序。当优先级队列对您的数据进行排序时,它将需要一种方法来知道如何在它们之间进行比较。您必须通过将函数传递给自定义数据类或结构中的优先级队列或重载运算符来指定它。

      你可以查看this的回答。 This 可能对您有帮助。我试图解释为自定义数据类型使用优先级队列的多种方法。

      【讨论】:

        【解决方案4】:

        你可以这样做!

        struct example{
           int height;
           int weight;
        };
        
        struct comp{
                bool operator()(struct example a, struct example b){
                 //Sorting on the basis of height(Just for example)
                    return (a.height > b.height);
                }
            };
        
        // And here comes your priority queue
         priority_queue<struct example, vector<struct example>, comp> pq;
        struct example your_obj;
        pq.push(your_obj);
        
        

        【讨论】:

          【解决方案5】:

          结构中的优先级队列可以通过两种方式实现:

          1. 重载 < 运算符
          2. 传递布尔比较运算符 包裹在结构中。

            让我们采用模型和价格均为整数的结构汽车,并根据顶部的最高价格构建优先级队列。

            struct Car
            {
                int m;
                int p;
                void set(int a,int b){
                    m =a,p = b;
                }
            };
            

            对于方法 1,我们需要这样做:

            bool operator < (const Car& c1,const Car& c2){
                return c1.p < c2.p;
            }
            

            对于方法 2,我们需要传递这个结构:

            struct Cmp{
                bool operator()(const Car& c1,const Car& c2){
                        return c1.p < c2.p;
                }
            };
            

            用于说明的完整代码:::

            #include <iostream>
            #include<unordered_map>
            #include<string>
            #include<queue>
            #include<vector>
            using namespace std;
            
            struct Car
            {
                int m;
                int p;
                void set(int a,int b){
                    m =a,p = b;
                }
            };
            struct Car cT[50];
            int cI=0;
            void printC(){
                for(int i=0;i<cI;i++){
                    cout <<" car model: " << cT[i].m << " price: " <<cT[i].p <<endl;
                }
            }
            
            bool operator < (const Car& c1,const Car& c2){
                return c1.p < c2.p;
            } //Method 1
            
            struct Cmp{
                bool operator()(const Car& c1,const Car& c2){
                        return c1.p < c2.p;
                }
            }; //Method 2
            
            void pushQ(priority_queue<Car,vector<Car>,Cmp>& Q){
             for(int i=0;i<cI;i++){
                    Q.push(cT[i]);
                    cout <<"Queue Push car model: " << cT[i].m << " price: " <<cT[i].p <<endl;
                } 
            };
            
            void printQ(priority_queue<Car,vector<Car>,Cmp> Q){
                while(!Q.empty()){
                    Car c = Q.top();
                    Q.pop();
                    cout <<" car model: " << c.m << " price: " <<c.p <<endl;
                }
            }
            int main() {
                // Write C++ code here
                
                // priority_queue<Car> Q; // #Method 1 
                priority_queue<Car,vector<Car>,Cmp> Q;// #Method 2 
                cT[cI++].set(4,5);
                cT[cI++].set(34,4);
                cT[cI++].set(43,6);
                cT[cI++].set(41,15);
                pushQ(Q);
                printQ(Q);
               // printC();
                // cout << "Hello world!" <<f1;
            
                return 0;
            }
            

            输出将是:::

            car model: 41 price: 15
            car model: 43 price: 6
            car model: 4 price: 5
            car model: 34 price: 4
            

          【讨论】:

          • bool operator &lt; (const Car&amp; c1,const Car&amp; c2){ return c1.p &lt; c2.p; } 这里的优先队列顺序为 Car 2 如果 return true 将优先于 Car1 它将在优先列表的上方。 &lt; (const Car&amp; c1,const Car&amp; c2) ==> 如果 c2 的真实优先级高于 c1。由于优先级队列默认实现为 Max Heap Fashion。
          猜你喜欢
          • 2018-11-16
          • 1970-01-01
          • 2021-09-26
          • 1970-01-01
          • 1970-01-01
          • 2011-02-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多