如何在类中初始化向量?

时间:2022-12-06 22:49:34
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.

我已经尝试将其初始化,其中使用vector > board定义了board(9,vector (9,“。”));但那不起作用。

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?

在构造函数内部,那些也没有工作。那么我是否仅限于将矢量初始化为我在第一个例子中做的方式(它确实有效)?还是有另一种方法可以做到吗?

2 个解决方案

#1


2  

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, "." ) ); 
    }
};

#2


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, ".");
}

#1


2  

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, "." ) ); 
    }
};

#2


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, ".");
}