3

我正在尝试计算向量向量内的一系列元素的能量。一旦特定元素具有正确的能量,它就会被推入另一个向量向量。这是一个示例,因为很难解释:

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每个包含以下值:

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}`

怎么了?

向量的向量似乎覆盖了插入其中的块。所以代替上面的,我只得到它发现的最后一个元素:

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

我正在考虑和尝试的是将每个“块”存储在包含足够能量的向量向量中,然后将所有这些值推入 a 中vector<double>,然后将该向量插入到簇块中。

有没有另一种方法,一个更简单的解决这个问题的方法?

4

4 回答 4

1

更正的代码:

#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 
于 2013-09-18T15:35:09.173 回答
0

我更改了一段代码使其工作

  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());
      }
      if(!energy(vals[i]) && energy(vals[i+1])) {
        clusters.push_back( tmp );
        ++j;
      }

  }

不要使用调整大小。您可以将一个空向量 push_back 到您的集群中。

对于每个附加向量的操作,只需使用按范围插入

void insert (iterator position, InputIterator first, InputIterator last);

参考:http ://en.cppreference.com/w/cpp/container/vector/insert

于 2013-09-18T15:40:46.160 回答
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++;
    }
}

首先,你的energy函数返回一个int,所以当返回值非零时,它会将条件评估为true,而当它为 0 时,它将是false

其次,您实际上是push_backif分支中执行 a ,但您只是推送当前向量(您没有将其附加到现有向量)。

为了解决这个问题,我建议使用 amap<int, vector<int>>这样您就可以快速引用每个集群(例如,能量为 5 的集群的键为 5,因此您可以创建一个新向量并插入这些值,或者附加到那里已经存在)。如果您在向量向量中需要它,您可以在对它们进行排序并将它们交换到向量向量中之后迭代地图。

于 2013-09-18T15:40:55.210 回答
0

你可以简化很多你的代码,所以它会更容易阅读和理解,例如你的能量函数,你可以使用算法将它写在一行中:

return std::inner_product(vals.begin(), vals.end(), vals.begin(), 0.) >= 5;
于 2013-09-18T16:02:13.563 回答