2016-07-01 5 views
-1

複雑な複素数のベクトルの最大要素(複素数の実数成分に基づいて)を見つけようとしています。以下のコードは次のとおりです。私はエラーを取得しています複雑な要素のstd :: max_element

#include <iostream> 
#include <algorithm> 
#include <vector> 
#include <complex> 

using namespace std; 

int main() 
{ 
    vector<complex<double> > rts; 
    for (int i = -1; i<5; i++) 
     rts.push_back(complex<double>(i,0)); 

    complex<double> d; 
    d = std::max_element(rts.begin(), rts.end(), [](complex<double> const & lhs, complex<double> const & rhs) {return lhs.real() < rhs.real();}); 

    return 0; 
} 

'演算子=' の不一致は、(オペランドの型が 'のstd ::複雑な' と「__gnu_cxx :: __ normal_iteratorの*、STDあること: :ベクトル>> ')|

誰でも問題の原因を理解できますか?

ありがとうございます!

+0

http://en.cppreference.com/w/cpp/algorithm/max_element – juanchopanza

答えて

3

イテレータを返すmax_element機能は、デリファレンスをする必要があり、それは:

d = *std::max_element(rts.begin(), rts.end(), [](complex<double> const & lhs, complex<double> const & rhs) {return lhs.real() < rhs.real();}); 

注:これは実際のコードで使用された場合、あなたはあなたの範囲は最初の空でないことを確認したいと思う、そうでない場合無効なイテレータを逆参照することになります。

+0

完璧、ありがとう! – Billy

関連する問題