2017-02-21 7 views
0

std::mismatchを使用して、構造体の2つのベクトルがまったく同じかどうかを確認しています。通常、私のプログラムでは、そうではありませんが、例外的な場合には起こるかもしれません。私は、次の検索documentationに:戻り値std ::等価ベクトルの不一致

「両方の配列に比べ要素が全て一致した場合、機能は、第二に、同じ相対位置にある要素にlast1する第一セット及び第二セットとペアを返しますシーケンス。

しかし、私は完全に同じである二つのベクトルを作成した場合、のstd ::不一致は値を返しません。私がやろうとしています何の小さな例:

#include <vector> 
#include <algorithm> 
#include <utility> 

struct structwithnumber { 
    int id; 
}; 

bool compare_structs (structwithnumber* struct1, structwithnumber* struct2) { 
    return struct1->id == struct2->id; 
}; 

bool compare_structvectors(std::vector<structwithnumber*> v1, std::vector<structwithnumber*> v2) { 
    if (v1.size() != v2.size()) 
    { 
     return false; 
    } 
    std::pair<std::vector<structwithnumber*>::iterator, std::vector<structwithnumber*>::iterator> mypair; 
    mypair = std::mismatch(v1.begin(), v1.end(), v2.begin(), compare_structs); 
    return (compare_structs(*mypair.first, *mypair.second)); 
} 

void simple_example() { 
    structwithnumber* struct1 = new structwithnumber(); 
    structwithnumber* struct2 = new structwithnumber(); 
    struct1->id = 1; 
    struct2->id = 2; 
    std::vector<structwithnumber*> v1; 
    std::vector<structwithnumber*> v2; 
    v1.push_back(struct1); 
    v1.push_back(struct2); 
    v2.push_back(struct1); 
    v2.push_back(struct2); 
    compare_structvectors(v1, v2); 
} 

私は視覚的なスタジオでこのコードを実行すると、15は、私はライン上のエラーを取得:さらに調査で

return (compare_structs(*mypair.first, *mypair.second)); 

それはmypair遺骨が判明不一致の後で空です。ドキュメントから、私はこれは各ベクトルの最後の値を返しますが。すべての要素が一致する2つのシーケンスが提示されたときのミスマッチの動作を誤解しましたか?

+2

「私はエラーが発生しました」 - あなたはそのエラーの性質をあなたの聴衆と分かち合いたいですか? (出力ウィンドウからコピー&ペースト) – molbdnilo

+1

[ここ](http://en.cppreference.com/w/cpp/algorithm/mismatch)は、より信頼性の高いリファレンスサイトです。 – molbdnilo

答えて

3

std::mismatchすべてが一致する場合は、少なくとも1つの過去イテレータを返します。あなたはcompare_structs(*mypair.first, *mypair.second)で行っているように逆参照することはできません。次のように

コードは、ケースをテストする必要があります

mypair = std::mismatch(v1.begin(), v1.end(), v2.begin(), compare_structs); 

if(mypair.first == v1.end()) { 
    // No mismatch, do something sensible 
} else { 
    return (compare_structs(*mypair.first, *mypair.second)); 
} 
+0

ありがとうございます。私はそれを次のように変更します: mypair = std :: mismatch(v1.begin()、v1.end()、v2.begin()、compare_structs); リターン(mypair。最初の== v1.end()); 2番目のステートメントは常に偽になります。 – MJV

0

をしかし、私は完全に同じである二つのベクトルを作成した場合、STDは::不一致が値を返さない、

もちろん、それは何かを返す必要があります。

さらなる調査では、ミスマッチ後にmypairが空のままになることが判明しました。

これは何を意味しますか? mypair はどのように空です?これはペアで、作成時に2人のメンバーしかいなかったし、mismatchの呼び出し後にはまだ2人のメンバーしかいない。

しかし、両方のシーケンスが一致する場合、それはendイテレータのペアです。 これは空の不一致のシーケンスが1組あります(これはまだ不可能な空のペアと同じではありません)。

これらのイテレータを逆参照することはできません。そのため、あなたのコードは壊れています。参照外にする前にテストする必要があります。

const bool v1_mismatch = (mypair.first != v1.end()); 
const bool v2_mismatch = (mypair.second != v2.end()); 
const bool identical = !(v1_mismatch || v2_mismatch); 

if (v1_mismatch && v2_mismatch) { 
    // this is the only case where you can dereference both 
    return compare_structs(*mypair.first, *mypair.second); 
} 
// otherwise you can dereference at most one iterator, 
// and if v1,v2 are identical, you can't dereference either 
+0

あなたはおそらく '||'を意味しています – Ap31

+0

ええ、私は最後のビットにどれくらいの細かいディテールを入れようとしているのか二人の心でした... – Useless

関連する問題