|
Boost Users : |
From: Caleb Epstein (caleb.epstein_at_[hidden])
Date: 2004-11-18 17:51:55
On Thu, 18 Nov 2004 15:21:06 -0700, Chris Goller
<cgoller_at_[hidden]> wrote:
> David-
>
> Thanks.
>
> Upon reading my poorly phrased email I realized this:
>
> I want unique keys so that if the data looks like this:
>
> key value
> 3 1
> 1 2
> 1 4
> 4 3
> 5 5
> 5 9
>
> So, what I would like to do is to a print each value for a particular
> key in such a way:
>
> key: value:
> 1----
> |------2
> |------4
> 3----
> |------1
> 4----
> |------3
> 5----
> |------5
> |------9
>
> Or, for example, if I had a list of directories as keys and and files
> are their values then for each directory I could print out the file.
> Right now, I do this:
>
> for (multimap<string, string>::iterator dir = filesystem.begin(); i !=
> filesystem.end())
> {
> pair<multimap<string, string>::iterator, multimap<string,
> string>::iterator> bounds;
> bounds = filesystem.equal_range(dir->first);
> cout << dir->first << "----" << endl;
> for (multimap<string, string>::iterator file = bounds.first; file
> != bounds.second; file++)
> {
> cout << " |------" << file->second << endl;
> }
> // Moves the directory iterator to the next directory key.
> dir = file;
> }
>
> So, I'm trying to get better at using iterators much smarter. I would
> like to make this code much more concise.
>
> I figured that if I could jump from key to key I write the above code
> much cleaner.
You can use multimap::upper_bound instead of iter++ to help navigate
the unique key space:
using namespace std;
using namespace boost::assign;
typedef multimap<int, int> mymap;
mymap m = map_list_of (1,1)(1,2)(1,3)(2,1)(2,2)(2,3)(3,1)(3,2)(4,9)(5,10);
for (mymap::const_iterator i = m.begin (); i != m.end (); i =
m.upper_bound (i->first))
cout << i->first << endl;
outputs
1
2
3
4
5
-- Caleb Epstein caleb dot epstein at gmail dot com
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net