使用emplace代替之前的construct object
use emplace instead of construct object before
#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)
{}
};
两个问题:
-
基本上,您的类型需要支持以下内容,而您的
Time
类型不支持这些内容。要解决这个问题,你应该包含合适的3个参数构造函数。new Time(3, 2, 40);
-
您的比较器应该更
const
:bool operator() (Time const & t1, Time const & t2) const {
相关文章:
- 当覆盖存在时调用基本虚拟"binded to object"函数
- "Unable to start debugging. No process is associated with this object." - 在Visual Studio Code中使用GDB
- C++将派生类转换为基类时'object slicing'期间发生的情况
- 如何理解 C++17 标准参考中的 [intro.object]/3? N4659 的
- "Cannot convert Python object argument to type '<typename>'" - 使用 Cython 包装 C++ 类时出错
- 使用 object 中的方法调用带有 std::bind 和 std::function.target 的 C 样式函数
- "Called Object Type is Not a Function or Pointer" 与 typedef 和类
- C++线程"Call to non-static member function without an object argument"
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- std::allocator_traits::construct调用了错误的构造函数
- 当两个成员位于同一类中时出错"a nonstatic member reference must be relative to a specific object"
- Nan::ObjectWrap to Napi::Object Wrap 无法访问 JavaScript 中的属性
- 现代C++会支持 C# "Object Initalizer"语法吗?
- 指向对象生存期之外的已分配内存的指针是"invalid pointer[s]"还是"pointer[s] to an object"?
- C++ Equivalent of Java Map<String, Object>
- 将赋值运算符实现为"destroy + construct"是否合法?
- WebKit2GTK+ "window-object-cleared"未清除
- 返回 *&object 时是否允许复制/移动省略?
- Construct unordered_map with a value_type of unique_ptr
- 使用emplace代替之前的construct object