C++/STL Algorithms Binary search/equal range
Содержание
equal_range and distance
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
using namespace std;
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << " ";
}
std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection
* - NOTE: NO half-open range
*/
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
for (int i=first; i<=last; ++i) {
coll.insert(coll.end(),i);
}
}
int main()
{
list<int> coll;
INSERT_ELEMENTS(coll,1,9);
INSERT_ELEMENTS(coll,1,9);
coll.sort ();
PRINT_ELEMENTS(coll);
// print first and last position 5 could get inserted
pair<list<int>::iterator,list<int>::iterator> range;
range = equal_range (coll.begin(), coll.end(),
5);
cout << "5 could get position "
<< distance(coll.begin(),range.first) + 1
<< " up to "
<< distance(coll.begin(),range.second) + 1
<< " without breaking the sorting" << endl;
}
/*
1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9
5 could get position 9 up to 11 without breaking the sorting
*/
Get equal range with equal_range
#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
template <class T>
void print(T& c){
for( typename T::iterator i = c.begin(); i != c.end(); i++ ){
std::cout << *i << endl;
}
}
int main( ){
const int len = 15;
const int a[len] = { 9, 2, 3, 3, 7, 5, 7, 7, 4, 10, 5, 6, 7, 4, 7 };
vector<int> v( a, a + len );
vector<int>::iterator v_end = v.end();
sort( v.begin(), v.end() );
vector<int>::iterator start = v.begin();
int mode_range = 0;
int mode_grade = 0;
pair<vector<int>::iterator,vector<int>::iterator> range;
// look for the largest range, which is the mode
while( start != v_end )
{
range = equal_range( start, v_end, *start );
if( range.second - range.first > mode_range ){
mode_range = range.second - range.first;
mode_grade = *start;
}
start = range.second;
}
cout << "Mode by method 2: " << mode_grade
<< "\n\nMinimum: " << v[0]
<< " Maximum: " << v[len-1]
<< " Median: " << v[len/2] << endl;
}
Use equal function to check for corresponding even and odd elements
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << " ";
}
std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection
* - NOTE: NO half-open range
*/
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
for (int i=first; i<=last; ++i) {
coll.insert(coll.end(),i);
}
}
using namespace std;
bool bothEvenOrOdd (int elem1, int elem2)
{
return elem1 2;
}
int main()
{
vector<int> coll1;
list<int> coll2;
INSERT_ELEMENTS(coll1,1,7);
INSERT_ELEMENTS(coll2,3,9);
PRINT_ELEMENTS(coll1,"coll1: ");
PRINT_ELEMENTS(coll2,"coll2: ");
// check for corresponding even and odd elements
if (equal (coll1.begin(), coll1.end(), // first range
coll2.begin(), // second range
bothEvenOrOdd)) { // comparison criterion
cout << "even and odd elements correspond" << endl;
}
else {
cout << "even and odd elements do not correspond" << endl;
}
}
/*
coll1: 1 2 3 4 5 6 7
coll2: 3 4 5 6 7 8 9
even and odd elements correspond
*/
Use equal function to check whether both collections are equal
/* The following code example is taken from the book
* "The C++ Standard Library - A Tutorial and Reference"
* by Nicolai M. Josuttis, Addison-Wesley, 1999
*
* (C) Copyright Nicolai M. Josuttis 1999.
* Permission to copy, use, modify, sell and distribute this software
* is granted provided this copyright notice appears in all copies.
* This software is provided "as is" without express or implied
* warranty, and with no claim as to its suitability for any purpose.
*/
#include <iostream>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>
#include <numeric>
/* PRINT_ELEMENTS()
* - prints optional C-string optcstr followed by
* - all elements of the collection coll
* - separated by spaces
*/
template <class T>
inline void PRINT_ELEMENTS (const T& coll, const char* optcstr="")
{
typename T::const_iterator pos;
std::cout << optcstr;
for (pos=coll.begin(); pos!=coll.end(); ++pos) {
std::cout << *pos << " ";
}
std::cout << std::endl;
}
/* INSERT_ELEMENTS (collection, first, last)
* - fill values from first to last into the collection
* - NOTE: NO half-open range
*/
template <class T>
inline void INSERT_ELEMENTS (T& coll, int first, int last)
{
for (int i=first; i<=last; ++i) {
coll.insert(coll.end(),i);
}
}
using namespace std;
int main()
{
vector<int> coll1;
list<int> coll2;
INSERT_ELEMENTS(coll1,1,7);
INSERT_ELEMENTS(coll2,3,9);
PRINT_ELEMENTS(coll1,"coll1: ");
PRINT_ELEMENTS(coll2,"coll2: ");
// check whether both collections are equal
if (equal (coll1.begin(), coll1.end(), // first range
coll2.begin())) { // second range
cout << "coll1 == coll2" << endl;
}
else {
cout << "coll1 != coll2" << endl;
}
}
/*
coll1: 1 2 3 4 5 6 7
coll2: 3 4 5 6 7 8 9
coll1 != coll2
*/
Use equal_range to determine both the lower- and upper-bound insertion points
#include <iostream>
using std::cout;
using std::endl;
#include <algorithm>
#include <vector>
#include <iterator>
int main()
{
int a1[ 10 ] = { 2, 2, 4, 4, 4, 6, 6, 6, 6, 8 };
std::vector< int > v( a1, a1 + 10 );
std::ostream_iterator< int > output( cout, " " );
std::copy( v.begin(), v.end(), output );
std::pair< std::vector< int >::iterator, std::vector< int >::iterator > eq;
eq = std::equal_range( v.begin(), v.end(), 6 );
cout << "\n\n\nLower bound of 6 is element " << ( eq.first - v.begin() ) << " of vector v \n\n\n";
cout << "Upper bound of 6 is element " << ( eq.second - v.begin() ) << " of vector v";
return 0;
}
/*
2 2 4 4 4 6 6 6 6 8
Lower bound of 6 is element 5 of vector v
Upper bound of 6 is element 9 of vector v
*/