Ventacademy
  • Socials
  • Business Fundamentals
    • Investments
  • High Income Skills
  • Programming
  • Software Review
  • Website Development
    • WordPress
    • Search Engine Optimization
    • Free Templates and Plugins
      • Support
  • Finance
No Result
View All Result
Knowledge base
No Result
View All Result
Home Support

C++ priority_queue: how to modify STL template to find & delete based on pair’s 1st element?

by
November 27, 2020
Share on FacebookShare on Twitter

I want to modify the <queue> STL so that it can find any element from the priority_queue, then delete it from the pq. The code is based on the answer by alexm from How to remove element not at top from priority_queue?

However, what I want to achieve is more complicated: the element in the priority_queue is pair<char, double>, and I want to achieve a min-heap. Thus, I modified the code as below:

#include <iostream>
#include <queue>
using namespace std;
using item_type = pair<char, double>;

template<typename T>
class custom_priority_queue : public priority_queue<T, vector<T>, greater<>> {
public:
    bool eleExist(const T& value) {
        auto it = lower_bound(this->c.begin(), this->c.end(), value);
        if (it != this->c.end())
            return true;
        else
            return false;
    }
    
    bool remove(const T& value) {
        auto it = lower_bound(this->c.begin(), this->c.end(), value);
        if (it != this->c.end()) {
            this->c.erase(it);
            make_heap(this->c.begin(), this->c.end(), this->comp);
            return true;
        }
        else {
            return false;
        }
    }
};

int main() {
    custom_priority_queue<item_type> pq;

    // use list to fill priority_queue later
    // larger number means higher priority
    initializer_list<item_type> il {
        {'A', 10},
        {'B', 12},
        {'H', 12}
    };
    // iterator through list to fill into priority_queue
    for (const auto& item : il) {
        pq.emplace(item);
    }
    
    if (pq.eleExist({'A', 10})) {
        cout << "A exists. Thus remove it" << endl;
        pq.remove({'A', 10});
    }
    else
        cout << "A doens't exist" << endl;

    // print priority_queue
    cout << "Currently in priority queue:" << endl;
    while (!pq.empty()) {
        cout << pq.top().first << ": " << pq.top().second << endl;
        pq.pop();
    }
    cout << endl;

    return 0;
}

Unfortunately, I can only use eleExist and remove functions with the full pair in my main function. What I really want to achieve is, to find element in priority_queue based on the 1st element in pair, and is able to delete the pair element based on the eleExist function. How do I modify my derived class custom_priority_queue to achieve that?

Thanks.

Newest questions tagged stl – Stack Overflowc++, stl, priority-queue, std-pair

Related Posts

c++ – Why do STL algorithms require two iterators explicitly?

April 21, 2021

c++ – Can insert into vector Out of order?

April 21, 2021

c++ – Sorting the std::list of structures by rule

April 21, 2021

c++ – What does this line with comment mean?

April 21, 2021
Next Post
Settings for Success: Landscape Photography with Marc Muench

Settings for Success: Landscape Photography with Marc Muench

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

Economic Data, Brexit, and Capitol Hill to drive the Majors

December 18, 2020

Shoprite looks set to completely dispose of its Nigerian subsidiary

April 7, 2021

Subscribe.

Trending.

No Content Available

About Us

Knowledge base

We bring you the best technology news, Finance news and lots more. you can download premium themes, plugins scripts, courses and lots more here.

Follow Us

Categories

  • All
  • Books
  • Business Fundamentals
  • Finance
  • Free Course
  • Free download
  • Free Templates and Plugins
  • Health
  • High Income Skills
  • Investments
  • NSFW
  • Photography
  • Search Engine Optimization
  • Software Review
  • Support
  • Technical Analysis
  • Website Development
  • Wordpress
  • World

Activity

FREEDOWNLOAD

Automatically generates native code for adding splash screens in Android and iOS

Vikinger 1.5.1 – BuddyPress and GamiPress Social Community

EduMall 2.7.1 – Professional LMS Education Center WordPress Theme

Admin Columns Pro – BuddyPress Addon 1.5.1

Microsoft Dynamics AX 2012 R3 Programming – Level 3 – Download Udemy Courses For Free

YITH Deals for WooCommerce Premium 1.0.21

Front End Web Design UX, CSS, FLEXBOX & SASS Complete Course – Download Udemy Courses For Free

Learn Python 3 programming | Become job ready using Pycharm – Download Udemy Courses For Free

Oxygen 5.9 – WooCommerce WordPress Theme

Preston 1.2.1 – Real Estate WordPress Theme

Support Board 3.2.4 – WordPress Chat Plugin

YITH WooCommerce Customize My Account Page 3.1.1 Nulled

YITH Cost of Goods for WooCommerce 1.2.13 Nulled

BoomBox 2.7.7 Nulled – Viral Magazine WordPress Theme

Calendarista 13.3 – Premium WordPress Booking Plugin

  • Socials
  • About
  • Advertise
  • Privacy & Policy
  • Contact

© 2020 Ventacademy

No Result
View All Result
  • Socials
  • Business Fundamentals
    • Investments
  • High Income Skills
  • Programming
  • Software Review
  • Website Development
    • WordPress
    • Search Engine Optimization
    • Free Templates and Plugins
      • Support

© 2020 Ventacademy