Partager via


set::insert

Inserts an element or a range of elements into a set.

pair<iterator, bool> insert(
   const value_type& _Val
);
iterator insert(
   iterator _Where,
   const value_type& _Val
);
template<class InputIterator>
   void insert(
      InputIterator _First,
      InputIterator _Last
);
template<class ValTy>
    pair<iterator, bool> insert(
        ValTy&& _Val
);
template<class ValTy>
    iterator insert(
        const_iterator _Where,
        ValTy&& _Val
);

Parameters

Parameter

Description

_Val

The value of an element to be inserted into the set unless the set already contains that element or, more generally, an element whose key is equivalently ordered.

_Where

The place to start searching for the correct point of insertion. (Insertion can occur in amortized constant time, instead of logarithmic time, if the insertion point immediately follows _Where.)

_First

The position of the first element to be copied from a set.

_Last

The position just beyond the last element to be copied from a set.

Return Value

The first insert member function returns a pair whose bool component returns true if an insertion was made and false if the set already contained an element whose key had an equivalent value in the ordering, and whose iterator component returns the address where a new element was inserted or where the element was already located.

The second insert member function returns an iterator that points to the position where the new element was inserted into the set.

The last two member functions behave the same as the first two, except that val is used to construct the inserted value.

Remarks

The third member function inserts the sequence of element values into a set corresponding to each element addressed by an iterator of in the range [_First, _Last) of a specified set.

Example

// set_insert.cpp
// compile with: /EHsc
#include <set>
#include <iostream>
#include <string>

int main( )
{
   using namespace std;
   set <int>::iterator s1_pIter, s2_pIter;

   set <int, less<int> > s1, s2;
   s1.insert( 10 );
   s1.insert( 20 );
   s1.insert( 30 );
   s1.insert( 40 );

   cout << "The original s1 =";
   for ( s1_pIter = s1.begin( ); s1_pIter != s1.end( ); s1_pIter++ )
      cout << " " << *s1_pIter;
   cout << "." << endl;

   pair< set<int>::iterator, bool > pr;
   pr = s1.insert( 10 );

   if(pr.second == true)   
   {
      cout << "The element 10 was inserted in s1 successfully."
           << endl;
   }
   else   
   {
      cout << "The element 10 already exists in s1 and"
           << " *( pr.first ) = " << *( pr.first ) << "." << endl;
   }

   s1.insert( --s1.end( ), 50 );

   cout << "After the insertions, s1 =";
   for ( s1_pIter = s1.begin( ); s1_pIter != s1.end( ); s1_pIter++ )
      cout << " " << *s1_pIter;
   cout << "." << endl;

   s2.insert( 100 );
   s2.insert( ++s1.begin( ), --s1.end( ) );

   cout << "s2 =";
   for ( s2_pIter = s2.begin( ); s2_pIter != s2.end( ); s2_pIter++ )
      cout << " " << *s2_pIter;
   cout << "." << endl;

   // Construct by moving
   set<string> s3, s4;
   string str1("a"), str2("b");

   s3.insert(move(str1));
   cout << "After the move insertion, s3 contains: "
      << *s3.begin() << endl;

   s4.insert(s4.begin(), move(str2));
   cout << "After the move insertion, s4 contains: "
      << *s4.begin() << endl;
 }
The original s1 = 10 20 30 40.
The element 10 already exists in s1 and *( pr.first ) = 10.
After the insertions, s1 = 10 20 30 40 50.
s2 = 20 30 40 100.
After the move insertion, s3 contains: a
After the move insertion, s4 contains: b

Requirements

Header: <set>

Namespace: std

See Also

Reference

set Class

Standard Template Library

Other Resources

set Members