1 Question: How to map char values to int using enum

question created at Sat, Jun 1, 2019 12:00 AM

I am trying to map some chars in a string to some integer values using enum. Please tell where am I going wrong?

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;
    }
};
0
  1. What exactly are you trying to do? Could you post the full code?
    2019-06-01 04:56:13Z
  2. Do not define enum if you want to use it as array. If you want to use as enum and still want to access as array here is answer: stackoverflow.com/questions/321801/enum-c-get-by-index
    2019-06-01 05:02:18Z
  3. What you are trying to do cannot be done, and is sort of weird enough that I can't recommend a better option because I don't know why you were trying to do it this way in the first place.
    2019-06-01 05:10:01Z
  4. To map character values to ints use a std::map<char,int> X = {{'u',0},{'r',1},{'d',0},{'l',-1}}; analogous for Y.
    2019-06-01 05:14:37Z
  5. ...and also you have defined 'moves' twice. First as enum and then as string
    2019-06-01 05:22:14Z
1 Answers 1

I would drop the idea with an enum because I feel it has no use for the actual problem – to map characters to navigation moves. For this, I would use a std::map or a std::unordered_map. (Considering, that there are 4 entries only, the performance difference is probably hard to measure.)

While I was preparing a sample code, πάντα ῥεῖ gave a similar hint. Though, I would even recommend to bundle x and y of moves together:

#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;
}

Output:

(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

2
2019-06-01 05:47:32Z
  1. I had the same idea to combine X/Y moves into one pair, was just long for a comment :-)
    2019-06-01 05:29:51Z
  2. Need C++11 though
    2019-06-01 05:44:49Z
  3. @Atul I don't see why. This should work with c++-98 standards. (unless you mean the using statement, but that could be easily replaced with a typedef)
    2019-06-01 05:45:58Z
  4. @Atul It should compile with C++11 as well. (At least it did with g++ -std=c++11.)
    2019-06-01 05:46:32Z
  5. @πάνταῥεῖ Initializing std::map compiles with and above C++11 But anyways since OP has put tag C++11 I believe this should solve his/her problem.
    2019-06-01 05:52:56Z
source placed here