2つの配列が等しいかどうかを調べる必要がありますが、不一致の要素を見つけたら3番目の配列(他の2つと同じサイズ)それを無視することができます。C++は3つの配列を基準として等しいかどうかを確認します
私はそれを解決するための簡単なアルゴリズムを書くことができますが、これを解決するにはstdアルゴリズムを使用するC++のようなスタイルがあるかどうかを知りたいと思います。
例:
A = [1, 2, 3, 4, 5, 6]
B = [1, 2, 3, A, 5, 6]
M = [true, true, true, false, true, true]
N = [true, true, true, true, true, true]
bool equal1 = the_equal(begin_A, end_A, begin_B, begin_M); // Should return true, even if '4' is different from 'A' since in the same position of the found mismatch, in the M sequence, we have a false that indicates that position should be ignored.
bool equal2 = the_equal(begin_A, end_A, begin_B, begin_N); // Should return false, since '4' is different from 'A' and we have a true in sequence N for that position.
のようなものによって解決することができます
template<I1, I2, I3> // iterators
bool the_equal(I1 first1, I1 last1, I2 first2, I3 first3) {
while (first1 != last1) {
if (*first1 != *first2 && *first3 != false)
return false;
++first1; ++first2; ++first3;
}
return true;
}
編集:私は、私はC++ 98のによるツールチェーンの制限でこれを解決する必要があると言うことを忘れてしまった:(
if(first1!= * first2 && * first3!= false) '(if first1!= first2 && first3!= false)'とすることができますか? (参照解除のために追加されたアスタリスクに注意してください) – Arun
あなたは何をしたいですか?それはコーディングスタイルについてですか?私はこのような簡単な例でも、おそらくそれを行う方法はたくさんありますが、おそらくあなたが思いついたものより悪く見える可能性があります。 – user1810087
@Arunが修正されました。ありがとうございます。 – Valmir