map::insert (STL/CLR)

將元素加入。

    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);

參數

  • 第一個
    若要插入範圍的開頭。

  • last
    若要插入範圍的結尾。

  • right
    若要插入的列舉型別。

  • val
    若要插入的機碼值。

  • where
    若要插入 (只有提示) 的容器中的位置。

備註

每個成員函式插入剩下的運算元所指定的序列。

第一個成員函式插入具有值的項目力圖造成val,並傳回一組數值X。如果X.second為 true, X.first將指定的新插入的項目。 否則X.first會指定與對等項目順序已經存在,而且會插入新的項目。您可以用它來插入單一項目。

第二個成員函式插入具有值的項目val、 使用where為提示,(以提升效能),並傳回 iterator,指派新插入的項目。您可以用它來插入單一項目可能會與之相鄰的項目,您知道。

第三個成員函式插入序列[first, last)。您可以用它來插入複製另一個序列中的零或多個項目。

第四個成員函式插入所指定的順序right。您可以用它來插入所描述的列舉值的序列。

每個項目插入要花在受控制序列中項目的數字的對數成比例的時間。插入可出現在 amortized 固定的時間,不過,提供一個提示,將指定的插入點到相鄰的項目。

範例

// 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); 
    } 
 
  

需求

標頭: < cliext/地圖 >

Namespace: cliext

請參閱

參考

map (STL/CLR)