Share via


map::insert (STL/CLR)

Adds elements.

    cliext::pair<iterator, bool> insert(value_type val);
    iterator insert(iterator where, value_type val);
    template<typename InIter>
        void insert(InIter first, InIter last);
    void insert(System::Collections::Generic::IEnumerable<value_type>^ right);

Parameters

  • first
    Beginning of range to insert.

  • last
    End of range to insert.

  • right
    Enumeration to insert.

  • val
    Key value to insert.

  • where
    Where in container to insert (hint only).

Remarks

Each of the member functions inserts a sequence specified by the remaining operands.

The first member function endeavors to insert an element with value val, and returns a pair of values X. If X.second is true, X.first designates the newly inserted element; otherwise X.first designates an element with equivalent ordering that already exists and no new element is inserted. You use it to insert a single element.

The second member function inserts an element with value val, using where as a hint (to improve performance), and returns an iterator that designates the newly inserted element. You use it to insert a single element which might be adjacent to an element you know.

The third member function inserts the sequence [first, last). You use it to insert zero or more elements copied from another sequence.

The fourth member function inserts the sequence designated by the right. You use it to insert a sequence described by an enumerator.

Each element insertion takes time proportional to the logarithm of the number of elements in the controlled sequence. Insertion can occur in amortized constant time, however, given a hint that designates an element adjacent to the insertion point.

Example

// cliext_map_insert.cpp 
// compile with: /clr 
#include <cliext/map> 
 
typedef cliext::map<wchar_t, int> Mymap; 
typedef Mymap::pair_iter_bool Pairib; 
int main() 
    { 
    Mymap c1; 
    c1.insert(Mymap::make_value(L'a', 1)); 
    c1.insert(Mymap::make_value(L'b', 2)); 
    c1.insert(Mymap::make_value(L'c', 3)); 
 
// display contents " [a 1] [b 2] [c 3]" 
    for each (Mymap::value_type elem in c1) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
 
// insert a single value, unique and duplicate 
// insert a single value, success and failure 
    Pairib pair1 = c1.insert(Mymap::make_value(L'x', 24)); 
    System::Console::WriteLine("insert([L'x' 24]) = [[{0} {1}] {2}]", 
        pair1.first->first, pair1.first->second, pair1.second); 
 
    pair1 = c1.insert(Mymap::make_value(L'b', 2)); 
    System::Console::WriteLine("insert([L'b' 2]) = [[{0} {1}] {2}]", 
        pair1.first->first, pair1.first->second, pair1.second); 
 
    for each (Mymap::value_type elem in c1) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
 
// insert a single value with hint 
    Mymap::iterator it = 
        c1.insert(c1.begin(), Mymap::make_value(L'y', 25)); 
    System::Console::WriteLine("insert(begin(), [L'y' 25]) = [{0} {1}]", 
        it->first, it->second); 
    for each (Mymap::value_type elem in c1) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
 
// insert an iterator range 
    Mymap c2; 
    it = c1.end(); 
    c2.insert(c1.begin(), --it); 
    for each (Mymap::value_type elem in c2) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
 
// insert an enumeration 
    Mymap c3; 
    c3.insert(   // NOTE: cast is not needed 
        (System::Collections::Generic:: 
            IEnumerable<Mymap::value_type>^)%c1); 
    for each (Mymap::value_type elem in c3) 
        System::Console::Write(" [{0} {1}]", elem->first, elem->second); 
    System::Console::WriteLine(); 
    return (0); 
    } 
 
 [a 1] [b 2] [c 3]
insert([L'x' 24]) = [[x 24] True]
insert([L'b' 2]) = [[b 2] False]
 [a 1] [b 2] [c 3] [x 24]
insert(begin(), [L'y' 25]) = [y 25]
 [a 1] [b 2] [c 3] [x 24] [y 25]
 [a 1] [b 2] [c 3] [x 24]
 [a 1] [b 2] [c 3] [x 24] [y 25]

Requirements

Header: <cliext/map>

Namespace: cliext

See Also

Reference

map (STL/CLR)