post  Set iterator difference

kunigami (14)   Link to this post
Hi all!
I'd like to know if C++ STL has a way to count elements in a certain range from a set. I tried using lower_bound/upper_bound, but I couldn't subtract a set<int>::iterator from another.

Is there any other way to do that using STL?

Thank you very much,
Bazzy (4120)   Link to this post
Try set::size or set::count
http://www.cplusplus.com/reference/stl/set/
seymore15074 (449)   Link to this post
Like distance?
http://www.cplusplus.com/reference/std/iterator/distance.html

Just be sure to read the information and make sure it works with the container you are using...
jsmith (3807)   Link to this post
Another way is via std::count_if and boost::lambda (the boost::lambda just allows me to do this with one line of code instead of writing a function object):

1
2
cout << std::count_if( s.begin(), s.end(),
  boost::lambda::_1 >= MIN && boost::lambda::_1 <= MAX );


seymore15074 (449)   Link to this post
Man, I've got to learn boost::lambda...I read through some of the information at boost.org, but I still haven't actually used it in my own code for anything... Is that the same one that has the bind stuff?
jsmith (3807)   Link to this post
boost::lambda has its own version of bind, but there is also the boost::bind library.

It's cool, but has a steep learning curve.


This topic is archived - New replies not allowed.