c++ - 将多个元素插入二维 block

标签 c++ c++11 vector

我正在尝试计算 vector vector 中一系列元素的能量。一旦特定元素具有合适的能量,它就会被插入另一个 vector vector 中。这是一个例子,因为它很难解释:

bool energy(const std::vector<double> &vals)
{
  float sum = 0.0;
  for(unsigned i=0; (i < vals.size()); i++)
  {
     sum += (vals[i]*vals[i]);
  }
  //cout << sum << endl;
  return (sum >= 5);
}

int main(int argc, char *argv[]) {

 std::vector<vector<double> > vals {

    {0, 0, 0, 0, 0}, // This has an energy of "0" -> does not count
    {1, 1, 1, 1, 1}, // This has an energy of "5" -> push_back to vector[0]
    {1, 1, 1, 1, 1}, // This has an energy of "5" -> push_back to vector[0]
    {1, 1, 1, 1, 1}, // This has an energy of "5" -> push_back to vector[0]
    {1, 2, 1, 1, 1}, //This has an energy of "5" -> push_back to vector[0]
    {0, 0, 0, 0, 0}, // This has an energy of "0" -> does not count && start a new 
                                // vector
    {1, 2, 3, 4, 5}, // This has an energy of "55" -> push_back to vector[1]
    {1, 2, 3, 4, 5}, // This has an energy of "55" -> push_back to vector[1]
    {1, 2, 3, 4, 5}, // This has an energy of "55" -> push_back to vector[1]
    {1, 2, 3, 4, 5} // This has an energy of "55" -> push_back to vector[1]
};
std::vector<vector<double> > clusters; 
std::vector<vector<double> > tmp;

//std::for_each(vals.begin(), vals.end(), energy);

int j = 0;

for(unsigned i=0; (i < vals.size()); i++)
{
    if(energy(vals[i]))
    {
        clusters.resize(j + 1);
        clusters[j] = vals[i];
    }else if(!energy(vals[i]) && energy(vals[i+1]))
    {
        j++;
    }
}

for(unsigned i=0; (i < clusters.size()); i++)
{
    for(unsigned j=0; (j < clusters[i].size()); j++)
    {
        cout << clusters[i][j] << ' ';
    }
    cout << endl;
}
 }

应该发生什么

应该有2名为 clusters 的 vector 的 vector 元素每个包含值:

clusters[0] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1 };

`clusters[1] = 1, 2, 3, 4, 5, 
      1, 2, 3, 4, 5,
      1, 2, 3, 4, 5, 
      1, 2, 3, 4, 5}`

发生了什么?

vector 的 vector 似乎覆盖了插入其中的 block 。因此,我只是得到它找到的最后一个元素,而不是上面的内容:

`cluster[0] = {1 2 1 1 1}
cluster[1] = {1 2 3 4 5}`

我的想法和尝试是将每个“ block ”存储在包含足够能量的 vector vector 中,然后将所有这些值放入 vector<double> 中。然后将这个 vector 插入簇 block 中..

是否有替代方法,更简单的解决此问题的方法?

最佳答案

更正后的代码:

#include<vector>
#include<iostream>
using namespace std;

bool energy(const std::vector<double> &vals)
{
  float sum = 0.0;
  for(unsigned i=0; (i < vals.size()); i++)
  {
     sum += (vals[i]*vals[i]);
  }
  //cout << sum << endl;
  return (sum >= 5);
}

int main(int argc, char *argv[]) {

 std::vector<vector<double> > vals {

    {0, 0, 0, 0, 0}, // This has an energy of "0" -> does not count
    {1, 1, 1, 1, 1}, // This has an energy of "5" -> push_back to vector[0]
    {1, 1, 1, 1, 1}, // This has an energy of "5" -> push_back to vector[0]
    {1, 1, 1, 1, 1}, // This has an energy of "5" -> push_back to vector[0]
    {1, 2, 1, 1, 1}, //This has an energy of "5" -> push_back to vector[0]
    {0, 0, 0, 0, 0}, // This has an energy of "0" -> does not count && start a new
                                // vector
    {1, 2, 3, 4, 5}, // This has an energy of "55" -> push_back to vector[1]
    {1, 2, 3, 4, 5}, // This has an energy of "55" -> push_back to vector[1]
    {1, 2, 3, 4, 5}, // This has an energy of "55" -> push_back to vector[1]
    {1, 2, 3, 4, 5} // This has an energy of "55" -> push_back to vector[1]
};
std::vector<vector<double> > tmp(vals.size());
std::vector<vector<double> > clusters(vals.size());

int j = 0;

for(unsigned i=0; (i < vals.size()); i++)
{
    if(energy(vals[i]))
    {
        clusters[j].insert(clusters[j].end(), vals[i].begin(), vals[i].end());
    }else if(!energy(vals[i]) && energy(vals[i+1]))
    {
        j++;
    }
}

for(unsigned i=0; (i < clusters.size()); i++)
{
    for(unsigned j=0; (j < clusters[i].size()); j++)
    {
        cout << clusters[i][j] << ' ';
    }
    cout << endl;
}
 }

输出是:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 
1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 

关于c++ - 将多个元素插入二维 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18876115/

相关文章:

c++ - C++ 中成员数量可变的模板类

c++ - std::forward_list.push_front(std::thread) 编译失败

c++ - 几个 vector 的笛卡尔积

r - 在 r 中创建特定长度和字符的向量

c# - 为双边移动统一镜像 Oculus Rift Controller 位置

c++ - 使用 `std::greater` 通过 `priority_queue` 创建最小堆的原因

c++ - 如何在低内存条件下正确分配 C++ 中的内存

c++ - 不能将 operator= 与 std::stringstream 一起使用

c++ - 整数转换问题

c++ - 从 Windows 启动程序时如何查看键盘修饰符?