Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
binary_search.cpp
Go to the documentation of this file.
1
29#include <algorithm> // for binary_search(), copy(), sort()
30#include <deque> // for deque
31#include <functional> // for less
32#include <iostream> // for cout
33#include <iterator> // for ostream_iterator, char_traits
34
35#include <examples.h>
36
37
38int main ()
39{
40 // Typedefs for convenience.
41 typedef short Value;
42 typedef std::deque<Value, std::allocator<Value> > Deque;
43 typedef std::char_traits<char> Traits;
44 typedef std::ostream_iterator<Value, char, Traits> Iterator;
45
46 const Value arr[] = { 0, 1, 2, 2, 3, 4, 2, 2, 6, 7 };
47
48 // Populate and sort the container.
49 Deque d (arr + 0, arr + sizeof arr / sizeof *arr);
50 std::sort (d.begin (), d.end ());
51
52 // Arbitrary values to search for.
53 const Value val_1 = 3;
54 const Value val_2 = 11;
55
56 // Try binary_search variants.
57 const bool found_1 = std::binary_search (d.begin (), d.end (), val_1);
58 const bool found_2 = std::binary_search (d.begin (), d.end (), val_2,
59 std::less<Value>());
60
61 // Output the sorted sequence.
62 std::cout << "Container contents: ";
63 std::copy (d.begin (), d.end (), Iterator (std::cout, " "));
64
65 // Output the results of the algorithms.
66 std::cout << "\nThe number " << val_1 << " was "
67 << (found_1 ? "" : "NOT ") << "found";
68
69 std::cout << "\nThe number " << val_2 << " was "
70 << (found_2 ? "" : "NOT ") << "found\n";
71
72 return 0;
73}
InputIter< UserClass > Iterator
#define d
Definition 17.names.cpp:115
#define Traits
Definition 17.names.cpp:556
int main()
std::deque< std::string, std::allocator< std::string > > Deque
Definition deque.cpp:35