Udostępnij za pośrednictwem


hash_map::insert

[!UWAGA]

Ten interfejs API jest nieaktualny.Alternatywą jest unordered_map — Klasa.

Wstawia element lub szereg elementów do hash_map.

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

Parametry

Parametr

Opis

_Val

Wartość elementu ma zostać wstawiona do hash_map, chyba że hash_map już zawiera ten element (lub, bardziej ogólnie, element równoważnie porządkowania, której klucz).

_Where

Wskazówka dotycząca miejsca rozpoczęcia wyszukiwania dla poprawnego punktu wstawiania.

_First

Pozycja pierwszego elementu do skopiowania z hash_map.

_Last

Pozycja tylko poza ostatni element do skopiowania z hash_map.

Wartość zwracana

Pierwszy Wstaw funkcji składowej zwraca parę którego składnik bool zwraca wartość true, jeśli dokonano wstawiania i wartość false, jeśli element której klucz miał równoważnej wartości w kolejności, a których komponent sterująca zwraca adres został wstawiony nowy element lub element znajdował się już zawartych już hash_map.

Aby uzyskać dostęp do składnika sterująca pary pr zwracana przez tę funkcję Państwa, użyj pr. pierwszyi aby usunąć odwołania do niego, należy użyć * (pr.pierwszy).Do dostępu do bool części pary pr zwracana przez tę funkcję Państwa, użyj pr. drugii aby usunąć odwołania do niego, należy użyć * (pr.drugi).

Drugi Wstaw funkcji składowej, wersja wskazówka zwraca iterację, który wskazuje miejsce, gdzie nowy element został wstawiony do hash_map.

Ostatnie dwa Wstaw funkcji elementów członkowskich działają tak samo, jak dwa pierwsze, chyba, że są one przenoszone skonstruować wstawiona wartość.

Uwagi

value_type elementu jest parą, dzięki czemu wartość elementu będzie uporządkowaną parą z pierwszym składnikiem równym wartości klucza i drugim składnikiem równym wartości danych elementu.

Dla wersji wstawienia z podpowiedzią, wstawianie może wystąpić w stałym, amortyzowanym czasie zamiast czasu logarytmicznego, jeśli ten punkt bezpośrednio poprzedza _Where.

Trzeci funkcji składowej wstawia sekwencji wartości elementu do hash_map, odpowiadające każdemu elementowi kierowane przez iterację z zakresu [First, Last) z określonego zestawu.

Przykład

// hash_map_insert.cpp
// compile with: /EHsc
#include<hash_map>
#include<iostream>
#include <string>

int main()
{
    using namespace std;
    using namespace stdext;
    hash_map<int, int>::iterator hm1_pIter, hm2_pIter;

    hash_map<int, int> hm1, hm2;
    typedef pair<int, int> Int_Pair;

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

    cout<< "The original elements (Key => Value) of hm1 are:";
    for (hm1_pIter = hm1.begin(); hm1_pIter != hm1.end(); hm1_pIter++)
        cout << endl << " " << hm1_pIter -> first << " => "
             << hm1_pIter->second;
    cout << endl;

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

    if (pr.second == true)
    {
        cout<< "The element 10 was inserted in hm1 successfully."
            << endl;
    }
    else
    {
        cout<< "The element 10 already exists in hm1\n with a key value of"
            << "((pr.first) -> first)= "<<(pr.first)-> first
            << "."<< endl;
    }

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

    cout<< "After the insertions, the elements of hm1 are:";
    for (hm1_pIter = hm1.begin(); hm1_pIter != hm1.end(); hm1_pIter++)
        cout << endl << " " << hm1_pIter -> first << " => "
             << hm1_pIter->second;
    cout << endl;

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

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

    cout<< "After the insertions, the elements of hm2 are:";
    for (hm2_pIter = hm2.begin(); hm2_pIter != hm2.end(); hm2_pIter++)
        cout << endl << " " << hm2_pIter -> first << " => "
             << hm2_pIter->second;
    cout << endl;

    // The templatized versions move constructing elements
    hash_map<int, string> hm3, hm4;
    pair<int, string> is1(1, "a"), is2(2, "b");

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

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

Wymagania

Nagłówek: <hash_map>

Przestrzeń nazw: stdext

Zobacz też

Informacje

hash_map — Klasa

Standardowa biblioteka szablonów