c++中返回向量的函数
Function returning a vector in c++
为了完成我的项目,我需要一个包含返回矢量(STL)的函数。下面的函数在使用http://en.cppreference.com的在线编译器运行时确实返回一个向量:
#include <cstddef>
#include <utility>
#include <numeric>
#include <complex>
#include <valarray>
#include <vector>
#include <iostream>
typedef std::complex<double> Complex;
typedef std::valarray <Complex> CArray;
int long Gidx=0;
std::vector<int long>ar;
std::vector<int long> tet(CArray& y) {
auto max_index = std::max_element (std::begin(y), std::end(y),
[](const Complex& a ,const Complex& b)
{
return a.real() < b.real() ;
}
);
std::cout << "index of first max element is " << max_index-std::begin(y)+1 << 'n';
std::cout << "indices of all matches of max element is: " << "[";
for (auto it= std::begin(y), end = std::end(y); it != end; ++it){
if(it->real() == max_index->real()) {
std::cout << it - std::begin(y) +1 <<' ' ;
Gidx= it - std::begin(y) +1;
ar.push_back(Gidx);
}
}
std::cout << "]"<<std::endl;
return ar;
}
int main() {
CArray m({{0.12,1},{0.04,4},{0.12,6},{0.01, 2}, {0.03, 4}, {0.12, 0}, {0.09, 0}, {0.07, 0}, {0.09, 0},{0.12,4}});
std::vector<int long> Gidx4=tet(m);
//std::valarray<int long>Gidx5 = Gidx4;
//std::cout << Gidx4[3] << std::endl;
//double ix=Gidx4[1];
//std::cout << ix << std::endl;
}
输出:index of first max element is 1
indices of all matches of max element is: [1 3 6 10 ]
奇怪的是,当我试图在程序中使用相同的代码部分时(见下文),结果是不同的(它只返回向量的最后一个元素),而向量包含5个元素
我的程序:
//iterDelayEst.cpp
#include <cstddef>
#include <utility>
#include <numeric>
#include <vector>
#include "myexternval.cpp"
/***********************get all indices corresponding to the max element*********************
*********************************************************************************************/
std::vector<int long> tet(CArray& y) {
auto max_index = std::max_element (std::begin(y), std::end(y),
[](const Complex& a ,const Complex& b)
{
return a.real() < b.real() ;
}
);
std::cout << "index of first max element is " << max_index-std::begin(y)+1 << 'n';
std::cout << "indices of all matches of max element is: " << "[";
for (auto it= std::begin(y), end = std::end(y); it != end; ++it){
if(it->real() == max_index->real()) {
std::cout << it - std::begin(y)+1 <<' ';
Gidx= it - std::begin(y)+1;
ar.push_back(Gidx);
}
}
std::cout <<"]"<<std::endl;
return ar;
}
/*******************************xcorr function*************************************************
**********************************************************************************************/
/*
void xcorr(CArray& x, int n){
int i;
fft(x);
x *=x.apply(std::conj);
ifft(x);
for ( i = 0 ; i < n ; i++ ){
cout << "x[" << i <<"] =" << x[i] << endl;
}
}
*/
/******************************iterDelayEst*****************************************************
************************************************************************************************/
double iterDelayEst(int n,CArray& x, CArray& y)
{ /***************************constants************************************************/
//exit if uncertainty below threshold
int j;
double thr_samples = 1e-7;
double halfN= floor (n/2);
//exit after fixed number of iterations
double nIter = 25;
fft(x);
fft(y);
//frequency domain representation of signals
std::vector<double> tau;
auto f = binFreq(n);
std::vector<double> e;
Complex nf4(0.0,0.0);
Complex nf5(0.0,0.0);
for ( j = 0 ; j < n ; j++ )
{
auto nfa=(x * x.apply(std::conj));
nf4 +=nfa[j];
auto nfb=(y * y.apply(std::conj));
nf5 +=nfb[j];
}
auto nf1 = (nf4 * nf5);
auto nf2 =std::sqrt(nf1);
auto nf =nf2/(double)n;
cout << "nf1" << nf1 <<endl;
cout << "nf2" << nf2 <<endl;
cout << "nf" << nf <<endl;
double x1=-1;
double x2=-1;
double x3=-1;
double y1=-1;
double y2=-1;
double y3=-1;
int i;
/****************************iteration loop*************************************
*******************************************************************************/
//for(i=0; i<nIter; i++)
// std::vector<complex<double> > v;
/****crosscorrelation with time-shifted signal****
*************************************************/
x = x.apply(std::conj);
y *= x;
ifft(y);
y =std::abs(y);
y=y/nf;
for ( i = 0 ; i < n ; i++ ){
cout << "y[" << i <<"] =" << y[i] << endl;
}
std::vector<int long> Gidx4=tet(y);
double ixLow =fmod((ix -1)-1 , n) +1; //one below
double ixMid =ix;
double ixHigh =fmod((ix -1)+1 , n) +1; //one above
//delay corresponding to the three bins
double tauLow =fmod(ixLow -1 + halfN, n) - halfN;
double tauMid =fmod(ixMid -1 + halfN, n) - halfN;
double tauHigh =fmod(ixHigh -1 + halfN, n) - halfN;
}
输出:...
(-1.78919e-15,-1.78435e-15)
(-1.34929e-16,1.98328e-16)
(-1.46056e-15,1.58589e-15)
nf1(147456,0)
nf2(384,0)
nf(12,0)
y[0] =(0.583333,0)
y[1] =(0.583333,0)
y[2] =(0.5,0)
y[3] =(0.416667,0)
y[4] =(0.333333,0)
y[5] =(0.333333,0)
y[6] =(0.333333,0)
y[7] =(0.333333,0)
y[8] =(0.416667,0)
y[9] =(0.416667,0)
y[10] =(0.5,0)
y[11] =(0.583333,0)
y[12] =(0.416667,0)
y[13] =(0.333333,0)
y[14] =(0.25,0)
y[15] =(0.0833333,0)
y[16] =(0.0833333,0)
y[17] =(0.166667,0)
y[18] =(0.333333,0)
y[19] =(0.5,0)
y[20] =(0.5,0)
y[21] =(0.5,0)
y[22] =(0.583333,0)
y[23] =(0.583333,0)
y[24] =(0.5,0)
y[25] =(0.416667,0)
y[26] =(0.25,0)
y[27] =(0.0833333,0)
y[28] =(0.0833333,0)
y[29] =(0.166667,0)
y[30] =(0.333333,0)
y[31] =(0.5,0)
index of first max element is 23
indices of all matches of max element is: [23 ]
serge@ubuntu:~/Downloads/OpenCV/opencv-2.4.9/build$
有人能帮帮我吗?
我做错了什么?
为什么说这与返回一个向量有关?当我阅读你的代码时,std::max_element
正在寻找索引23处的最大值,这是最后出现0.583333。然后使用==
将0.583333的其他出现与最后一次出现进行比较。你确定所有出现的0.583333 都完全相等吗?通常情况下,最好将浮点数与某个epsilon内的数进行比较,而不是要求精确相等。
为了测试我的理论,在for循环中,你可以打印出it->real()
和==
比较的布尔结果,看看你是得到一个精确匹配还是五个精确匹配。
相关文章:
- 函数向量_指针有不同的原型,我可以构建一个吗
- 如何使用向量的template_back函数
- 如何通过派生类函数更改基类中的向量
- 变量没有改变?通过向量的函数调用
- 一对向量构造函数:初始值设定项列表与显式构造
- 在c++中使用向量时,如何调用构造函数和析构函数
- std::sort()函数无法对向量的一部分进行排序
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- C++中函数的向量返回类型引发错误
- 添加存储在向量中的大整数的函数出现问题
- 如何在 c++ 中定义接受不同参数类型的函数向量?
- 具有 lambda 函数的函数向量
- 你能用参数制作函数向量吗?
- 在C++中使用成员函数向量时,有没有办法实现协变返回类型
- 在编写 lambda 函数向量时出现隔离错误
- 如何在 boost.process 间共享内存中使用复杂结构"push_back"函数向量
- 是否将std矢量重新分配到默认的构造函数向量是擦除的好方法
- 运行提升for_each中的成员函数向量
- 创建和回推函数向量
- C++,指向函数向量的指针的STL映射