insert [C++ Reference]
Keywords: stl,multiset,insert
Date: 2009-09-07T23:12:21-0700

[[insert]]
C++ Reference
Translations of this page?:

- en
- br-pt
- cn
- fr
- it
- jp
- pl
- ru
- tr

## insert

Syntax:

        #include <set>
        iterator insert( iterator pos, const TYPE& val );
        iterator insert( const TYPE& val );
        void insert( input_iterator start, input_iterator end );

The function insert() either:

- inserts val after the element at pos (where pos is really just a suggestion as to where val should go, since multisets and multimaps are ordered), and returns an iterator to that element.
- inserts val into the multiset, returning an iterator to the element inserted.
- inserts a range of elements from start to end.

For example, the following code uses insert() to add elements to a multiset:

        multiset<int> ms;
        multiset<int>::iterator iter;
        int i;

        for (i = 1; i < 5; i++) {
            ms.insert(i);
            ms.insert(i*i);
            ms.insert(i-1);
        }

        cout << "ms is:" ;
        for (iter = ms.begin(); iter != ms.end(); iter++)
            cout << " " << *iter;
        cout << "." << endl;

The above code produces the following output:

    ms is: 0 1 1 1 2 2 3 3 4 4 9 16.

Edit this page • Old revisions
• • • Login • Index • Recent changes • RSS • cc •