1
template < class ProteinTypeInfo >
 class T_PP_edge{
 private:
   std::pair<ProteinTypeInfo, ProteinTypeInfo> paired_info;
   unsigned priority;

public:
 // the constructor stores the Protein types in lexico ordering
    T_PP_edge(ProteinTypeInfo one, ProteinTypeInfo two){
    if (one<two) {paired_info.first = one; paired_info.second = two; } 
    else         {paired_info.first = two; paired_info.second = one; }
    priority = 0;
    }

  T_PP_edge& operator=(T_PP_edge& other){
   priority = other.priority;
   paired_info.first = other.paired_info.first;
   paired_info.second = other.paired_info.second;
   return *this ;
   }

   bool operator<(const T_PP_edge& a)const{
     if((*this).paired_info.first < a.paired_info.first) return true;
     else if((*this).paired_info.second < a.paired_info.second) return true;
     else return false;
   }

  const std::pair<ProteinTypeInfo, ProteinTypeInfo>& get_paired_info()const{
    return paired_info;}

 unsigned get_priority()const{return priority;}
 void set_priority(unsigned p) {priority = p;}
 };

// The lexico ordering used to compare pairs of proteins


template < class ProteinTypeInfo >
struct T_PP_edge_lexico_cmp{
public:
 typedef T_PP_edge<ProteinTypeInfo> PP_edge;
 bool operator()(const PP_edge& a, const PP_edge& b)const{
 if(a.get_paired_info().first < b.get_paired_info().first) return true;
 else if(a.get_paired_info().second < b.get_paired_info().second) return true;
 else return false;
 }

};

typedef std::string Protein_type;
typedef T_PP_edge<Protein_type> PP_edge;
typedef T_PP_edge_lexico_cmp<Protein_type> PP_edge_lexico_cmp;

int main(){
  typedef std::set<PP_edge, PP_edge_lexico_cmp> Edge_set;
  typedef Edge_set::iterator set_iterator;
  Edge_set edgeP_set, edgeR_set;
  PP_edge e1("aa", "bb"), e2("aa", "cc"), e3("bb", "cc");
  PP_edge e4("aa", "dd"), e5("aa", "cc"), e6("bb", "cc");
  edgeP_set.insert(e1); edgeP_set.insert(e2); edgeP_set.insert(e3);
  edgeR_set.insert(e4); edgeR_set.insert(e5); edgeR_set.insert(e6);
  Edge_set result;
  std::set_intersection(edgeP_set.begin(), edgeP_set.end(), edgeR_set.begin(), 
  edgeR_set.end(), result.begin());
  set_iterator itb = result.begin(), ite = result.end();

  for(; itb != ite; ++itb)
     std::cout << itb->get_paired_info().first << "-" << itb->get_paired_info().second <<  "\n";

  return 0;
  }            

注意:包括 set_intersection 函数的算法。

上面的代码在编译时出现以下错误:

g++ test_code.cpp
In file included from /usr/lib/gcc/i686-redhat-linux/4.5.1/../../../../include/c++/4.5.1/algorithm:63:0,
                 from test_code.cpp:4:
/usr/lib/gcc/i686-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h: In function ‘_OIter std::set_intersection(_IIter1, _IIter1, _IIter2, _IIter2, _OIter) [with _IIter1 = std::_Rb_tree_const_iterator<T_PP_edge<std::basic_string<char> > >, _IIter2 = std::_Rb_tree_const_iterator<T_PP_edge<std::basic_string<char> > >, _OIter = std::_Rb_tree_const_iterator<T_PP_edge<std::basic_string<char> > >]’:
test_code.cpp:79:111:   instantiated from here
/usr/lib/gcc/i686-redhat-linux/4.5.1/../../../../include/c++/4.5.1/bits/stl_algo.h:5648:6: error: no match for ‘operator=’ in ‘__result.std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = T_PP_edge<std::basic_string<char> >, const _Tp& = const T_PP_edge<std::basic_string<char> >&]() = __first1.std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = T_PP_edge<std::basic_string<char> >, const _Tp& = const T_PP_edge<std::basic_string<char> >&]()’
test_code.cpp:21:14: note: candidate is: T_PP_edge<ProteinTypeInfo>& T_PP_edge<ProteinTypeInfo>::operator=(T_PP_edge<ProteinTypeInfo>&) [with ProteinTypeInfo = std::basic_string<char>, T_PP_edge<ProteinTypeInfo> = T_PP_edge<std::basic_string<char> >]

Compilation exited abnormally with code 1 at Mon Nov 26 21:28:38

我怀疑这可能是由于赋值运算符中的 const 不正确。请帮帮我。谢谢。

4

1 回答 1

4

result.begin()不是预期的 OutputIterator set_intersection

采用

std::inserter(result, result.end())

即使result.begin()可以解决,它通常也只是段错误。std::vector在不保留足够内存的情况下尝试使用 a 。

并且,为了您的读者,请:摆脱空白。使用有意义的空格来显示程序的逻辑流程。

另外,你operator=是不必要的。它与默认生成的相同。

于 2012-11-26T20:53:35.260 回答