unordered_set::insert

添加元素。

std::pair<iterator, bool> insert(const value_type& val);
iterator insert(iterator where, const value_type& val);
template<class InIt>
    void insert(InIt first, InIt last);
template<class ValTy>
    pair<iterator, bool> insert(ValTy&& val);
template<class ValTy>
    iterator insert(const_iterator where, ValTy&& val);

参数

Parameter

说明

InIt

迭代器类型。

ValTy

就地构造函数参数类型。

first

范围开头插入的。

last

范围的末尾插入的。

val

要插入的值。

where

在插入(仅提示的容器)。

备注

第一个成员函数确定元素 X 是否存在于键具有相同顺序对该 val的序列。否则,它创建这样一个元素 X 并将它初始化与 val。函数来确定指定 X的迭代器 where。如果插入发生的事件,函数返回 std::pair(where, true)。否则,该调用将返回 std::pair(where, false)。

在搜索的控件序列中的起始位置插入点,第二个成员函数返回 insert(val).first,使用 where。(插入某些可能更快,可能会发生,如果插入点紧邻或遵循 where。)

元素顺序值的第三个成员函数插入,每 where 的范围内 [first, last),通过调用 insert(*where)。

最后两个成员函数的行为与前两个相同,不同之处在于,val 用于构造该插入的值。

如果在单个元素的插入时引发,容器未更改,并且异常来重新引发。如果在多个组件的插入时引发,容器在稳定左侧,但未指定的状态和异常来重新引发。

示例

// std_tr1__unordered_set__unordered_set_insert.cpp 
// compile with: /EHsc 
#include <unordered_set> 
#include <iostream>
#include <string> 
 
typedef std::unordered_set<char> Myset; 
int main() 
    { 
    Myset c1; 
 
    c1.insert('a'); 
    c1.insert('b'); 
    c1.insert('c'); 
 
// display contents " [c] [b] [a]" 
    for (Myset::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << *it << "]"; 
    std::cout << std::endl; 
 
// insert with hint and reinspect 
    Myset::iterator it2 = c1.insert(c1.begin(), 'd'); 
    for (Myset::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << *it << "]"; 
    std::cout << std::endl; 
 
// insert range and inspect 
    Myset c2; 
 
    c2.insert(c1.begin(), c1.end()); 
    for (Myset::const_iterator it = c2.begin(); 
        it != c2.end(); ++it) 
        std::cout << " [" << *it << "]"; 
    std::cout << std::endl; 
 
// insert with checking and reinspect 
    std::pair<Myset::iterator, bool> pib = 
        c1.insert('e'); 
    std::cout << "insert(['a',]) success == " 
        << std::boolalpha << pib.second << std::endl; 
    pib = c1.insert('a'); 
    std::cout << "insert(['a',]) success == " 
        << std::boolalpha << pib.second << std::endl; 
    for (Myset::const_iterator it = c1.begin(); 
        it != c1.end(); ++it) 
        std::cout << " [" << *it << "]"; 
    std::cout << std::endl; 

// The templatized versions move constructing elements
    unordered_set<string> c3, c4;
    string str1("a"), str2("b");

    c3.insert(move(str1));
    cout << "After the move insertion, c3 contains: "
        << *c3.begin() << endl;

    c4.insert(c4.begin(), move(str2));
    cout << "After the move insertion, c4 contains: "
        << *c4.begin() << endl;
 
     return (0); 
    } 
 
  

要求

标头: <unordered_set>

命名空间: std

请参见

参考

<unordered_set>

unordered_set Class