-2

I have come across a scenario where I am not able to find a way to push a value in the vector which is in the pair. I have made a priority_queue of pair<int, vector<string>> and I want to push values in the vector, for example:

priority_queue<pair<int, vector<string>> pq;

I want to know how to push elements in this priority queue.

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982

2 Answers2

0

You don't have access to the underlying container of a std::priority_queue, so you can't access any of the std::vector elements you store in it, other than the top() element, eg:

priority_queue<pair<int, vector<string>> pq;
pq.push(...);
pq.emplace(...);
...
// use pq.top().second as needed...

You can iterate the strings in the vector in the pair returned by top(), but you can't push_back() more string into the vector since the pair will be const, and thus the vector will also be const.

std::priority_queue is probably not the best container for you to use. Maybe a std::map would make more sense?

map<int, vector<string>> m;
m[1] = vector<string>{"I", "a"};
m[1].push_back(...);

Live Demo

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    Please do not recommend code to novice developers with multiple `operator[]` with the same agrument. `std::map::operator[]` is quite expensive operation. – Slava Aug 11 '20 at 20:54
  • @Slava then how do you expect to access an element of a given key multiple times? Are you thinking of something like `auto &elem = m[1]; elem = {"I", "a"}; elem.push_back(...);`? There is nothing wrong using using `operator[]` like this. If you want more efficiency, use `std::unordered_map` instead. – Remy Lebeau Aug 11 '20 at 20:58
  • Still missing the point that OP did not clarify, what he needs, and you are suggesting some data structures. Also, add to @Slava said, a suggestion for `using namespace std;` for a newbie-demo also not a good idea. – JeJo Aug 11 '20 at 21:01
  • 1
    Yes, something like that. And I may need `std::map` for a different reason, I do not think that is a good excuse to put unnecessary inefficiency into your code. Especially that using `operator[]` in the first place is not efficient either. – Slava Aug 11 '20 at 21:07
0

Thank you so much for the clarification. I was able to solve the problem and here is how I approached it.

typedef pair<int, vector<string>> pi;
class Compare {
    public:
    bool operator() (pair<int, vector<string>> a, pair<int, vector<string>> b) {
        return a.first > b.first;
    }  
};

class Solution {
public:
    
    string arrangeWords(string text) {
        int n = text.length();
        if(n==0)
            return "";
        text[0] = tolower(text[0]);
        unordered_map<int, vector<string>> m;
        string temp = "";
        for(int i = 0;i<n;i++) {
            if(text[i] != ' ') {
                temp += text[i];
            } else {
                m[temp.length()].push_back(temp);
                temp = "";
            }
            if(i==n-1) {
                m[temp.length()].push_back(temp);
            }
        }
        
        
        priority_queue<pi, vector<pi>, Compare> pq;
        for(auto x: m) {
            pq.push(make_pair(x.first, x.second));
        }
        
        string res = "";
        while(!pq.empty()) {
            auto t = pq.top(); pq.pop();
            int len = t.second.size();
            for(int i=0;i<len;i++) {
                res += t.second[i];
                res += " ";
            }
        }
        res[0] = toupper(res[0]);
        return res.substr(0, res.length()-1);
    }
};