如何访问向量内的向量
How to access a vector inside a vector?
,所以我有一个向量的向量型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++)
做相同的基于范围的技巧,但是由于我们在这里迭代多个数组,这并不是很有帮助。
- 尝试通过多个向量访问变量时,向量下标超出范围
- C++从另一个类访问公共静态向量的正确方法是什么
- 访问存储在向量C++中的结构的多态成员
- 如何从静态向量访问类元素?
- 在类 A 中创建类型为 B 类的向量 - 访问数据 [C++] [成员在两个类中都是私有的]
- 从父类的向量访问子类函数,而无需向下转换
- 如何使用常数向量访问成员函数
- 如何从C 向量访问单个单词
- 从C++中传递给函数的指针向量访问矩阵
- 多向量访问与本地对象
- C 迭代器与用于向量访问的指针
- 从向量访问成员功能
- 如何在 c++ 中从 "pointer-to-objects" 向量访问对象
- 如何从双精度向量的向量访问数据
- 从指针向量访问成员函数?C++
- 如何使用SWIG Python从内部向量访问c ++对象方法
- 无法从向量访问对象的getter
- 从向量访问某些类函数C++的问题
- 从基类类型的向量访问派生类方法
- 从对象指针向量访问对象