简体   繁体   中英

Ask user string input and store in priority queue c++

How do i ask user a string input and store them in a priority queue? I only know how to ask int data type using priority queue

priority_queue<string> q;
string s;
cin >> s;
q.push(s);

You can use std::getline like so:

#include <iostream>
#include <string>

int main()
{
    while(true)
    {
        std::string s;
        std::cout << "enter a string: ";
        std::getline(std::cin, s);
        std::cout << "You entered: " << s << std::endl;
    }

    return 0;
}

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