共用方式為


map::insert

插入元素或某個範圍的對應。

pair <iterator, bool> insert(
   const value_type& _Val
);
iterator insert(
   const_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
);

參數

參數

描述

_Val

要插入之項目的值。對應,除非這個對應,一般而言,已包含該項目或索引鍵相當於已排序的項目。

_Where

如需這個位置開始的提示下搜尋修正問題的外掛程式。

_First

從對應會複製的第一個項目的位置。

_Last

位置是從對應會複製的最後一個項目之外。

傳回值

第一 insert 成員函式來傳回 bool 元件傳回 true 的配對,如果插入點會進行錯誤的,如果對應已經包含索引鍵具有等值的順序,,和 Iterator 元件傳回電子郵件地址的插入新項目或的項目已經在位置項目。

若要存取一組 pr 的 Iterator 元件 (此成員函式所傳回的,請使用 pr。first和取值 (Dereference),使用* (pr。first)。 若要存取一組 pr 的 bool 元件 (此成員函式所傳回的,請使用 pr。second

第二 insert 成員函式,提示版本,傳回 Iterator 為新的項目插入至對應位置的點。

最後兩個成員函式一般作業的前兩個相同,但是有一點例外,就是 _Val 用來建構要插入的值。

備註

項目的 value_type 為,因此,項目的值將會與第一個元件相等於這個機碼值和秒數的排序配對等於項目之資料值。

如果插入點後面緊接著 _Where,插入至插入的提示版本會讓舊的常數時間可能發生,而非對數時間。

第三 + 成成員函式插入項目值序列對應至 Iterator 所定址的每個項目對應到範圍 [_First, _Last) 中指定的設定。

範例

// map_insert.cpp
// compile with: /EHsc
#include <map>
#include <iostream>
#include <string>

int main( ) {
   using namespace std;
   map <int, int>::iterator m1_pIter, m2_pIter;

   map <int, int> m1, m2;
   typedef pair <int, int> Int_Pair;

   m1.insert ( Int_Pair ( 1, 10 ) );
   m1.insert ( Int_Pair ( 2, 20 ) );
   m1.insert ( Int_Pair ( 3, 30 ) );
   m1.insert ( Int_Pair ( 4, 40 ) );

   cout << "The original key values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> first;
   cout << "." << endl;

   cout << "The original mapped values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> second;
   cout << "." << endl;

   pair< map<int,int>::iterator, bool > pr;
   pr = m1.insert ( Int_Pair ( 1, 10 ) );

   if( pr.second == true ) {
      cout << "The element 10 was inserted in m1 successfully." << endl;
   }
   else {
      cout << "Key number 1 already exists in m1\n"
           << "with an associated value of ( (pr.first) -> second ) = " 
           << ( pr.first ) -> second 
           << "." << endl;
   }

   // The hint version of insert
   m1.insert( --m1.end( ), Int_Pair ( 5, 50 ) );

   cout << "After the insertions, the key values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> first;
   cout << "," << endl;

   cout << "and the mapped values of m1 =";
   for ( m1_pIter = m1.begin( ); m1_pIter != m1.end( ); m1_pIter++ )
      cout << " " << m1_pIter -> second;
   cout << "." << endl;

   m2.insert ( Int_Pair ( 10, 100 ) );

   // The templatized version inserting a range
   m2.insert( ++m1.begin( ), --m1.end( ) );

   cout << "After the insertions, the key values of m2 =";
   for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
      cout << " " << m2_pIter -> first;
   cout << "," << endl;

   cout << "and the mapped values of m2 =";
   for ( m2_pIter = m2.begin( ); m2_pIter != m2.end( ); m2_pIter++ )
      cout << " " << m2_pIter -> second;
   cout << "." << endl;

    // The templatized versions move constructing elements
    map<int, string> m3, m4;
    pair<int, string> is1(1, "a"), is2(2, "b");

    m3.insert(move(is1));
    cout << "After the move insertion, m3 contains:" << endl
      << " " << m3.begin()->first
      << " => " << m3.begin()->second
      << endl;

    m4.insert(c4.begin(),move(is2));
    cout << "After the move insertion, m4 contains:" << endl
      << " " << m4.begin()->first
      << " => " << m4.begin()->second
      << endl;
}

Output

The original key values of m1 = 1 2 3 4.
The original mapped values of m1 = 10 20 30 40.
Key number 1 already exists in m1
with an associated value of ( (pr.first) -> second ) = 10.
After the insertions, the key values of m1 = 1 2 3 4 5,
and the mapped values of m1 = 10 20 30 40 50.
After the insertions, the key values of m2 = 2 3 4 10,
and the mapped values of m2 = 20 30 40 100.
After the move insertion, m3 contains:
 1 => a
After the move insertion, m4 contains:
 2 => b

需求

標題: <map>

命名空間: std

請參閱

參考

map Class

map::insert, map::find, 和 map::end

標準樣板程式庫