使用emplace代替之前的construct object

use emplace instead of construct object before

本文关键字:construct object emplace 使用      更新时间:2023-10-16
#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;
}

我尝试使用放置而不是构造对象之前。但是出现了如下错误:

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

不知道我哪里做错了。

谢谢

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

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

两个问题:

  1. 基本上,您的类型需要支持以下内容,而您的Time类型不支持这些内容。要解决这个问题,你应该包含合适的3个参数构造函数。

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

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