c++ - 如何使用枚举将 char 值映射到 int

标签 c++ c++11 c++14

我正在尝试使用枚举将字符串中的一些字符映射到一些整数值。请告诉我哪里出错了?

enum moves{U,R,D,L};
class Solution {
public:
    bool judgeCircle(string moves) {   

// moves is a string having values like ULLDDRR, ULRD, UULLDDRR 
        int X[] = {0,1,0,-1};
        int Y[] = {1,0,-1,0};

// while iterating the string if I get a 'U' , I want to use it as an index 
//with  U representing the 0th index, R as index=1 and so on.. as specified 
 //in the enum

        int x=0 , y=0;
        enum moves ind;
        for( int i = 0 ; i < moves.length() ; i++ ) {
            ind = moves[i];  // but this line here gives error
            x += X[ind];
            y += Y[ind];
        }

        if(!x && !y)
            return true;
        else
            return false;
    }
};

最佳答案

我会放弃使用 enum 的想法,因为我觉得它对实际问题毫无用处——将字符映射到导航移动。为此,我会使用 std::mapstd::unordered_map . (考虑到只有 4 个条目,性能差异可能很难衡量。)

当我准备示例代码时,πάντα ῥεῖ给出了类似的提示。不过,我什至建议将 x 和 y 的 Action 捆绑在一起:

#include <map>
#include <iomanip>
#include <iostream>

// bundle x and y for a move (which needs both of them)
struct Move {
  int dx, dy;
};

// a type to map chars to moves
using MoveMap = std::map<char, Move>;

// a pre-defined move map
static const MoveMap mapMoves = {
    { 'U', { 0, 1 } },
    { 'R', { 1, 0 } },
    { 'D', { 0, -1 } },
    { 'L', { -1, 0 } }
};

/* a function to use move map
 *
 * id ... one of U R D L
 * x, y ... coordinates (update)
 * return: true if successful, (false e.g. for wrong id)
 */
bool move(char id, int &x, int &y)
{
  const MoveMap::const_iterator iter = mapMoves.find(id);
  return iter != mapMoves.end()
    ? x += iter->second.dx, y += iter->second.dy, true
    : false;
}

// check it out:

int main()
{
  int x = 0, y = 0;
  const char test[] = "ULLDDRR, ULRD, UULLDDRR";
  for (char id : test) {
    std::cout << "(" << x << ", " << y << "): "
      << "Move '" << id << "' -> ";
    if (move(id, x, y)) {
      std::cout << "(" << x << ", " << y << ")\n";
    } else std::cout << "failed\n";
  }
  return 0;
}

输出:

(0, 0): Move 'U' -> (0, 1)
(0, 1): Move 'L' -> (-1, 1)
(-1, 1): Move 'L' -> (-2, 1)
(-2, 1): Move 'D' -> (-2, 0)
(-2, 0): Move 'D' -> (-2, -1)
(-2, -1): Move 'R' -> (-1, -1)
(-1, -1): Move 'R' -> (0, -1)
(0, -1): Move ',' -> failed
(0, -1): Move ' ' -> failed
(0, -1): Move 'U' -> (0, 0)
(0, 0): Move 'L' -> (-1, 0)
(-1, 0): Move 'R' -> (0, 0)
(0, 0): Move 'D' -> (0, -1)
(0, -1): Move ',' -> failed
(0, -1): Move ' ' -> failed
(0, -1): Move 'U' -> (0, 0)
(0, 0): Move 'U' -> (0, 1)
(0, 1): Move 'L' -> (-1, 1)
(-1, 1): Move 'L' -> (-2, 1)
(-2, 1): Move 'D' -> (-2, 0)
(-2, 0): Move 'D' -> (-2, -1)
(-2, -1): Move 'R' -> (-1, -1)
(-1, -1): Move 'R' -> (0, -1)
(0, -1): Move '' -> failed

Live Demo on coliru

关于c++ - 如何使用枚举将 char 值映射到 int,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56403856/

相关文章:

c++ 公历和儒略历继承

c++ - 没有 GL_COLOR_INDEX 的 glBitmap()

C++ 通用包装器类,它为某些函数添加了额外的处理

c++ - 在 C++ 中的 lambda 之外访问 lambda 捕获初始化变量

c++ - 创建一个实现选项卡并可以在 QMainWindow 中用作 "central widget"的类

c++ - 用原语 union 公共(public)初始序列

c++ - 如何使用 is_base_of 专门化模板而不与主模板混淆?

c++ - C++ 中多个值的多线程原子存储/加载

c++ std::enable_if 约束变体和问题

c++ - Boost 仅序列化 std::wstring 的第一个字符