I would like to find the highest value within an array of complex numbers.
Let assume this is the function I want to modify in order to get the highest values of array y : - //iterDelayEst.cpp
-
#include <vector>
-
#include <algorithm>
-
#include <iterator>
-
-
-
double iterDelayEst(int n,CArray& x, CArray& y)
-
{
-
/**********************************************constants************************************************
-
*******************************************************************************************************/
-
//exit if uncertainty below threshold
-
double thr_samples = 1e-7;
-
-
//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 nf3(0.0,0.0);
-
-
int j;
-
-
for ( j = 0 ; j < n ; j++ )
-
-
{
-
auto nf1 = ((x * x.apply(std::conj)) * (y * y.apply(std::conj)));
-
nf3 += nf1[j];
-
}
-
-
auto nf2 =std::sqrt(nf3);
-
auto nf =nf2/(double)n;
-
cout << "nf3" << nf3 <<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;
-
int k=0;
-
Complex MAX =0;
-
/***************************************iteration loop**********************************************
-
**************************************************************************************************/
-
-
//for(i=0; i<nIter; i++)
-
std::vector<Complex> v;
-
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;
-
v.push_back(y);
-
}
-
-
std::vector<Complex>::iterator result ;
-
const Complex maxim = *max_element(v.begin(), v.end());
-
for(result = std::find(v.begin(), v.end(), maxim); result != v.end(); result = std::find(result + 1, v.end(), maxim))
-
{
-
cout << result - v.begin()+1 << endl;
-
//printf("found at index %f\n", result -v.begin() +1 );
-
}
-
-
-
-
-
/**************************detect peak********************************************************/
-
-
}
[code]//main.cpp
#include <complex>
#include <iostream>
#include <valarray>
#include <malloc.h>
#include <string>
#include <stdlib.h>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cmath>
#include "fft.cpp"
#include "cs_delay.cpp"
#include "iterDelayEst.cpp"
using namespace std;
char filename[] = "myfile.txt";
char filename2[] = "myfile2.txt";
typedef std::complex<double> Complex;
typedef std::valarray <Complex> CArray;
/************************************************** *********************************************
* function declarations
************************** ************************************************** ****** ***********/
void fft(CArray& x);
void ifft(CArray& x);
std::vector<double> binFreq(int n);
void cs_delay(CArray& x, int rate_hz, int delay_s, int n);
double iterDelayEst(int n, CArray& x, CArray& x2);
int main()
{
int dTest_samples;
int cTest;
int n=32;
int i;
int j;
double x [n];
double y [n];
int rate_hz=1;
int delay_s=30;
/*****************************getting x*******************************/
string line;
double Result;
ifstream myfile (filename);
if (myfile.is_open())
{
for ( i = 0 ; (i < n) && (myfile >> x[i]) ; ++i)
cout << line << '\n';
stringstream convert(line);
if ( !(convert >> Result) )
Result = 0;
x[i]=Result;
}
else cout << "Unable to open file";
/*****************************getting y******************************/
string line2;
double Result2;
ifstream myfile2 (filename2);
if (myfile2.is_open())
{
for ( i = 0 ; (i < n) && (myfile2 >> x[i]) ; ++i)
cout << line2 << '\n';
stringstream convert(line2);
if ( !(convert >> Result2) )
Result2 = 0;
y[i]=Result2;
}
else cout << "Unable to open file2";
/************************************************** *********************/
/*********************for x******************/
Complex test[n];
for ( i = 0 ; i < n ; ++i )
test[i] = x[i];
CArray data(test,n);
/*********************for y******************/
Complex test2[n];
for ( j = 0 ; j < n ; ++j )
test2[j] = y[j];
CArray data2(test2,n);
// forward fft
fft(data);
std::cout << "fft" << std::endl;
for (int i = 0; i <n; ++i)
{
cout << data[i] << endl;
}
// inverse fft
ifft(data);
std::cout << std::endl << "ifft" << std::endl;
for (int i = 0; i <n; ++i)
{
std::cout << data[i] << std::endl;
}
cs_delay(data, 1, 6, n);
for (int i = 0; i <n; ++i)
{
std::cout << data[i] << std::endl;
}
iterDelayEst(n, data, data2 );
return 0;
}[code] - //fft.cpp
-
using namespace std;
-
const double PI = 3.141592653589793238460;
-
-
-
/************************************************************************************************
-
* Cooley–Tukey FFT (in-place, divide-and-conquer) *
-
* Higher memory requirements and redundancy although more intuitive *
-
************************************************************************************************/
-
-
-
typedef std::complex<double> Complex;
-
typedef std::valarray<Complex> CArray;
-
// Cooley–Tukey FFT (in-place, divide-and-conquer)
-
// Higher memory requirements and redundancy although more intuitive
-
void fft(CArray& x)
-
{
-
const size_t N = x.size();
-
if (N <= 1) return;
-
-
// divide
-
CArray even = x[std::slice(0, N/2, 2)];
-
CArray odd = x[std::slice(1, N/2, 2)];
-
-
// conquer
-
fft(even);
-
fft(odd);
-
-
// combine
-
for (size_t k = 0; k < N/2; ++k)
-
{
-
Complex t = std::polar(1.0, -2 * PI * k / N) * odd[k];
-
x[k ] = even[k] + t;
-
x[k+N/2] = even[k] - t;
-
}
-
}
-
// inverse fft (in-place)
-
void ifft(CArray& x)
-
-
{
-
// conjugate the complex numbers
-
x = x.apply(std::conj);
-
-
// forward fft
-
fft( x );
-
-
// conjugate the complex numbers again
-
x = x.apply(std::conj);
-
-
// scale the numbers
-
x /= x.size();
-
}
-
- //cs_delay.cpp
-
using namespace std;
-
-
typedef std::complex<double> Complex;
-
typedef std::valarray<Complex> CArray;
-
-
void cs_delay(CArray& x, int rate_hz, int delay_s, int n)
-
{
-
-
const size_t N = x.size();
-
if (N <= 1) return;
-
int j;
-
double cycLen_s;
-
double nCyc;
-
double* f = new double[n];
-
double* phase = new double[n];
-
Complex* rot = new Complex[n];
-
cycLen_s = n/rate_hz;
-
nCyc = delay_s / cycLen_s;
-
/*************************************************************/
-
for ( j = 0 ; j < n ; j++ ){
-
-
f[j] =j+floor(n/2);
-
f[j] =fmod(f[j], n);
-
f[j] =f[j]-floor(n/2);
-
phase[j] = -2 * PI * f[j] * nCyc;
-
rot[j] = exp(1i*phase[j]);
-
std::cout << "rot["<<j<<"] ="<<rot[j] <<std::endl;
-
fft(x);
-
x *= rot[j];
-
ifft(x);
-
}
-
/*************************************************************/
-
delete [] f;
-
delete [] phase;
-
delete [] rot;
-
}
- //binFreg.cpp
-
std::vector<double> binFreq(int n)
-
{
-
int j;
-
std::vector<double> f(n);
-
-
for ( j = 0 ; j < n ; j++ ){
-
-
f[j] =(fmod(j+(floor(n/2)), n)-floor(n/2))/n;
-
}
-
return f;
-
}
While trying to run the program I am receiving following error messages :
Output: - serge@ubuntu:~/Downloads/OpenCV/opencv-2.4.9/build$ g++ -o myfft myfft.cpp -std=c++14
-
In file included from myfft.cpp:16:0:
-
iterDelayEst.cpp: In function ‘double iterDelayEst(int, CArray&, CArray&)’:
-
iterDelayEst.cpp:67:38: error: no matching function for call to ‘std::vector<std::complex<double> >::push_back(CArray&)’
-
v.push_back(y);
-
^
-
iterDelayEst.cpp:67:38: note: candidates are:
-
In file included from /usr/include/c++/4.9/vector:64:0,
-
from /usr/include/c++/4.9/bits/random.h:34,
-
from /usr/include/c++/4.9/random:49,
-
from /usr/include/c++/4.9/bits/stl_algo.h:66,
-
from /usr/include/c++/4.9/algorithm:62,
-
from /usr/include/c++/4.9/valarray:38,
-
from myfft.cpp:3:
-
/usr/include/c++/4.9/bits/stl_vector.h:913:7: note: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = std::complex<double>; _Alloc = std::allocator<std::complex<double> >; std::vector<_Tp, _Alloc>::value_type = std::complex<double>]
-
push_back(const value_type& __x)
-
^
-
/usr/include/c++/4.9/bits/stl_vector.h:913:7: note: no known conversion for argument 1 from ‘CArray {aka std::valarray<std::complex<double> >}’ to ‘const value_type& {aka const std::complex<double>&}’
-
/usr/include/c++/4.9/bits/stl_vector.h:931:7: note: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = std::complex<double>; _Alloc = std::allocator<std::complex<double> >; std::vector<_Tp, _Alloc>::value_type = std::complex<double>]
-
push_back(value_type&& __x)
-
^
-
/usr/include/c++/4.9/bits/stl_vector.h:931:7: note: no known conversion for argument 1 from ‘CArray {aka std::valarray<std::complex<double> >}’ to ‘std::vector<std::complex<double> >::value_type&& {aka std::complex<double>&&}’
-
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:71:0,
-
from /usr/include/c++/4.9/bits/char_traits.h:39,
-
from /usr/include/c++/4.9/ios:40,
-
from /usr/include/c++/4.9/istream:38,
-
from /usr/include/c++/4.9/sstream:38,
-
from /usr/include/c++/4.9/complex:45,
-
from myfft.cpp:1:
-
/usr/include/c++/4.9/bits/predefined_ops.h: In instantiation of ‘bool __gnu_cxx::__ops::_Iter_less_iter::operator()(_Iterator1, _Iterator2) const [with _Iterator1 = __gnu_cxx::__normal_iterator<std::complex<double>*, std::vector<std::complex<double> > >; _Iterator2 = __gnu_cxx::__normal_iterator<std::complex<double>*, std::vector<std::complex<double> > >]’:
-
/usr/include/c++/4.9/bits/stl_algo.h:5473:30: required from ‘_ForwardIterator std::__max_element(_ForwardIterator, _ForwardIterator, _Compare) [with _ForwardIterator = __gnu_cxx::__normal_iterator<std::complex<double>*, std::vector<std::complex<double> > >; _Compare = __gnu_cxx::__ops::_Iter_less_iter]’
-
/usr/include/c++/4.9/bits/stl_algo.h:5496:41: required from ‘_FIter std::max_element(_FIter, _FIter) [with _FIter = __gnu_cxx::__normal_iterator<std::complex<double>*, std::vector<std::complex<double> > >]’
-
iterDelayEst.cpp:71:71: required from here
-
/usr/include/c++/4.9/bits/predefined_ops.h:42:23: error: no match for ‘operator<’ (operand types are ‘std::complex<double>’ and ‘std::complex<double>’)
-
{ return *__it1 < *__it2; }
-
^
-
/usr/include/c++/4.9/bits/predefined_ops.h:42:23: note: candidates are:
-
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:67:0,
-
from /usr/include/c++/4.9/bits/char_traits.h:39,
-
from /usr/include/c++/4.9/ios:40,
-
from /usr/include/c++/4.9/istream:38,
-
from /usr/include/c++/4.9/sstream:38,
-
from /usr/include/c++/4.9/complex:45,
-
from myfft.cpp:1:
-
/usr/include/c++/4.9/bits/stl_iterator.h:837:5: note: template<class _IteratorL, class _IteratorR, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_IteratorL, _Container>&, const __gnu_cxx::__normal_iterator<_IteratorR, _Container>&)
-
operator<(const __normal_iterator<_IteratorL, _Container>& __lhs,
-
^
-
/usr/include/c++/4.9/bits/stl_iterator.h:837:5: note: template argument deduction/substitution failed:
-
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:71:0,
-
from /usr/include/c++/4.9/bits/char_traits.h:39,
-
from /usr/include/c++/4.9/ios:40,
-
from /usr/include/c++/4.9/istream:38,
-
from /usr/include/c++/4.9/sstream:38,
-
from /usr/include/c++/4.9/complex:45,
-
from myfft.cpp:1:
-
/usr/include/c++/4.9/bits/predefined_ops.h:42:23: note: ‘std::complex<double>’ is not derived from ‘const __gnu_cxx::__normal_iterator<_IteratorL, _Container>’
-
{ return *__it1 < *__it2; }
-
^
-
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:67:0,
-
from /usr/include/c++/4.9/bits/char_traits.h:39,
-
from /usr/include/c++/4.9/ios:40,
-
from /usr/include/c++/4.9/istream:38,
-
from /usr/include/c++/4.9/sstream:38,
-
from /usr/include/c++/4.9/complex:45,
-
from myfft.cpp:1:
-
/usr/include/c++/4.9/bits/stl_iterator.h:844:5: note: template<class _Iterator, class _Container> bool __gnu_cxx::operator<(const __gnu_cxx::__normal_iterator<_Iterator, _Container>&, const __gnu_cxx::__normal_iterator<_Iterator, _Container>&)
-
operator<(const __normal_iterator<_Iterator, _Container>& __lhs,
-
^
-
/usr/include/c++/4.9/bits/stl_iterator.h:844:5: note: template argument deduction/substitution failed:
-
In file included from /usr/include/c++/4.9/bits/stl_algobase.h:71:0,
-
from /usr/include/c++/4.9/bits/char_traits.h:39,
Can someone help ?
I found a much elegant way to do that : - #include <cstddef>
-
#include <numeric>
-
#include <complex>
-
#include <valarray>
-
#include <iostream>
-
-
using Complex = std::complex<double>;
-
using CArray = std::valarray<Complex>;
-
-
std::size_t max_index(CArray const& y) {
-
struct acc_t {
-
double max_value;
-
std::size_t max_idx, current_idx;
-
-
constexpr acc_t next() const { return {max_value, max_idx, current_idx + 1}; }
-
constexpr acc_t next_with(Complex const c) const {
-
return {c.real(), current_idx, current_idx + 1};
-
}
-
};
-
-
return std::accumulate(
-
std::begin(y), std::end(y), acc_t{},
-
[](acc_t const acc, Complex const c) {
-
return c.real() < acc.max_value
-
? acc.next()
-
: acc.next_with(c);
-
}
-
).max_idx;
-
}
-
-
void test(CArray const& y) {
-
auto const max_idx = max_index(y);
-
std::cout << "The max is " << y[max_idx] << " at index " << max_idx << '\n';
-
}
-
-
int main() {
-
-
test({{1, 2}, {3, 4}, { 2, 0}, {9, 0}, {7, 0}, {9, 0}});
-
}
6 1918
I'm not sure exactly what you are doing. To find the max complex number in an array should look like: - class Complex
-
{
-
double X;
-
double Y;
-
public:
-
bool operator<(Complex& first);
-
};
-
-
int main()
-
{
-
vector<Complex> array;
-
-
vector<Complex>::iterator itr = array.begin();
-
-
Complex Max = array[0];
-
-
while (itr != array.end())
-
{
-
if (Max < *itr)
-
{
-
Max = *itr;
-
}
-
++itr;
-
-
}
-
-
//Max has the highest value
-
-
}
What am I missing?
Sorry, I am a beginner in c++,
Indeed,the title should be "How to find indices corresponding to the highest value in an array of complex numbers"
I would like to translate the following matlab code into C++:
ix = find(xcorr == max(xcorr));
the matlab code returns indices of elements corresponding to the highest value
xcorr is an array of complex numbers
I have tried to put your example in practice but it seems like I can't print that highest element: - #include <iostream>
-
#include <complex>
-
#include <vector>
-
#include <algorithm>
-
#include <stdlib.h>
-
#include <fstream>
-
#include <cstdlib>
-
-
using namespace std;
-
-
class Complex
-
{
-
double X;
-
double Y;
-
public:
-
bool operator<(Complex& first);
-
-
};
-
-
std::ostream& operator<<(std::ostream& os, const Complex& obj)
-
{
-
os << obj;
-
return os;
-
}
-
-
int main()
-
{
-
vector<Complex> array;
-
array.push_back((1,1));
-
array.push_back((-1,-1));
-
array.push_back((7,7));
-
array.push_back((3,3));
-
array.push_back((10,10));
-
-
vector<Complex>::iterator itr = array.begin();
-
-
Complex Max = array[0];
-
-
while (itr != array.end())
-
{
-
if (Max < *itr)
-
{
-
Max = *itr;
-
-
}
-
++itr;
-
//printf("found at index %f\n", Max );
-
-
}
-
-
//Max has the highest value
-
-
}
-
Output :
You can't print your element because obj is a user-defined type. The std::operator<< only works with built-in types.
You would write member functions to access the X and Y of the class Complex and then call those functions: - class Complex
-
{
-
double X;
-
double Y;
-
public:
-
bool operator<(Complex& first);
-
double GetReal() { return X; };
-
double GetImaginary() { return Y; };
-
-
-
};
-
and then in main(): - cout << Max.GetReal() << Max.GetImaginary() << endl;
Hi,
it seems to not work : - #include <iostream>
-
#include <complex>
-
#include <vector>
-
#include <algorithm>
-
#include <stdlib.h>
-
#include <fstream>
-
-
-
using namespace std;
-
-
class Complex
-
{
-
double X;
-
double Y;
-
public:
-
bool operator<<(Complex& first);
-
double GetReal() { return X; };
-
double GetImaginary() { return Y; };
-
};
-
-
-
int main()
-
{
-
vector<Complex> array;
-
array.push_back((1,1));
-
array.push_back((-1,-1));
-
array.push_back((7,7));
-
array.push_back((3,3));
-
array.push_back((10,10));
-
-
vector<Complex>::iterator itr = array.begin();
-
-
Complex Max = array[0];
-
-
while (itr != array.end())
-
{
-
if (Max << *itr)
-
{
-
Max = *itr;
-
-
}
-
++itr;
-
cout << Max.GetReal() << Max.GetImaginary() << endl;
-
-
}
-
-
}
I am getting following error messages : - main.cpp: In function 'int main()':
-
main.cpp:25:24: warning: left operand of comma operator has no effect [-Wunused-value]
-
array.push_back((1,1));
-
^
-
main.cpp:25:26: error: no matching function for call to 'std::vector<Complex>::push_back(int)'
-
array.push_back((1,1));
-
^
-
In file included from /usr/local/include/c++/6.1.0/vector:64:0,
-
from main.cpp:3:
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
-
push_back(const value_type& __x)
-
^~~~~~~~~
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from 'int' to 'const value_type& {aka const Complex&}'
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
-
push_back(value_type&& __x)
-
^~~~~~~~~
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from 'int' to 'std::vector<Complex>::value_type&& {aka Complex&&}'
-
main.cpp:26:26: warning: left operand of comma operator has no effect [-Wunused-value]
-
array.push_back((-1,-1));
-
^
-
main.cpp:26:28: error: no matching function for call to 'std::vector<Complex>::push_back(int)'
-
array.push_back((-1,-1));
-
^
-
In file included from /usr/local/include/c++/6.1.0/vector:64:0,
-
from main.cpp:3:
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
-
push_back(const value_type& __x)
-
^~~~~~~~~
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from 'int' to 'const value_type& {aka const Complex&}'
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
-
push_back(value_type&& __x)
-
^~~~~~~~~
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from 'int' to 'std::vector<Complex>::value_type&& {aka Complex&&}'
-
main.cpp:27:24: warning: left operand of comma operator has no effect [-Wunused-value]
-
array.push_back((7,7));
-
^
-
main.cpp:27:26: error: no matching function for call to 'std::vector<Complex>::push_back(int)'
-
array.push_back((7,7));
-
^
-
In file included from /usr/local/include/c++/6.1.0/vector:64:0,
-
from main.cpp:3:
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
-
push_back(const value_type& __x)
-
^~~~~~~~~
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from 'int' to 'const value_type& {aka const Complex&}'
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
-
push_back(value_type&& __x)
-
^~~~~~~~~
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from 'int' to 'std::vector<Complex>::value_type&& {aka Complex&&}'
-
main.cpp:28:24: warning: left operand of comma operator has no effect [-Wunused-value]
-
array.push_back((3,3));
-
^
-
main.cpp:28:26: error: no matching function for call to 'std::vector<Complex>::push_back(int)'
-
array.push_back((3,3));
-
^
-
In file included from /usr/local/include/c++/6.1.0/vector:64:0,
-
from main.cpp:3:
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
-
push_back(const value_type& __x)
-
^~~~~~~~~
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from 'int' to 'const value_type& {aka const Complex&}'
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
-
push_back(value_type&& __x)
-
^~~~~~~~~
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from 'int' to 'std::vector<Complex>::value_type&& {aka Complex&&}'
-
main.cpp:29:25: warning: left operand of comma operator has no effect [-Wunused-value]
-
array.push_back((10,10));
-
^~
-
main.cpp:29:28: error: no matching function for call to 'std::vector<Complex>::push_back(int)'
-
array.push_back((10,10));
-
^
-
In file included from /usr/local/include/c++/6.1.0/vector:64:0,
-
from main.cpp:3:
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
-
push_back(const value_type& __x)
-
^~~~~~~~~
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:914:7: note: no known conversion for argument 1 from 'int' to 'const value_type& {aka const Complex&}'
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: candidate: void std::vector<_Tp, _Alloc>::push_back(std::vector<_Tp, _Alloc>::value_type&&) [with _Tp = Complex; _Alloc = std::allocator<Complex> std::vector<_Tp, _Alloc>::value_type = Complex]
-
push_back(value_type&& __x)
-
^~~~~~~~~
-
/usr/local/include/c++/6.1.0/bits/stl_vector.h:932:7: note: no known conversion for argument 1 from 'int' to 'std::vector<Complex>::value_type&& {aka Complex&&}'
You must use functions the way they were designed.
This code:
needs a push_back function that has integer arguments, In this program the push_back requires a Complex argument. You must build the Complex object then you can do the push_back: - Complex obj;
-
obj.SetReal(1);
-
obj.SetImaginary(1);
-
-
array.push_back(obj);
-
You will need to write the SetReal SetImaginary member function s of Complex. Just follow the pattern for the GetReal and GetImaginary. You can use a constructor instead.
Keep posting. You are making progress.
I did not go through the whole code. Fix these errors and repost
I found a much elegant way to do that : - #include <cstddef>
-
#include <numeric>
-
#include <complex>
-
#include <valarray>
-
#include <iostream>
-
-
using Complex = std::complex<double>;
-
using CArray = std::valarray<Complex>;
-
-
std::size_t max_index(CArray const& y) {
-
struct acc_t {
-
double max_value;
-
std::size_t max_idx, current_idx;
-
-
constexpr acc_t next() const { return {max_value, max_idx, current_idx + 1}; }
-
constexpr acc_t next_with(Complex const c) const {
-
return {c.real(), current_idx, current_idx + 1};
-
}
-
};
-
-
return std::accumulate(
-
std::begin(y), std::end(y), acc_t{},
-
[](acc_t const acc, Complex const c) {
-
return c.real() < acc.max_value
-
? acc.next()
-
: acc.next_with(c);
-
}
-
).max_idx;
-
}
-
-
void test(CArray const& y) {
-
auto const max_idx = max_index(y);
-
std::cout << "The max is " << y[max_idx] << " at index " << max_idx << '\n';
-
}
-
-
int main() {
-
-
test({{1, 2}, {3, 4}, { 2, 0}, {9, 0}, {7, 0}, {9, 0}});
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics
by: mike |
last post by:
I have ListArray with number in Eg:
1, 1.456, 2.43, 4, 6.78
next i have a decimal variable containing one number EG:
1.786
Could someone please tell me how i find the "closest match" number...
|
by: Ada |
last post by:
hello folks,
just wanna get some feedback from someone here who has more experience. :-)
i'm developing a small app that require searching for the highest index
within a directory.
i was...
|
by: Jan |
last post by:
Hi there,
Is there a fast way to get the highest value from an array?
I've got the array strStorage(intCounter)
I tried something but it all and's to nothing
If someone good helpme, TIA
|
by: Russ |
last post by:
I'd like to get output formatting for my own classes that mimics the
built-in output formatting. For example,
>>> x = 4.54
>>> print "%4.2f" % x
4.54
In other words, if I substitute a class...
|
by: Ros |
last post by:
Peeps,
I need help with trying to find a value in array of arrays.
Public Module myModule
Private Site_Access() As Array
Property Set_SA()
Get
Return Site_Access
End Get
|
by: =?Utf-8?B?cm9kY2hhcg==?= |
last post by:
hey all,
i have an int array and was just wondering if there is a way to get the
highest value in the array?
for instance,
int myValues = new int { 0, 1, 2 }
highest value is 2.
thanks,
|
by: X106 |
last post by:
how to using the technique of Array so that I can input 5 values and then the program will find out which value is the large one.
Who can help me????
THANK!!!!!
|
by: jacob navia |
last post by:
The C99 standard forgot to define the printf equivalent for complex numbers
Since I am revising the lcc-win implementation of complex numbers
I decided to fill this hole with
"Z"
for...
|
by: void main |
last post by:
I'm rather new to complex numbers in C and was wondering, how do I
initialize a complex variable properly if the imaginary part is 0.
I tried
--------
#include <complex.h>
float complex c...
|
by: mimic01 |
last post by:
I can't get it to work to find the highest value in the given matrix. Any ideas? (total C beginner here).
Also in this one i get the user to fill in the array values but which would be the fastest...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
| |