【发布时间】:2011-09-06 19:12:16
【问题描述】:
回答后编辑:
< 应提供给std::map。有关最佳实践的更多信息,请参阅 James McNellis 的答案。
这个问题中包含的代码写得不好。只是因为我在玩 SPOJ 并且输入的数据是严格有效的。 std::string 方法是我一开始选择的,但结果证明它不够快。
谢谢。
我知道我不能直接将char[] 与地图一起使用,例如map<char[], int>。所以我把它放在一个班级里。但它仍然可以通过编译。怎么处理?
#include <stdio.h>
#include <map>
using namespace std;
class id {
public:
char v [30];
};
int main () {
map<id, int> m;
id a;
while (gets(a.v)) {
m[a]++;
}
return 0;
}
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_function.h: In member function ‘bool std::less<_Tp>::operator()(const _Tp&, const _Tp&) const [with _Tp = id]’:
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_map.h:418: instantiated from ‘_Tp& std::map<_Key, _Tp, _Compare, _Alloc>::operator[](const _Key&) [with _Key = id, _Tp = int, _Compare = std::less<id>, _Alloc = std::allocator<std::pair<const id, int> >]’
prog.cpp:15: instantiated from here
/usr/lib/gcc/i686-pc-linux-gnu/4.3.4/include/g++-v4/bits/stl_function.h:230: error: no match for ‘operator<’ in ‘__x < __y’
好像和比较有关系,但我还是一头雾水。
【问题讨论】: