0
class Suduko { private: vector<vector<string>> board; public: Suduko() : board(9, vector<string>(9, ".")) {} } 

Is this the only way to do it?

I've tried initializing it right where board is defined with vector<vector<string>> board(9, vector<string>(9, ".")); but that doesnt work.

I also tried:

Suduko() { board(9, vector<string>(9, ".")); } 

and

Suduko() { board = board(9, vector<string>(9, ".")); } 

inside of the constructor and those didn't work either. So am I limited to initializing the vector to the way I did in the first example (which did work)? Or is there another way I can do it?

4
  • 3
    vector isn't special. Look up how to initialize any data member and you'll have your answer. Commented Oct 25, 2015 at 21:46
  • Vectors are for cases where you need dynamically resized arrays. How large do you envision your Sudoku field to grow at runtime? Also, can different rows grow separately? Seriously, this is one case where a simple 9x9 array is called for. Commented Oct 25, 2015 at 21:46
  • @UlrichEckhardt This is an exercise in 2D vectors, I understand a 2D array would work more appropriately here. Commented Oct 25, 2015 at 21:48
  • You have away that always works, and you don't like it? Why? Commented Oct 25, 2015 at 21:54

2 Answers 2

3

Here are listed some ways to initialize the vector

class Suduko { private: std::vector<std::vector<std::string>> board { 9, std::vector<std::string>( 9, "." ) }; //..... }; class Suduko { private: std::vector<std::vector<std::string>> board = std::vector<std::vector<std::string>>( 9, std::vector<std::string>( 9, "." ) ); //..... }; class Suduko { private: std::vector<std::vector<std::string>> board; public: Suduko() : board( 9, std::vector<std::string> (9, "." ) ) { } }; class Suduko { private: std::vector<std::vector<std::string>> board; public: Suduko() : board{ 9, std::vector<std::string> (9, "." ) } { } }; class Suduko { private: std::vector<std::vector<std::string>> board; public: Suduko() { board.assign( 9, std::vector<std::string> (9, "." ) ); } }; 
Sign up to request clarification or add additional context in comments.

1 Comment

The specific issue is that C++ tries to make any declaration outside of a function body of the form: Type name(anything here); into a function declaration. You can even see this with a simple declaration like: int i(4); Use one of the initializer forms Vlad posted above instead.
0

To get your other attempts to work, you must use:

board = vector<vector<string>>(9, vector<string>(9, ".")); 

You can also use:

board.resize(9); for (auto& v : board) { v.resize(9, "."); } 

7 Comments

I like the second method, thank you for sharing. Is the first seen as bad practice?
@gdagger The second could possibly be faster because it doesn't create a temporary vector, I'm not sure.
@NeilKirk The speed benefit is quite small, unless you create 10000000 Sudokus each second. Readability is more impotent here. Less code = easier to understand what's going on.
@АндрейБеньковский It doesn't matter in this case but could if the technique is generalized to other projects.
@NeilKirk Compilers, processors and operating systems do so much work under the hood that it's impossible to speak about speed without actual benchmarking (except obvious cases). Your solution might be faster, but the only way to prove it is to do an experiment (create 10000000 sudikus in a loop).
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.