之前使用emplace代替构造对象

时间:2014-07-22 15:03:33

标签: 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代替构造对象。 但得到的错误如:

/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&)

不知道我哪里做错了。

由于

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:2)

两个问题:

  1. 基本上,您的类型需要支持以下Time类型不支持的内容。要修复它,您应该包含适当的3参数构造函数。

    new Time(3, 2, 40);
    
  2. 您的比较器应该更多const

    bool operator() (Time const & t1, Time const & t2) const {