C++ 对向量的更改<;比特集>;更改另一个向量中的值
提供以下代码:C++ 对向量的更改<;比特集>;更改另一个向量中的值,c++,vector,bitset,C++,Vector,Bitset,提供以下代码: #include <iostream> #include <bitset> #include <set> #include <string> #include <vector> using namespace std; typedef pair<int, int> coordinate; typedef vector<coordinate> path; class Game { privat
#include <iostream>
#include <bitset>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> coordinate;
typedef vector<coordinate> path;
class Game {
private:
vector<bitset<15>> board{15};
vector<path> paths;
public:
Game() {
for (int i = 0; i < 15; i++) {
string line;
getline(cin, line);
for (int j = 0; j < 15; j++) {
if (line[j] == '.') {
board[i].set(j); // the problem no longer occurs after removing this line
if (paths.size() > 2)
cout << "A: " << paths[2].back().first << endl;
paths.emplace_back(1, make_pair(j, i));
if (paths.size() > 2)
cout << "B: " << paths[2].back().first << endl;
}
}
}
}
};
int main() {
Game game;
return 0;
}
此输入产生以下输出:
A: 2
B: 2
A: 2
B: 2
...
A: 2
B: 2
A: 3
B: 3
A: 3
B: 3
A: 7
B: 7
A: 15
B: 15
A: 31
B: 31
A: 63
B: 63
A: 127
B: 127
A: 255
B: 255
A: 511
B: 511
A: 1023
B: 1023
A: 2047
B: 2047
A: 4095
B: 4095
A: 8191
B: 8191
A: 16383
B: 16383
A: 32767
B: 32767
...
A: 32767
B: 32767
A: 32767
B: 32767
我试过的一件事是换板[I].set(j);要执行[i]|=1操作,问题在于初始化:
vector<bitset<15>> board{15};
或在构造函数初始值设定项列表中对其进行初始化:
Game() : board(15) // intialize with 15 default-constructed elements
{ /* other logic */}
问题在于初始化:
vector<bitset<15>> board{15};
或在构造函数初始值设定项列表中对其进行初始化:
Game() : board(15) // intialize with 15 default-constructed elements
{ /* other logic */}
“向量路径中的值是由线路板[i].set(j);“为什么这么说?”向量路径中的值是由线路板[i].set(j);“为什么这么说?我明白了,我一定是把构造函数弄混了。”。使用std::array非常好,谢谢。我明白了,我一定是把构造函数搞混了。使用std::array非常好,谢谢。