写入向量<向量<bool>>
writing to a vector< vector<bool> >
我正在为类编写Conway在环面中的生命游戏的一个实现。函数cargarToroide
(loadToroid(应将每个单元格(celda
(的适当状态(活动或死亡-真或假-1或0(从文件加载到向量中,其签名如下:
toroide cargarToroide(string nombreArchivo, bool &status);
nombreArchivo
是文件名,如果加载文件或文件格式有任何问题,则status
应为false。
数据结构定义如下(我无法更改(:
typedef vector< vector<bool> > toroide;
该文件的结构如下:
numberOfLines numberOfColums
list of the values of the cells
number of live cells
例如:
4 4
1 0 0 0
0 0 1 0
0 0 0 1
0 1 0 0
4
问题是,我找不到让它发挥作用的方法。我在网上读到,当你试图以通常的方式加载vector<bool>
时,它会出现问题,这是我尝试的第一件事。
toroide cargarToroide(string nombreArchivo, bool &status)
{
toroide t;
ifstream fi (nombreArchivo);
int cantidadFilas, cantidadColumnas;
int celda;
if(!fi){
status = false;
}
fi >> cantidadFilas;
fi >> cantidadColumnas;
for(int i = 0; i < cantidadFilas; i++) {
for (int j = 0; j < cantidadColumnas; j++) {
fi >> celda;
if(celda == 1) {
t[i].push_back(true);
}
else if(celda == 0){
t[i].push_back(false);
}
else{
status = false;
return t;
}
}
}
return t;
}
我还尝试将celda
定义为布尔值,并仅使用
t[i].push_back(celda);
使用C++11实现这一点的最佳方法是什么?
您需要调整外部向量的大小,然后才能对其使用运算符[]。在读取数据时还应该使用正确的类型(bool(,并检查输入文件中的错误。我在代码中评论道:
#include <iostream>
#include <fstream>
#include <vector>
typedef std::vector< std::vector<bool> > toroide;
// Both cargarToroide and cargarToroide_improved can be used
bool cargarToroide_improved(const std::string& nombreArchivo, toroide& in_toroide)
{
std::ifstream fi(nombreArchivo);
if(!fi) return false;
int cantidadFilas, cantidadColumnas, liveCells=0;
// use a bool to read the bool data
bool celda;
fi >> cantidadFilas;
fi >> cantidadColumnas;
// check if the stream is in a failed state
if(fi.fail()) return false;
// Temporary used to not mess with in_toroide until we're finished.
// Create it with cantidadFilas default inserted rows
toroide t(cantidadFilas);
for(auto& row : t) {
// default insert columns into the row
row.resize(cantidadColumnas);
for (int col = 0; col < cantidadColumnas; ++col) {
fi >> celda;
// check if the stream is in a failed state
// (non-bool read or the file reached eof())
if(fi.fail()) return false;
// set column value in the row
row[col] = celda;
// count live cells
liveCells += celda;
}
}
// compare live cells in matrix with checksum
int cmpLive;
fi >> cmpLive;
if(fi.fail() || cmpLive!=liveCells) return false;
// a successful toroide was read, swap your temporary
// toroide with the user supplied one
std::swap(t, in_toroide);
return true;
}
// if the signature of this function really can't be changed (which it should),
// make it a proxy for the function with a slightly nicer interface
// Like this:
toroide cargarToroide(std::string nombreArchivo, bool &status)
{
toroide rv;
status = cargarToroide_improved(nombreArchivo, rv);
return rv;
}
使用改进的签名:
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv+1, argv+argc);
for(auto& file : args) {
toroide my_toroide;
if(cargarToroide_improved(file, my_toroide)) {
for(auto& r : my_toroide) {
for(auto c : r) {
std::cout << c << " ";
}
std::cout << "n";
}
} else {
std::clog << "failed loading " << file << "n";
}
}
}
使用您被迫使用的签名:
int main(int argc, char* argv[]) {
std::vector<std::string> args(argv+1, argv+argc);
for(auto& file : args) {
bool status;
toroide my_toroide = cargarToroide(file, status);
if(status) {
for(auto& r : my_toroide) {
for(auto c : r) {
std::cout << c << " ";
}
std::cout << "n";
}
} else {
std::clog << "failed loading " << file << "n";
}
}
}
如果您知道编译时的行数(就像在本例中所做的那样(,则可以使用resize
。在这种情况下,您必须在两个for
循环之前执行以下操作。
t.resize(cantidadFilas);
事实上,您也可以对列执行同样的操作。这样,您就不再需要在内部for
循环中使用push_back
了。
如果不知道行数,则只使用push_back
并将行添加到toroide
。然后在第二个for
循环之前添加以下行。
vector<bool> row;
t.push_back(row)
相关文章:
- 写入向量<向量<bool>>
- 函数向量_指针有不同的原型,我可以构建一个吗
- std::向量与传递值的动态数组
- 将值指定给向量(2D)的向量中的某个位置
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 如何使用向量的template_back函数
- 尝试通过多个向量访问变量时,向量下标超出范围
- 如何通过派生类函数更改基类中的向量
- C++从另一个类访问公共静态向量的正确方法是什么
- 如何将ampl中的集合表示为c++中的向量
- 变量没有改变?通过向量的函数调用
- 迭代时从向量和内存中删除对象
- 向量 <int> a {N, 0} 和 int arr a[N] = {0} 的时间复杂度有什么区别
- 如何为模板化对象创建模板向量?VS正在投掷C3203
- 计算排序向量的向量中唯一值的计数
- 矩阵向量乘法(cublasDgemv)返回零
- EASTL矢量<向量<int>>连续的
- 为什么将此对向量&lt; map&lt; int,int&gt;&gt;中的地图进行更新.失败
- 如何加入向量&lt; int&gt;到C 中的单个INT
- 将图像加载到Mat-vvector<向量<垫子>>