I am new to C# and now I need to rewrite my code for C to C#.
std::vector<std::vector<std::pair<int, bool>>> board(n);
for (int i = 0; i < m; i )
{
int node1 = 0, node2 = 0;
std::cin >> node1 >> node2;
board[node1 - 1].push_back(std::make_pair(node2 - 1, true));
board[node2 - 1].push_back(std::make_pair(node1 - 1, false));
}
It is my try, but I can`t create an array with a specified length and add the values to it.
List<Dictionary<int, bool>> board = new List<Dictionary<int, bool>>();
for (int i = 0; i < n; i ) board.Add(new List<Dictionary<int, bool>>);
for (int i = 0; i < m; i )
{
String[] road = Console.ReadLine().Split(' ');
int node1 = int.Parse(road[0]), node2 = int.Parse(road[1]);
board[node1 - 1].Add(Dictionary<int, bool>(node2 - 1, true));
board[node2 - 1].Add(Dictionary<int, bool>(node1 - 1, false));
}
CodePudding user response:
Your C code allocates an n-element vector/List of vectors/Lists of (int, bool)- Tuples.
The "verbatim" translation to to C# would be a List<List<(int, bool)>>
, or with the n-element initialization e.g.:
var board = Enumerable.Range(0, n).Select(i => new List<(int, bool)>()).ToList();
CodePudding user response:
The equivalent to std::vector
in c# is List<T>
. For a tuple you can chose between Tuple
and ValueTuple
. So your type could be List<List<(int, bool)>>
.
You may also consider using a multidimensional array. I.e. (int, bool)[,]
.