0

find_iffromを使いたいのです#include <algorithm>が、ベクトルが空かどうかが認識できないのが難点です。

次のスニペットを想定します

typedef std::vector< std::pair<int , int > > myVector;
myVector aVector;
struct equal: std::unary_function< std::pair<int , int >,bool >
{
  equal(const int &aNum) : theNum(aNum) {}
  bool operator()(const std::pair<int , int > &arg) const { return arg.first == theNum; }
  const int &theNum;
};
...

void check_and_insert(int num) {
   myVector::iterator it = find_if( aVector.begin(), aVector.end(), equal(num));
   if (it == aVector.end())
     aVector.push_back( std::make_pair(num, 1) );
   else
     ++dit->second;
}

aVectorが空であると仮定します。の結果はそうでfind_ifはないaVector.end()ので、elseどちらが間違っているかがわかります。これは find_if が機能する方法ですか? 奇妙ですが、最初の要素を挿入することで修正できます。

if (aVector.empty()) {
  aVector.push_back( std::make_pair(num, 1) );
  return;
}
// find_if...

これが唯一の解決策ですか?もっと良いアイデアはありますか?

UPDATE コメントに記載されているように、find_ifは正しく機能します。バグはaVector、別の関数での参照による呼び出しによって変更されたものでした。ご協力ありがとうございました。ご迷惑をおかけして申し訳ありません

4

4 に答える 4

0

このサンプル テスト コードでは、vector::end()(VS2010 SP1) との比較が正常に機能しているようです。

#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
    vector<int> v;
   /*   
    v.push_back(11);
    v.push_back(22);
    v.push_back(33);
   */ 

    auto it = find_if(v.begin(), v.end(), [](int x){ return (x % 2) == 1; } );    

    if (it != v.end())
        cout << *it;
    else
        cout << "Not found.";

    cout << endl;
}

出力:

見つかりません。


編集

コメント セクションで、コード スニペットのテストを依頼しました。実際、あなたのコード スニペットはコンパイル可能なスニペットではないため、他の人にテストしてもらいたい場合は、将来的にコンパイル可能なコード スニペットを提供することをお勧めします。

いずれにせよ、元のコードを調整しようとしました。たとえば、VS2010 SP1 で利用可能ないくつかの便利なC++11機能を使用して、ラムダ(ファンクターの代わりに) やauto.

そして、このテストコードは機能します:

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

typedef vector<pair<int, int>> MyVector;

void CheckAndInsert(MyVector& aVector, int num) 
{
    auto it = find_if( 
        aVector.begin(), aVector.end(), 
        [=](const std::pair<int, int>& elem)
        {
            return elem.first == num;
        }
    );

    if (it == aVector.end())
    {
        cout << "*** EMPTY VECTOR*** (num == " << num << ")\n";

        aVector.push_back( make_pair(num, 1) );
    }
    else
    {
        ++(it->second);
    }
}

int main()
{
    MyVector aVector;

    CheckAndInsert(aVector, 1);
    CheckAndInsert(aVector, 1);
    CheckAndInsert(aVector, 2);
    CheckAndInsert(aVector, 3);
    CheckAndInsert(aVector, 4);
    CheckAndInsert(aVector, 3);

    for (auto it = aVector.begin(); it != aVector.end(); ++it)
    {
        cout << "(" << (it->first) << ", " << (it->second) << ")\n";
    }
}

出力:

*** EMPTY VECTOR*** (num == 1)
*** EMPTY VECTOR*** (num == 2)
*** EMPTY VECTOR*** (num == 3)
*** EMPTY VECTOR*** (num == 4)
(1, 2)
(2, 1)
(3, 2)
(4, 1)

std::map<int, int>補足として、 の代わりに を使用することも検討してくださいstd::vector<std::pair<int, int>>

于 2013-06-17T10:54:04.663 に答える
0

aVector が空であると仮定します。find_if の結果が aVector.end() ではないことがわかります

ここに矛盾があります。空の範囲である空のベクトルにbegin(),を渡すと、 end() がすぐに返されます。end()find_if

于 2013-06-17T11:00:26.577 に答える