cplusplus.com
C++ : Forum : UNIX/Linux Programming : Set iterator difference
 
cplusplus.com
Information
Documentation
Reference
Articles
Forum
Forum
Beginners
Windows Programming
UNIX/Linux Programming
General C++ Programming
Lounge
Jobs


post Set iterator difference

kunigami (14)
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 (6258)
Try set::size or set::count
http://www.cplusplus.com/reference/stl/set/
seymore15074 (449)
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 (5804)
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)
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 (5804)
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.

Topic archived. No new replies allowed.