/************************************************************************** * * binary_search.cpp - Example program of binary search. * * $Id: binary_search.cpp 590052 2007-10-30 12:44:14Z faridz $ * *************************************************************************** * * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed * with this work for additional information regarding copyright * ownership. The ASF licenses this file to you 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. * * Copyright 1994-2006 Rogue Wave Software. * **************************************************************************/ #include // for binary_search(), copy(), sort() #include // for deque #include // for less #include // for cout #include // for ostream_iterator, char_traits #include int main () { // Typedefs for convenience. typedef short Value; typedef std::deque > Deque; typedef std::char_traits Traits; typedef std::ostream_iterator 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()); // 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; }