向量的向量,从数组中分离出不同的值
Vector of vectors , Separating different values from array
>我需要从二维数组中分离值。我需要做的就是存储具有特定值的字段的所有索引。
例如,我可能有一个数组,其中包含 3 个值为 1 的单元格和 10 个值为 2 的字段。我尝试创建 1 个向量,该向量存储值 1 的所有索引,另一个向量存储值 2 的所有索引。
这是我一直在尝试编写的代码,但它似乎没有改变
void searchForGrains(Cell **tab, int _size){
storage.push_back(tab[0][0]);
Point p(0,0); // Point refers to array indexes
storage[0].points.push_back(p);
for(int i=0 ; i<_size ; ++i){
for(int j=0 ; j<_size ; ++j){
int counter = 0;
for(unsigned int k=0 ; k<storage.size() ; k++){
if(tab[i][j].value == storage[k].value){
Point pp(i,j);
storage[k].points.push_back(pp);
}
else
counter++;
}
if(counter == storage.size())
storage.push_back(tab[i][j]);
}
}
}
我认为这是一个非常简单的逻辑错误。问题是,当您找到一个新值时,尽管您向存储向量添加了新条目,但您不会添加找到新值的点。除了你做了一个特例的 0,0 处,但你不需要这样做。试试这个
void searchForGrains(Cell **tab, int _size)
{
for(int i=0 ; i<_size ; ++i)
{
for(int j=0 ; j<_size ; ++j)
{
int counter = 0;
for(unsigned int k=0 ; k<storage.size() ; k++)
{
if(tab[i][j].value == storage[k].value)
{
Point pp(i,j);
storage[k].points.push_back(pp);
}
else
counter++;
}
if(counter == storage.size())
{
storage.push_back(tab[i][j]);
Point pp(i,j);
storage[storage.size() - 1].points.push_back(pp);
}
}
}
}
毫无疑问,这可以提高效率,但我认为它有效。
相关文章:
- C++:如何读取分离变量,然后读取向量
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 为什么负数不取模向量大小给出负数?
- 为什么我的向量::擦除调用会抛出"vector subscript out of range"?
- C++ 向量加减抛出"expression: vector subscript out of range."错误
- 数组为此合并排序函数提供了正确的输出,但向量给出了不正确的输出.出了什么问题?
- 打印出对象向量中的每个元素C++
- 为什么迭代向量在 c++ 中给出固定地址?
- C++向量排序给出0作为输出
- C++基向量是指导出的向量
- GLM 矩阵乘法向量给出不正确的结果
- 从文本文件中读取数据并分离出每行中包含的数据
- 向量给出垃圾值
- 2D 向量给出分割错误
- 向量的向量,从数组中分离出不同的值
- 在opencv 2.3中从RGB图像中分离出红色分量图像
- std::equal_range 对于对的向量抛出<运算符错误
- 哪个向量抛出了索引超出范围的异常
- 具有预先确定大小的c++向量抛出编译错误
- 跨向量踢出重复条目