简体   繁体   English

如何在 c++ 对中的向量内推送一个值?

[英]How to push a value inside a vector which in a c++ pair?

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 .我遇到了一种情况,我无法找到一种方法来推送pair中的vector中的值。 I have made a priority_queue of pair<int, vector<string>> and I want to push values in the vector , for example:我已经创建了pair<int, vector<string>>priority_queue ,我想在vector中推送值,例如:

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

I want to know how to push elements in this priority queue.我想知道如何在这个优先级队列中推送元素。

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:您无权访问std::priority_queue的底层容器,因此您无法访问存储在其中的任何std::vector元素,除了top()元素,例如:

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 .您可以在top()返回的pair中迭代vector中的字符串,但不能将更多字符串push_back()放入vector中,因为该pair将是const ,因此vector也将是const

std::priority_queue is probably not the best container for you to use. std::priority_queue可能不是您使用的最佳容器。 Maybe a std::map would make more sense?也许std::map会更有意义?

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

Live Demo现场演示

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);
    }
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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