How do I work with nested vectors in C++?

前端 未结 1 924
情书的邮戳
情书的邮戳 2021-01-18 11:49

I\'m trying to work with vectors of vectors of ints for a sudoku puzzle solver I\'m writing.

Question 1:

If I\'m going to access a my 2d vec

相关标签:
1条回答
  • 2021-01-18 12:27

    Q1: Yes, that is the correct way to handle it. However, notice that nested vectors are a rather inefficient way to implement a 2D array. One vector and calculating indices by x + y * width is usually a better option.

    Q2A: Calculating grid[i][j] + " " does not concatenate two strings (because the left hand side is int, not a string) but instead adds the numeric value to a pointer (the memory address of the first character of the string " "). Use cout << grid[i][j] << " " instead.

    Q2B: You are passing the array by value (it gets copied) for readAPuzzle. The the function reads into its local copy, which gets destroyed when the function returns. Pass by reference instead (this avoids making a copy and uses the original instead):

    void readAPuzzle(array2d_t& grid)
    
    0 讨论(0)
提交回复
热议问题