C++ Tutorial/set multiset/multiset count — различия между версиями

Материал из C\C++ эксперт
Перейти к: навигация, поиск
м (1 версия: Импорт контента...)
 
м (1 версия: Импорт контента...)
 
(нет различий)

Текущая версия на 10:29, 25 мая 2010

Count elements in multiset by value

#include <iostream>
using std::cout;
using std::endl;
#include <set> // multiset class-template definition
#include <algorithm> // copy algorithm
#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, " " );
   cout << "There are currently " << intMultiset.count( 15 )
      << " values of 15 in the multiset\n";
   intMultiset.insert( 15 );
   intMultiset.insert( 15 );
   cout << "After inserts, there are " << intMultiset.count( 15 )
      << " values of 15 in the multiset\n\n";

   cout << endl;
   return 0;
}
There are currently 0 values of 15 in the multiset
After inserts, there are 2 values of 15 in the multiset

Count the value in multiset

#include <iostream>
#include <set>
#include <algorithm>
using namespace std;
int main()
{
   const int SIZE = 10;
   int a[ SIZE ] = { 7, 22, 9, 1, 18, 30, 100, 22, 85, 13 };
   typedef multiset< int, less< int > > ims;
   ims intMultiset;    // ims for "integer multiset"
   cout << "There are currently " << intMultiset.count( 15 )
        << " values of 15 in the multiset\n";   
   return 0;
}

multiset.count

#include <iostream>
using std::cout;
using std::endl;
#include <set> // multiset class-template definition
#include <algorithm> // copy algorithm
#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, " " );
   cout << "There are currently " << intMultiset.count( 15 )
      << " values of 15 in the multiset\n";
   intMultiset.insert( 15 );
   intMultiset.insert( 15 );
   cout << "After inserts, there are " << intMultiset.count( 15 )
      << " values of 15 in the multiset\n\n";

   cout << endl;
   return 0;
}
There are currently 0 values of 15 in the multiset
After inserts, there are 2 values of 15 in the multiset