![]() |
---|
此 API 已过时。另一种方法是 unordered_set Class。 |
插入元素或元素的范围。hash_set中。
pair<iterator, bool> insert(
const value_type& _Val
);
iterator insert(
iterator _Where,
const value_type& _Val
);
template<class InputIterator>
void insert(
InputIterator _First,
InputIterator _Last
);
参数
Parameter |
描述 |
_Val |
要插入的元素的值设置为 hash_set,除非 hash_set,通常,已包含该元素或键相同地排序的元素。 |
_Where |
起始位置搜索正确位置插入。(插入在能够的常量时发生,而不是对数时,因此,如果插入点紧跟在 _Where。) |
_First |
从 hash_set将复制的第一个元素的位置。 |
_Last |
在从 hash_set将复制的最后一个元素之外。 |
返回值
第一个 insert 成员函数返回 bool 组件返回 true 的对,如果插入是使和 false,如果 hash_set 已经包含一个键具有等效值顺序,并且,迭代器元素返回地址插入或新元素的元素已找到其中的元素。
访问对的迭代器元素 pr 由该成员函数,使用 pr.first 返回和取消引用它,使用 *(pr.first)。访问对的 bool 元素 pr 由该成员函数,使用 pr.second返回和取消引用它,使用 *(pr.second)。
第二个 insert 成员函数返回指向位置新元素插入 hash_set的迭代器。
备注
元素顺序值到 hash_set 的每个元素对应的第三成员函数插入由迭代器在范围解析的 [_First,_Last) 的指定 hash_set。
示例
// hash_set_insert.cpp
// compile with: /EHsc
#include <hash_set>
#include <iostream>
int main( )
{
using namespace std;
using namespace stdext;
hash_set <int>::iterator hs1_pIter, hs2_pIter;
hash_set <int, hash_compare <int, less<int> > > hs1, hs2;
hs1.insert( 10 );
hs1.insert( 20 );
hs1.insert( 30 );
hs1.insert( 40 );
cout << "The original hs1 =";
for ( hs1_pIter = hs1.begin( ); hs1_pIter != hs1.end( );
hs1_pIter++ )
cout << " " << *hs1_pIter;
cout << "." << endl;
pair< hash_set<int>::iterator, bool > pr;
pr = hs1.insert( 10 );
if(pr.second == true)
{
cout << "The element 10 was inserted in hs1 successfully."
<< endl;
}
else
{
cout << "The element 10 already exists in hs1 and"
<< " *( pr.first ) = " << *( pr.first ) << "."
<< endl;
}
hs1.insert( --hs1.end( ), 50 );
cout << "After the insertions, hs1 =";
for ( hs1_pIter = hs1.begin( ); hs1_pIter != hs1.end( );
hs1_pIter++ )
cout << " " << *hs1_pIter;
cout << "." << endl;
hs2.insert( 100 );
hs2.insert( ++hs1.begin( ), --hs1.end( ) );
cout << "hs2 =";
for ( hs2_pIter = hs2.begin( ); hs2_pIter != hs2.end( );
hs2_pIter++ )
cout << " " << *hs2_pIter;
cout << "." << endl;
}
要求
标头: <hash_set>
命名空间: stdext