blob: 54d65a7d3a5927fe9019789022397fcfb5879662 [file] [log] [blame]
/**************************************************************************
*
* binary_search.cpp - Example program of binary search.
*
* $Id$
*
***************************************************************************
*
* Copyright 2005-2006 The Apache Software Foundation or its licensors,
* as applicable.
*
* Copyright 1994-2006 Rogue Wave Software.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**************************************************************************/
#include <algorithm> // for binary_search(), copy(), sort()
#include <deque> // for deque
#include <functional> // for less
#include <iostream> // for cout
#include <iterator> // for ostream_iterator, char_traits
#include <examples.h>
int main ()
{
// Typedefs for convenience.
typedef short Value;
typedef std::deque<Value, std::allocator<Value> > Deque;
typedef std::char_traits<char> Traits;
typedef std::ostream_iterator<Value, char, Traits> Iterator;
const Value arr[] = { 0, 1, 2, 2, 3, 4, 2, 2, 6, 7 };
// Populate and sort the container.
Deque d (arr + 0, arr + sizeof arr / sizeof *arr);
std::sort (d.begin (), d.end ());
// Arbitrary values to search for.
const Value val_1 = 3;
const Value val_2 = 11;
// Try binary_search variants.
const bool found_1 = std::binary_search (d.begin (), d.end (), val_1);
const bool found_2 = std::binary_search (d.begin (), d.end (), val_2,
std::less<Value>());
// Output the sorted sequence.
std::cout << "Container contents: ";
std::copy (d.begin (), d.end (), Iterator (std::cout, " "));
// Output the results of the algorithms.
std::cout << "\nThe number " << val_1 << " was "
<< (found_1 ? "" : "NOT ") << "found";
std::cout << "\nThe number " << val_2 << " was "
<< (found_2 ? "" : "NOT ") << "found\n";
return 0;
}