다음을 통해 공유


hash_map::insert

참고

이 API는 사용되지 않습니다.unordered_map 클래스를 대신 사용하는 것이 좋습니다.

Inserts an element or a range of elements into a 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
);

매개 변수

Parameter

설명

_Val

The value of an element to be inserted into the hash_map unless the hash_map already contains that element (or, more generally, an element whose key is equivalently ordered).

_Where

정확한 지점으로 삽입에 대한 검색을 시작할 위치에 대한 힌트입니다.

_First

The position of the first element to be copied from a hash_map.

_Last

The position just beyond the last element to be copied from a hash_map.

반환 값

The first insert member function returns a pair whose bool component returns true if an insertion was made and false if the hash_map already contained an element whose key had an equivalent value in the ordering, and whose iterator component returns the address where a new element was inserted or where the element was already located.

To access the iterator component of a pair pr returned by this member function, use pr.first, and to dereference it, use *(pr.first). To access the bool component of a pair pr returned by this member function, use pr.second, and to dereference it, use *(pr.second).

The second insert member function, the hint version, returns an iterator that points to the position where the new element was inserted into the hash_map.

The last two insert member functions behave the same as the first two, except that they move construct the inserted value.

설명

요소의 값을 키 값과 같은 첫 번째 구성 요소와 요소의 데이터 값과 같은 두 번째 구성 요소를 사용하여 정렬된 된 쌍이 될 수 있도록 요소의 value_type 는 한 쌍으로 되어있습니다.

삽입 지점이 _Where 바로 뒤에 있는 경우, 삽입은 로그 시간 대신 힌트 버전을 위해 분할된 일정한 시간에 발생할 수 있습니다.

The third member function inserts the sequence of element values into a hash_map corresponding to each element addressed by an iterator of in the range [First, Last) of a specified set.

예제

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

요구 사항

헤더: <hash_map>

네임스페이스: stdext

참고 항목

참조

hash_map 클래스

표준 템플릿 라이브러리