Aracılığıyla paylaş


map::insert

Bir öğe veya öğeleri aralığını eşlemenin içine ekler.

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

Parametreler

Parametre

Description

_Val

Harita, öğe veya daha genel tanımıyla, anahtarı equivalently sipariş öğe içermiyorsa eşlemenin içine eklenecek öğenin değerini.

_Where

Doğru ekleme noktası için aramaya başlamak için yer ile ilgili bir ipucu.

_First

Bir Eşlem'den kopyalanacak ilk öğenin konumu.

_Last

Bir Eşlem'den kopyalanacak son öğenin altından başladığından ötesinde pozisyon.

Dönüş Değeri

İlk Ekle üye işlev çifti bool bileşeni ekleme yaptıysanız true değerini döndürür ve öğe sırası içindeki eşdeğer bir değeri, anahtarı vardı ve yeni bir öğe eklenmiş olan yerlerde veya öğe zaten bulunduğu yerde, Yineleyici bileşeninin döndürdüğü adres eşlemesi zaten içeriyorsa false döndürür.

Yineleyici bileşen çiftinin erişmek için pr kullanmak bu üye işlevi tarafından döndürülen, pr. ilkve bu başvuru için * (pr.ilk).Access bool bileşen çiftinin pr kullanmak bu üye işlevi tarafından döndürülen, pr.second.

İkinci Ekle üye işlevi, ipucu sürüm burada yeni öðe eklenir eşlemeye konumu işaret eden bir yineleyici döndürür.

Son iki üye işlevleri dışında ilk ikisi aynı davranır _Val eklenen değer oluşturmak için kullanılabilir.

Notlar

Value_type öğesi bir çifti, ve böylece öğe değerinin sıralı bir çifti ilk bileşen anahtar değerine eşit ve ikinci bileşen öğesinin veri değerine eşit olacaktır.

Ekleme meydana gelebilir Logaritmik saat yerine INSERT ipucu sürümü için amortized sabit zaman hemen ekleme noktasını izleyen, _Where.

Üçüncü üye işlev öğe değerlerinin dizisini Yineleyici aralıktaki tarafından gönderilen her öğeye karşılık gelen eşleme ekler [_First, _Last) belirtilen kümesi.

Örnek

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

Çıktı

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

Gereksinimler

Başlık: <map>

Namespace: std

Ayrıca bkz.

Başvuru

map Class

map::insert, map::find, ve map::end

Standart Şablon Kütüphanesi