Debian Codegraph @

yo/containers/examples/mapbounds.cc

raw
1 #include <iostream>
2 #include <map>
3 using namespace std;
4
5 int main()
6 {
7 pair<string, int> pa[] =
8 {
9 pair<string,int>("one", 10),
10 pair<string,int>("two", 20),
11 pair<string,int>("three", 30),
12 };
13 map<string, int> object(&pa[0], &pa[3]);
14 map<string, int>::iterator it;
15
16 if ((it = object.lower_bound("tw")) != object.end())
17 cout << "lower-bound `tw' is available, it is: " <<
18 it->first << '\n';
19
20 if (object.lower_bound("twoo") == object.end())
21 cout << "lower-bound `twoo' not available" << '\n';
22
23 cout << "lower-bound two: " <<
24 object.lower_bound("two")->first <<
25 " is available\n";
26
27 if ((it = object.upper_bound("tw")) != object.end())
28 cout << "upper-bound `tw' is available, it is: " <<
29 it->first << '\n';
30
31 if (object.upper_bound("twoo") == object.end())
32 cout << "upper-bound `twoo' not available" << '\n';
33
34 if (object.upper_bound("two") == object.end())
35 cout << "upper-bound `two' not available" << '\n';
36
37 pair
38 <
39 map<string, int>::iterator,
40 map<string, int>::iterator
41 >
42 p = object.equal_range("two");
43
44 cout << "equal range: `first' points to " <<
45 p.first->first << ", `second' is " <<
46 (
47 p.second == object.end() ?
48 "not available"
49 :
50 p.second->first
51 ) <<
52 '\n';
53 }
54 /*
55 Generated output:
56
57 lower-bound `tw' is available, it is: two
58 lower-bound `twoo' not available
59 lower-bound two: two is available
60 upper-bound `tw' is available, it is: two
61 upper-bound `twoo' not available
62 upper-bound `two' not available
63 equal range: `first' points to two, `second' is not available
64 */