c++中stl 哈希函数_C ++ STL中的set :: emplace()函数

c++中stl 哈希函数

C ++ STL set :: emplace()函数 (C++ STL set::emplace() function)

set::emplace() function is a predefined function, it is used to insert a new element to the set, if element is unique.

set :: emplace()函数是预定义的函数,如果element是唯一的,则用于将新元素插入集合。

Prototype:

原型:

    set<T> st; //declaration
    st.emplace(T item);

Parameter: T item; //T is the data type

参数: T项; // T是数据类型

Return type: If it successfully inserts then it returns a pair<Iterator pointer to the inserted value, True&gt, Else It returns a pair<iterator to the existing value in the set, False>

返回类型:如果成功插入,则返回对<迭代器指针,指向插入的值True&gt ,否则返回对<迭代器 ,返回集合中的现有值False>

Usage: The function inserts unique elements to the set.

用法:该函数在集合中插入唯一元素。

Example:

例:

    For a set of integer,
    set<int> st;
    st.emplace(5);
    st.emplace(4);
    set content: //sorted always(ordered)
        4
        5

    st.emplace(5) //no insertion this time
    set content: 
        4
        5

Header file to be included:

包含的头文件:

    #include <iostream>
    #include <set>
    OR
    #include <bits/stdc++.h>

C++ implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

void printSet(set<int> st){
	set<int>:: iterator it;
	cout<<"Set contents are:\n";
	if(st.empty()){
		cout<<"empty set\n";
		return;
	}
	for(it=st.begin();it!=st.end();it++)
		cout<<*it<<" ";
	cout<<endl;
}

int main(){
	cout<<"Example of emplace function\n";
	set<int> st;
	set<int>:: iterator it;
	cout<<"inserting 4\n";
	st.emplace(4);
	cout<<"inserting 6\n";
	st.emplace(6);
	cout<<"inserting 10\n";
	st.emplace(10);

	printSet(st); //printing current set
	
	return 0;
}

Output

输出量

Example of emplace function
inserting 4
inserting 6
inserting 10
Set contents are:
4 6 10


翻译自: https://www.includehelp.com/stl/set-emplace-function-in-cpp-stl.aspx

c++中stl 哈希函数

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值