使用STL和一元函数适配仿函数检查列表成员资格

Sha*_*ter 0 c++ iterator stl generic-programming functor

我试图编写一个简短的实用程序函数,它接受两个std :: pair项并测试它们的相等性,但忽略了元素的排序.另外(这是我遇到麻烦的地方)我写了一个函数来获取那些std :: pair项的容器并测试容器中给定对参数的成员资格.

/* A quick functor way to check the identity of the two items of a pair to see if each pair contains the same items regardless of order */
template <class T>
class EqualPairs : public std::binary_function<T,T,bool> {
  T arg2;

  public:
  explicit EqualPairs (const T& x) : arg2(x) { }

  bool operator() (const T& arg1) { 
    bool same = false;
    if (arg1 == arg2 || (arg1.first == arg2.second && arg1.second == arg2.first))
      same = true;
    return same;
  }
};

/* checks to see if the give pair p is a member of the list of pairs l. The pairs are compared disregarding the order of the pair elements (i.e. (4,2) == (2,4)) */
template <class P>
bool PairListMember (const P& p, const std::vector<P>& l)
{
  std::vector<P>::iterator it;
  it = find_if (l.begin(), l.end(), EqualPairs<P>(p));
  bool member_of_list = (it != l.end()) ? true : false;
  return member_of_list;
}
Run Code Online (Sandbox Code Playgroud)

我想不出允许通用容器选择的简洁方法,所以我现在硬编码std :: vector作为容器类型.关于使容器类型通用的帮助也将受到赞赏,但是现在我只想让上面的内容进行编译和工作.我得到的错误是:

In function ‘bool PairListMember(const P&, const std::vector<P, std::allocator<_CharT> >&)’:

    error: expected `;' before ‘it’
    error: ‘it’ was not declared in this scope

In function ‘bool PairListMember(const P&, const std::vector<P, std::allocator<_CharT> >&) [with P = std::pair<int, int>]’:

    error: dependent-name ‘std::vector<P,std::allocator<_CharT> >::iterator’ is parsed as a non-type, but instantiation yields a type
    note: say ‘typename std::vector<P,std::allocator<_CharT> >::iterator’ if a type is meant
Run Code Online (Sandbox Code Playgroud)

通过添加建议的'typename'来更改代码只会导致以下错误:

error: no match for ‘operator=’ in ‘it = std::find_if [with _InputIterator = __gnu_cxx::__normal_iterator<const std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >, _Predicate = EqualPairs<std::pair<int, int> >](((const std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >*)l)->std::vector<_Tp, _Alloc>::begin [with _Tp = std::pair<int, int>, _Alloc = std::allocator<std::pair<int, int> >](), ((const std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > >*)l)->std::vector<_Tp, _Alloc>::end [with _Tp = std::pair<int, int>, _Alloc = std::allocator<std::pair<int, int> >](), EqualPairs<std::pair<int, int> >(((const std::pair<int, int>&)((const std::pair<int, int>*)p))))’

/usr/include/c++/4.2/bits/stl_iterator.h:637: note: candidates are: __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >& __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >::operator=(const __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int>, std::allocator<std::pair<int, int> > > >&)
Run Code Online (Sandbox Code Playgroud)

Cha*_*via 5

对于编译器错误,您需要使用typename关键字.

typename std::vector<P>::iterator it;
Run Code Online (Sandbox Code Playgroud)

iterator是一个typename,即它指的是std :: vector中的嵌入类型.当您::在模板中使用运算符访问typename时,需要使用typename关键字,以便编译器知道它是类型的名称,而不是类中某个变量或函数的名称.

编辑:另外,你需要使用a const_iterator,因为在这种情况下你的向量是const.

typename std::vector<P>::const_iterator it;
Run Code Online (Sandbox Code Playgroud)