C++ Tutorial/set multiset/multiset insert

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

add array a to 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"
   ims::const_iterator result;
   intMultiset.insert( a, a + SIZE ); // add array a to multiset
   return 0;
}

Insert 4 again to an integer set and process return value

/* 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 <set>
#include <algorithm>
#include <iterator>
using namespace std;
int main()
{
    /* type of the collection:
     * - duplicates allowed
     * - elements are integral values
     * - descending order
     */
    typedef multiset<int,greater<int> > IntSet;
    IntSet coll1;        // empty multiset container
    // insert elements in random order
    coll1.insert(4);
    coll1.insert(3);
    coll1.insert(5);
    coll1.insert(1);
    coll1.insert(6);
    coll1.insert(2);
    coll1.insert(5);
    // iterate over all elements and print them
    IntSet::iterator pos;
    for (pos = coll1.begin(); pos != coll1.end(); ++pos) {
        cout << *pos << " ";
    }
    cout << endl;
    // insert 4 again and process return value
    IntSet::iterator ipos = coll1.insert(4);
    cout << "4 inserted as element "
         << distance(coll1.begin(),ipos) + 1
         << endl;
}
6 5 5 4 3 2 1
4 inserted as element 5

Insert elements in array to a multiset

#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, " " );
   // 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 );

   cout << endl;
   return 0;
}
After insert, intMultiset contains:
1 7 9 13 18 22 22 30 85 100

Insert element to a multiset

#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