如何访问向量内的向量

How to access a vector inside a vector?

本文关键字:向量 访问 何访问      更新时间:2023-10-16

,所以我有一个向量的向量型double。我基本上需要能够将360个数字设置为舒适,然后将这些360个数字放入Cosiney [0]中,然后再获得另外360个数字,该数字现在用不同的A计算,然后将它们放入cosiney [1]。向量将是Cosineya,然后我需要能够为我指定的一个舒适...

我的代码是这样说的:

for (int a = 0; a < 8; a++)
{
   for int n=0; n <= 360; n++
   {
      cosY[n] = cos(a*vectorOfY[n]);
   }
   cosineY.push_back(cosY);
 }

我希望这是实际设置它的正确方法。

,但随后我需要为我指定的A舒适,并计算另一个360向量,该向量将再次存储在另一个向量中作为向量向量。

现在我有:

for (int a = 0; a < 8; a++
{
    for (int n = 0; n <= 360; n++)
    {
       cosProductPt[n] = (VectorOfY[n]*cosY[n]);
    }
    CosProductY.push_back(cosProductPt);
 }

矢量是输入波的幅度。我正在做的是试图创建具有不同频率(a)的余弦波。然后,我在每个频率下计算输入和余弦波的乘积。我需要能够在程序中以后在每个频率上访问这360点,现在我还需要计算每个频率(存储在cosproducty中)中的所有元素的添加,并将其存储在向量中dotproductcos [a]。

我一直在尝试解决它,但是我不知道如何访问向量向量中的所有元素以添加它们。我一直在尝试整天这样做,没有任何结果。现在,我不了解,以至于我什至不知道如何在向量内显示或访问向量,但是我需要使用该访问点进行添加。

谢谢您的帮助。

for (int a = 0; a < 8; a++)
{
   for int n=0; n < 360; n++) // note traded in <= for <. I think you had an off by one 
                              // error here.
   {
      cosY[n] = cos(a*vectorOfY[n]);
   }
   cosineY.push_back(cosY);
 }

听起来很长,只要cosY已预先分配到至少包含360个元素。你可以

std::vector<std::vector<double>> cosineY;
std::vector<double> cosY(360); // strongly consider replacing the 360 with a well-named 
                               // constant
for (int a = 0; a < 8; a++) // same with that 8
{
   for int n=0; n < 360; n++)
   {
      cosY[n] = cos(a*vectorOfY[n]);
   }
   cosineY.push_back(cosY);
 }

例如,但这比您需要的时间长于cosY,以后可能会引起问题,因此我可能会通过将上述代码扔入函数来确定cosY

std::vector<std::vector<double>> buildStageOne(std::vector<double> &vectorOfY)
{
    std::vector<std::vector<double>> cosineY;
    std::vector<double> cosY(NumDegrees);
    for (int a = 0; a < NumVectors; a++)
    {
       for int n=0; n < NumDegrees; n++)
       {
          cosY[n] = cos(a*vectorOfY[n]); // take radians into account if needed.
       }
       cosineY.push_back(cosY);
     }
    return cosineY;
}

这看起来很恐怖,以值返回vector,但是绝大多数编译器将利用复制省略或其他偷偷摸摸的优化来消除复制。

然后我要为第二步做几乎完全相同的事情。

std::vector<std::vector<double>> buildStageTwo(std::vector<double> &vectorOfY,
                                               std::vector<std::vector<double>> &cosineY)
{
    std::vector<std::vector<double>> CosProductY;
    for (int a = 0; a < numVectors; a++)
    {
        for (int n = 0; n < NumDegrees; n++)
        {
           cosProductPt[n] = (VectorOfY[n]*cosineY[a][n]);
        }
        CosProductY.push_back(cosProductPt);
     }
    return CosProductY;
}

,但我们可以进行几个优化

std::vector<std::vector<double>> buildStageTwo(std::vector<double> &vectorOfY,
                                               std::vector<std::vector<double>> &cosineY)
{
    std::vector<std::vector<double>> CosProductY;
    for (int a = 0; a < numVectors; a++)
    {
        // why risk constantly looking up cosineY[a]? grab it once and cache it
        std::vector<double> & cosY = cosineY[a]; // note the reference
        for (int n = 0; n < numDegrees; n++)
        {
           cosProductPt[n] = (VectorOfY[n]*cosY[n]);
        }
        CosProductY.push_back(cosProductPt);
     }
    return CosProductY;
}

,下一个是第一个的扩展:

std::vector<std::vector<double>> buildStageTwo(std::vector<double> &vectorOfY,
                                               std::vector<std::vector<double>> &cosineY)
{
    std::vector<std::vector<double>> CosProductY;
    std::vector<double> cosProductPt(360);
    for (std::vector<double> & cosY: cosineY) // range based for. Gets rid of 
    {
        for (int n = 0; n < NumDegrees; n++)
        {
           cosProductPt[n] = (VectorOfY[n]*cosY[n]);
        }
        CosProductY.push_back(cosProductPt);
     }
    return CosProductY;
}

我们可以为for (int n = 0; n < NumDegrees; n++)做相同的基于范围的技巧,但是由于我们在这里迭代多个数组,这并不是很有帮助。