在C++中使用hash_map [关闭]

cclgggtu  于 2024-01-09  发布在  其他
关注(0)|答案(1)|浏览(163)

**已关闭。**此问题需要debugging details。目前不接受回答。

编辑问题以包括desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem。这将帮助其他人回答问题。
8天前关闭
Improve this question
我已经在C++中创建了一个map,使用以下结构存储不同棋子的方向:

  1. map<Piece, vector<tuple<Offset, Type, Color>>> PIECE_DIRECTIONS;

字符串
当尝试向Map添加元素时,例如:

  1. PIECE_DIRECTIONS[Pawn].push_back(tuple<Up, Push, Red>);
  2. PIECE_DIRECTIONS[Soldier].push_back(tuple<Up, Push, Red>);


我收到错误消息“This declaration has no storage class or type specifier”。我该如何解决这个问题?是否有其他方法可以实现相同的目标,例如使用vector或数组而不是map?
与数组和向量相比,访问map中的元素更快吗?在我的特定情况下,访问元素的速度至关重要,插入和删除之类的方法并不是一个重要的考虑因素。

6pp0gazn

6pp0gazn1#

你会得到这样的东西(我还没有添加向量,但这增加了额外的{}级别用于初始化)。我只是想给你一个给予的想法。;)

  1. #include <string>
  2. #include <map>
  3. enum class Piece
  4. {
  5. Pawn,
  6. Bishop,
  7. Rook
  8. };
  9. enum class Color
  10. {
  11. Black,
  12. White
  13. };
  14. struct Direction
  15. {
  16. int horizontal;
  17. int vertical;
  18. };
  19. struct Movement
  20. {
  21. Color color;
  22. Direction direction;
  23. };
  24. int main()
  25. {
  26. std::map<Piece,Movement> moves{
  27. { Piece::Pawn, // <- key of your map, e.g. a Piece enum value
  28. {
  29. Color::White, // First member of Movement, e.g. Color enum value
  30. {0,1} // Second member of Movement, Direction {0 horizontal, 1 vertical)
  31. }
  32. },
  33. { Piece::Bishop, { Color::Black, {8,8} } }
  34. };
  35. return 0;
  36. }

字符串

展开查看全部

相关问题