C++ Tutorial/set multiset/multiset bound

Материал из C\C++ эксперт
Перейти к: навигация, поиск

Determine lower and upper bound of a value in intMultiset

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <set> // multiset class-template definition
  2. include <algorithm> // copy algorithm
  3. include <iterator> // ostream_iterator

int main() {

  int a[ 10 ] = { 7, 22, 9, 1, 18, 30, 100, 22, 85, 13 };
  std::multiset< int, std::less< int > > intMultiset;
  std::ostream_iterator< int > output( cout, " " );
  // insert elements of array a into intMultiset
  intMultiset.insert( a, a + 10 );
  cout << "\nAfter insert, intMultiset contains:\n";
  std::copy( intMultiset.begin(), intMultiset.end(), output );
  // determine lower and upper bound of 22 in intMultiset
  cout << "\nUpper bound of 22: " << *( intMultiset.upper_bound( 22 ) );
  cout << endl;
  return 0;

}</source>

After insert, intMultiset contains:
1 7 9 13 18 22 22 30 85 100
Upper bound of 22: 30

Determine lower bound of a value in intMultiset

<source lang="cpp">#include <iostream> using std::cout; using std::endl;

  1. include <set> // multiset class-template definition
  2. include <algorithm> // copy algorithm
  3. include <iterator> // ostream_iterator

int main() {

  int a[ 10 ] = { 7, 22, 9, 1, 18, 30, 100, 22, 85, 13 };
  std::multiset< int, std::less< int > > intMultiset;
  std::ostream_iterator< int > output( cout, " " );
  // insert elements of array a into intMultiset
  intMultiset.insert( a, a + 10 );
  cout << "\nAfter insert, intMultiset contains:\n";
  std::copy( intMultiset.begin(), intMultiset.end(), output );
  // determine lower bound of 22 in intMultiset
  cout << "\n\nLower bound of 22: "
     << *( intMultiset.lower_bound( 22 ) );
  cout << endl;
  return 0;

}</source>

After insert, intMultiset contains:
1 7 9 13 18 22 22 30 85 100
Lower bound of 22: 22