简体   繁体   中英

priority queue error

I am having trouble, Whenever I try to compile this program I get an error in the contains function where it says q.pop. If that line is commented out it will compile fine, but it stills gives the wrong value. It is always false for some reason.

The error is:
priority_queue_demo.cpp: In function 'int main()': priority_queue_demo.cpp:54:12: error: invalid conversion from 'int' to 'const char*' [-fpermissive] /usr/lib/gcc/x86_64-redhat-linux/4.6.1/../../../../include/c++/4.6.1/bits/basic_string.tcc:214:5: error: initializing argument 1 of 'std::basic_string<_CharT, _Traits, _Alloc>::basic_string(const _CharT*, const _Alloc&) [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>]' [-fpermissive] priority_queue_demo.cpp: In function 'bool contains(const std::priority_queue<T>&, T) [with T = int, typename std::vector<T, std::allocator<_Tp1> >::value_type = int]': priority_queue_demo.cpp:43:47: instantiated from here priority_queue_demo.cpp:27:3: error: passing 'const std::priority_queue<int>' as 'this' argument of 'void std::priority_queue<_Tp, _Sequence, _Compare>::pop() [with _Tp = int, _Sequence = std::vector<int, std::allocator<int> >, _Compare = std::less<int>]' discards qualifiers [-fpermissive]

#include <iostream>
#include <string>
#include <sstream>

#include <queue>

using namespace std;

template <typename T>
ostream &operator <<(ostream &os, priority_queue<T> &q) {
        os << "{";
        while (!q.empty()) {
               os << q.top() << (!q.empty() ? ", " : "");
               q.pop();
        }

        os << "}";

        return os;
}

template <typename T>
bool contains(const priority_queue<T> &q, T val) {
        for (int i = 0; i < q.size(); i++) {
                if (q.top() == val) return true;
                q.pop();
        }

        return false;
}

int main() {
        cout << boolalpha;

        priority_queue<int> qi;

        for (int i = 0; i < 20; i++)
                qi.push(i);

        cout << qi << endl;

        cout << "qi contians 15: " << contains(qi, 15) << endl;
        cout << "qi contians 23: " << contains(qi, 23) << endl;

        const int ARR_SIZE = 4;
        string arr[ARR_SIZE] = {"cat", "dog", "cow", "elephant"};

        cout << endl;

        priority_queue<string> qs;

        for(int i = 0; i < ARR_SIZE; i++)
                qs.push(i);

        cout << qs << endl;

        while(!qs.empty()) {
                qs.pop();
                cout << qs << endl;
        }


        return 0;
 }

The problem is that you are passing a const reference to the queue, but pop() is obviously not a const operation, because it mutates the queue.

You should pass a non-const reference:

bool contains(priority_queue<T>& q, T val) { .... }

As you can see, you can't really check the contents of a queue without altering it.

The next error is that you are pushing an integer into a queue of std::string :

qi.push(i);

You probably mean

qi.push(arr[i]);

Note your overload of ostream& operator<< for the queue empties the queue, so any attempt to use contains after printing the queue to std::cout will yield false regardless of the original content of the queue.

See demo here .

You have several issues in the code:

priority_queue<string> qs;

    for(int i = 0; i < ARR_SIZE; i++)
            qs.push(i);  //<<<<Error, should be arr[i]

You really should push arr[i] not i since i is integer while it expects string.

You pass the priority_queue by reference to the overloaded << operator function,which removes all the elements in the priority_queue , so all your contains query will becomes false .

You also use const priority_queue<T>& in contains function, however, the pop() function is not const , which results in compile error as pointed out by juanchopanza.

I made a comment before but there are other problems with your code that don't fit in comments.

1) qs is a priority_queue<string> but you are pushing int s to it (in the line qs.push(i) ); I guess what you wanted to do was to push arr[i] . Hence, replace

qs.push(i);

with

qs.push(arr[i]);

2) The template function contains take a priority_queue<T> by const reference. That means you can't call non const methods on q . In particular, you cannot call q.pop() because pop() is a non const method. (Indeed, this method is expected to change the queue.) Therefore, replace

template <typename T>
bool contains(const priority_queue<T> &q, T val) {

with

template <typename T>
bool contains(priority_queue<T> &q, T val) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM