c++ - 在之前使用 emplace 而不是构造对象

标签 c++ c++11 queue

#include <iostream>
#include <queue>
#include <iomanip>

using namespace std;

struct Time {
    int h;
    int m;
    int s;
};

class CompareTime {
public:
    bool operator() (Time& t1, Time& t2) {
        if (t1.h < t2.h) return true;
        if (t1.h == t2.h && t1.m < t2.m) return true;
        if (t1.h == t2.h && t1.m == t2.m && t1.s < t2.s) return true;
        return false;
    }
};

int main() {
    priority_queue<Time, vector<Time>, CompareTime> pq;
    pq.emplace(3,2,40);
    pq.emplace(3,2,26);
    pq.emplace(5,16,13);
    pq.emplace(5,14,20);

    while(!pq.empty()) {
        Time t2 = pq.top();
        cout << setw(4) << t2.h << " " << setw(3) << t2.m << " " << setw(3) <<
            t2.s << endl;
        pq.pop();
    }
    return 0;

}

我之前尝试使用 emplace 而不是 construct object。 但出现如下错误:

/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h: In member function âvoid __gnu_cxx::new_allocator<_Tp>::construct(_Tp*, _Args&& ...) [with _Args = int, int, int, _Tp = Time]â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:95:   instantiated from âvoid std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = int, int, int, _Tp = Time, _Alloc = std::allocator<Time>]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_queue.h:527:   instantiated from âvoid std::priority_queue<_Tp, _Sequence, _Compare>::emplace(_Args&& ...) [with _Args = int, int, int, _Tp = Time, _Sequence = std::vector<Time, std::allocator<Time> >, _Compare = CompareTime]â
time_queue_test.cpp:25:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:111: error: new initializer expression list treated as compound expression
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/ext/new_allocator.h:111: error: no matching function for call to âTime::Time(int)â
time_queue_test.cpp:7: note: candidates are: Time::Time()
time_queue_test.cpp:7: note:                 Time::Time(const Time&)
In file included from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/vector:69,
                 from /usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/queue:62,
                 from time_queue_test.cpp:2:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc: In member function âvoid std::vector<_Tp, _Alloc>::_M_insert_aux(__gnu_cxx::__normal_iterator<typename std::_Vector_base<_Tp, _Alloc>::_Tp_alloc_type::pointer, std::vector<_Tp, _Alloc> >, _Args&& ...) [with _Args = int, int, int, _Tp = Time, _Alloc = std::allocator<Time>]â:
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:100:   instantiated from âvoid std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = int, int, int, _Tp = Time, _Alloc = std::allocator<Time>]â
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/stl_queue.h:527:   instantiated from âvoid std::priority_queue<_Tp, _Sequence, _Compare>::emplace(_Args&& ...) [with _Args = int, int, int, _Tp = Time, _Sequence = std::vector<Time, std::allocator<Time> >, _Compare = CompareTime]â
time_queue_test.cpp:25:   instantiated from here
/usr/lib/gcc/x86_64-redhat-linux/4.4.7/../../../../include/c++/4.4.7/bits/vector.tcc:314: error: no matching function for call to âTime::Time(int, int, int)â
time_queue_test.cpp:7: note: candidates are: Time::Time()
time_queue_test.cpp:7: note:                 Time::Time(const Time&)

不知道我哪里做错了。

谢谢

最佳答案

接收3个int并初始化成员的构造函数,不是自动生成的,需要实现。

struct Time {
    int h;
    int m;
    int s;

    Time(int hv, int mv, int sv)
      : h(hv), m(mv), s(sv)
    {}
};

关于c++ - 在之前使用 emplace 而不是构造对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24890958/

相关文章:

android - Android中的原生库加载断点

c++ - 关于::(scope_resolution_operator) 用法的疑问

c++ - 为什么 std::vector 和 std::string 的比较运算符定义为模板函数?

c++ - 对和字符串流

Java从队列中检索所有元素,直到队列为空

c++ - 无法理解 CUDA 内核启动的行为

c++ - 定义 BIT0、BIT1、BIT2 等而不使用#define

c++ - 设置获取元素的位置

algorithm - 跟踪 FIFO 队列中的最大元素

c++ - 按特定顺序打印队列