我想在std::map容器中使用std::tm()作为键。但是当我尝试编译它时,会出现许多(10个)错误。
例如:
1.
error C2784:'bool std :: operator <(const std :: basic_string <_ Elem,_ Traits,_ Alloc>&,const _Elem *)':无法推断出 'const std :: basic_string <_ Elem,_ Traits,_ Alloc>&' 的模板参数,来自 'const tm' c:\ program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125
2.
error C2784:'bool std :: operator <(const _Elem *,const std :: basic_string <_ Elem,_ Traits,_ Alloc>&)':无法推断出 'const _Elem *' 的模板参数,来自 'const tm' c:\ program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125
3.
error C2784:'bool std :: operator <(const std :: vector <_ Ty,_ Ax>&,const std :: vector <_ Ty,_ Ax>&)':无法推断出 'const std :: vector <_ Ty,_ Ax>&' 的模板参数,来自 'const tm' c:\ program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125
所有这些是否意味着我“只需”创建一个函数对象,比较两个std :: tm,因为没有为此定义标准比较?还是有其他技巧?(或者甚至对我来说是不可能的?^^)
代码:
例如:
1.
error C2784:'bool std :: operator <(const std :: basic_string <_ Elem,_ Traits,_ Alloc>&,const _Elem *)':无法推断出 'const std :: basic_string <_ Elem,_ Traits,_ Alloc>&' 的模板参数,来自 'const tm' c:\ program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125
2.
error C2784:'bool std :: operator <(const _Elem *,const std :: basic_string <_ Elem,_ Traits,_ Alloc>&)':无法推断出 'const _Elem *' 的模板参数,来自 'const tm' c:\ program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125
3.
error C2784:'bool std :: operator <(const std :: vector <_ Ty,_ Ax>&,const std :: vector <_ Ty,_ Ax>&)':无法推断出 'const std :: vector <_ Ty,_ Ax>&' 的模板参数,来自 'const tm' c:\ program files (x86)\microsoft visual studio 10.0\vc\include\xfunctional 125
所有这些是否意味着我“只需”创建一个函数对象,比较两个std :: tm,因为没有为此定义标准比较?还是有其他技巧?(或者甚至对我来说是不可能的?^^)
代码:
#include <map>
#include <ctime>
#include <string>
int main()
{
std::map<std::tm, std::string> mapItem;
std::tm TM;
mapItem[TM] = std::string("test");
return 0;
};
unordered_map
仍然需要它。因为两种类型的 map 都需要检查键。它们必须检查 唯一 的键。 - Nawaztm_sec
不能相同?01:30:05
和03:49:05
都有相同的tm_sec
。你可以将tm
转换为秒数,然后仅比较秒数。或者如果你担心该值会超过数据类型(int
、long long
或其他你使用的类型)的最大值,则可以将tm
转换为小时和秒数。例如,5 天 4 小时 30 分钟 7 秒
将变为:(5 x 24 + 4)
小时和30 x 60 + 7
秒。如果这样做,那么比较就很容易了。 - Nawaz