开发者

counting number of occurrences of an object in a list

I am trying to find the number of occurrences of an object in a list:

class Complex{
  double re, im; 
 public:
   Complex (double r, double i):re(r), im(i){}
   Complex (){re = im = 0;}

   friend bool    operator == (Complex, Complex); 
};

bool operator == (Complex  a,  Complex b){
 return a.re == b.re and a.im == b.im;
}

template <class ContainerType, class ElementType>
int const count (ContainerType const & container, ElementType const & element){
  int count = 0;
  ty开发者_JS百科pename ContainerType::const_iterator i = std::find (container.begin(), container.end(), element);

  while (i != container.end()){
   ++count;
   i = std::find (i + 1, container.end(), element);
  }
 return count;
}

int main(){
 std::list <Complex> lc;
 lc.push_front (Complex (1.2, 3.4));

 std::cout << count (std::string("abhi"), 'a') << '\n';
 std::cout << count (lc, Complex (1.2, 3.4)) << '\n';

 return 0;
}

I get this error with g++ 4.5:

templatizedcharOccurences.c++: In function ‘const int count(const ContainerType&, const ElementType&) [with ContainerType = std::list<Complex>, ElementType = Complex]’:
templatizedcharOccurences.c++:51:44:   instantiated from here
templatizedcharOccurences.c++:41:4: error: no match for ‘operator+’ in ‘i + 1’
templatizedcharOccurences.c++:22:9: note: candidate is: Complex operator+(Complex, Complex)

Why is it complaining about i+1? Clearly, isn't i an iterator (pointer) and not a Complex object?


You could simplify the code a lot in one of two ways:

Use std::count or one of its variants to do the counting. So:

return std::count(container.begin(), container.end(), element);

Or just use a simple for loop like you already do but iterate it from beginning to end and do the counting yourself, like so:

int count = 0;
for (ContainerType::const_iterator it = container.begin(); it != container.end(); ++it)
{
    if (*it == element) ++count;
}
return count;

Since you're already using parts of the STL I would suggest using the first method.


It seems Iterator does not have overloaded operator+(int), try:

class Complex{
  double re, im; 
 public:
   Complex (double r, double i):re(r), im(i){}
   Complex (){re = im = 0;}

   friend bool    operator == (Complex, Complex); 
};

bool operator == (Complex  a,  Complex b){
 return a.re == b.re and a.im == b.im;
}

template <class ContainerType, class ElementType>
int const count (ContainerType const & container, ElementType const & element){
  int count = 0;
  typename ContainerType::const_iterator i = std::find (container.begin(), container.end(), element);

  while (i != container.end()){
   ++count;
   i = std::find (++i, container.end(), element);
  }
 return count;
}

int main(){
 std::list <Complex> lc;
 lc.push_front (Complex (1.2, 3.4));

 std::cout << count (std::string("abhi"), 'a') << '\n';
 std::cout << count (lc, Complex (1.2, 3.4)) << '\n';

 return 0;
}

I hope this would work.


It is a list iterator which doesn't have random access operations. You want to do:

++i;
i = std::find (i, container.end(), element);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜