C++ 使用默认值在 Struct 中实例化 2D Vector
C++ Instantiate 2D Vector inside Struct with default values
使用 C++11,我最初有一个具有默认值的以下形式的二维向量:
vector<vector<int>> upper({{1,2,3,4,5,6},{7,8,9,10,11,-1},{12,13,14,15,-1,-1},{16,17,18,-1,-1,-1},{19,20,-1,-1,-1,-1},{21,-1,-1,-1,-1,-1}});
vector<vector<int>> lower({{0,0,0,0,0,0},{0,0,0,0,0,-1},{0,0,0,0,-1,-1},{0,0,0,-1,-1,-1},{0,0,-1,-1,-1,-1},{0,-1,-1,-1,-1,-1}});
这代表了我要解开的谜题的上下两部分。现在我想修改我的程序,以便在结构内声明这些向量,但我不确定如何执行此操作并为二维向量提供默认值。这是我目前拥有的:
struct BoardState{
vector<int> row;
vector<vector<int>> upper;
vector<vector<int>> lower;
BoardState() : row(6,0), upper(6,row), lower(6,row) {};
};
但是当我尝试访问里面的内容时它会导致段错误,使用:
#include <iostream>
#include <vector>
#include <stdlib.h>
BoardState *board;
int main(){
using namespace std;
...
for(int i=0; i<6; i++){
for(int j=0; j<6; j++){
cout << board->upper[i][j] << " ";
}
cout << endl;
}
}
如何为结构内的二维向量赋予默认值?谢谢
来自 gcc warning" 'will be initialized after':
Make sure the members appear in the initializer list in the same order
as they appear in the class.
编辑:
#include <iostream>
#include <vector>
using namespace std;
struct BoardState{
vector<int> row;
vector<vector<int>> upper;
vector<vector<int>> lower;
BoardState() : row(6,0), upper(6,row), lower(6,row) {};
};
int main() {
BoardState board;
for(int i=0; i<6; i++){
for(int j=0; j<6; j++){
cout << board.upper[i][j] << " ";
}
cout << endl;
}
}
I'm not sure how to do this and give the 2d vectors default values.
与结构外完全一样
#include <vector>
#include <iostream>
struct BoardState
{
std::vector<std::vector<int>> upper{{1,2,3,4,5,6},{7,8,9,10,11,-1},
{12,13,14,15,-1,-1},{16,17,18,-1,-1,-1},
{19,20,-1,-1,-1,-1},{21,-1,-1,-1,-1,-1}};
std::vector<std::vector<int>> lower{{0,0,0,0,0,0},{0,0,0,0,0,-1},
{0,0,0,0,-1,-1},{0,0,0,-1,-1,-1},
{0,0,-1,-1,-1,-1},{0,-1,-1,-1,-1,-1}};
std::vector<int> row;
BoardState()
{ }
};
int main()
{
BoardState bs;
std::cout << bs.upper[3][1] << std::endl; // print 17
}
或
struct BoardState
{
std::vector<int> row {6, 0};
std::vector<std::vector<int>> upper {6, row};
std::vector<std::vector<int>> lower {6, row};
BoardState()
{ }
};
(在这种情况下,打印 0
)。
请注意,正如 ArchbishopOfBanterbury 和 Petter 所解释的那样,成员按照声明的顺序进行初始化;所以,如果你想用row
初始化upper
和lower
,你必须在row
之前声明。
使用 C++11,我最初有一个具有默认值的以下形式的二维向量:
vector<vector<int>> upper({{1,2,3,4,5,6},{7,8,9,10,11,-1},{12,13,14,15,-1,-1},{16,17,18,-1,-1,-1},{19,20,-1,-1,-1,-1},{21,-1,-1,-1,-1,-1}});
vector<vector<int>> lower({{0,0,0,0,0,0},{0,0,0,0,0,-1},{0,0,0,0,-1,-1},{0,0,0,-1,-1,-1},{0,0,-1,-1,-1,-1},{0,-1,-1,-1,-1,-1}});
这代表了我要解开的谜题的上下两部分。现在我想修改我的程序,以便在结构内声明这些向量,但我不确定如何执行此操作并为二维向量提供默认值。这是我目前拥有的:
struct BoardState{
vector<int> row;
vector<vector<int>> upper;
vector<vector<int>> lower;
BoardState() : row(6,0), upper(6,row), lower(6,row) {};
};
但是当我尝试访问里面的内容时它会导致段错误,使用:
#include <iostream>
#include <vector>
#include <stdlib.h>
BoardState *board;
int main(){
using namespace std;
...
for(int i=0; i<6; i++){
for(int j=0; j<6; j++){
cout << board->upper[i][j] << " ";
}
cout << endl;
}
}
如何为结构内的二维向量赋予默认值?谢谢
来自 gcc warning" 'will be initialized after':
Make sure the members appear in the initializer list in the same order as they appear in the class.
编辑:
#include <iostream>
#include <vector>
using namespace std;
struct BoardState{
vector<int> row;
vector<vector<int>> upper;
vector<vector<int>> lower;
BoardState() : row(6,0), upper(6,row), lower(6,row) {};
};
int main() {
BoardState board;
for(int i=0; i<6; i++){
for(int j=0; j<6; j++){
cout << board.upper[i][j] << " ";
}
cout << endl;
}
}
I'm not sure how to do this and give the 2d vectors default values.
与结构外完全一样
#include <vector>
#include <iostream>
struct BoardState
{
std::vector<std::vector<int>> upper{{1,2,3,4,5,6},{7,8,9,10,11,-1},
{12,13,14,15,-1,-1},{16,17,18,-1,-1,-1},
{19,20,-1,-1,-1,-1},{21,-1,-1,-1,-1,-1}};
std::vector<std::vector<int>> lower{{0,0,0,0,0,0},{0,0,0,0,0,-1},
{0,0,0,0,-1,-1},{0,0,0,-1,-1,-1},
{0,0,-1,-1,-1,-1},{0,-1,-1,-1,-1,-1}};
std::vector<int> row;
BoardState()
{ }
};
int main()
{
BoardState bs;
std::cout << bs.upper[3][1] << std::endl; // print 17
}
或
struct BoardState
{
std::vector<int> row {6, 0};
std::vector<std::vector<int>> upper {6, row};
std::vector<std::vector<int>> lower {6, row};
BoardState()
{ }
};
(在这种情况下,打印 0
)。
请注意,正如 ArchbishopOfBanterbury 和 Petter 所解释的那样,成员按照声明的顺序进行初始化;所以,如果你想用row
初始化upper
和lower
,你必须在row
之前声明。