2016-09-05 8 views
-7

私のプロジェクトを完成させるために、ベクトル(STL)を返す関数を組み込む必要があります。 http://en.cppreference.comからオンラインコンパイラで実行以下の関数は、ベクトルを返すん:ベクトルをC++で返す関数

#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 ] 

奇妙なことは、私は私のプログラムのコードの同じ部分を使用しようとしていますときです

012:ベクトルは、5つの要素

私のプログラムが含まれていながら、結果は(これは、ベクトルの最後の要素だけを返している)異なっている(下記参照)

出力:

... 
(-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 ] 
[email protected]:~/Downloads/OpenCV/opencv-2.4.9/build$ 

誰かが私を助けることができますか?

私は間違っていますか?

+1

このような問題を解決する適切なツールは、デバッガです。スタックオーバーフローを尋ねる前に、コードを一行ずつ進める必要があります。詳しいヘルプは、[小さなプログラムをデバッグする方法(Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)を参照してください。最低限、問題を再現する[最小、完全、および検証可能](http://stackoverflow.com/help/mcve)の例と、その問題を再現するためのデバッガ。 –

+1

tl; dr申し訳ありません..... –

+0

'[]'の代わりに 'at'メソッドを使用して、バインドされた配列を探し出します。それがスタートです。 –

答えて

0

なぜこれはベクターを返すことと関係があるとお考えですか?私があなたのコードを読むと、std::max_elementは、の最後のの出現数が0.583333であるインデックス23で最大値を見つけています。次に、他の0.583333の出現と最後の出現を、==を使って比較します。 0.583333のすべての出現が正確にと等しいと確信していますか?通常、正確な等価性を必要とするのではなく、浮動小数点数をいくつかのイプシロン内で比較する方が良いでしょう。

私の理論をテストするには、it->real()==比較のブール結果を印刷して、完全一致または完全一致が5つあるかどうかを調べることができます。

関連する問題