stl set是无序的_C ++ STL无序集– std :: unordered_set

stl set是无序的

In this tutorial you will learn about stl unordered set container in c++ and various functions applicable on it.

在本教程中,您将学习c ++中的stl无序集合容器以及适用于它的各种功能。

Unordered Set comes under unordered containers. As we discussed in introduction to stl article, unordered containers internally implemented with hash tables. Each item calculated by hash function, to map to hash table. The main advantage is if we have effective hash function we can find elements in O (1) time. On average it can go to linear time. Simply we can say it’s based on type of hash function used.

无序集位于无序容器下。 正如我们在stl文章的介绍中讨论的那样,内部使用哈希表实现的无序容器。 通过哈希函数计算的每个项目,以映射到哈希表 。 主要优点是,如果我们具有有效的哈希函数,我们可以在O(1)时间中找到元素。 平均而言,它可以达到线性时间。 简而言之,我们可以说它基于所使用的哈希函数的类型。

So we can say these are fastest among all containers.

因此,可以说这些是所有容器中最快的。

As name says that in unordered_set the order is not defined. Unordered set doesn’t allow duplicates.

顾名思义,在unordered_set中未定义顺序。 无序集不允许重复。

C ++ STL无序集– std :: unordered_set (C++ STL Unordered Set – std::unordered_set)

Useful iterators to work on this unordered set:

在此无序集合上工作的有用的迭代器:

begin(): returns iterator to the beginning

begin():将迭代器返回到开头

end(): returns iterator to the end of the list

end():将迭代器返回到列表的末尾

cbegin(): returns constant iterator to the beginning

cbegin():将常量迭代器返回到开头

cend(): returns constant iterator to the end.

cend():将常量迭代器返回到末尾。

First we need to include unordered_set header file. Which is #include<unordered_set>

首先,我们需要包含unordered_set头文件。 这是#include <unordered_set>

Inserting element into unordered_set:

将元素插入到unordered_set中:

There are various ways to insert elements into unordered_set.

有多种将元素插入unordered_set的方法。

Method 1: Insert directly by passing element.

方法1:通过传递元素直接插入。

UnOrdSet.insert(element);

UnOrdSet.insert(element);

Method 2: Using iterator. This returns iterator at inserted position.

方法2:使用迭代器。 这将在插入位置返回迭代器。

UnOrdSet.insert ( iterator, value)

UnOrdSet.insert(迭代器,值)

Method 3: Copying from another container.

方法3:从另一个容器复制。

Example program to insert into unordered_set:

插入unordered_set的示例程序:

#include<iostream>
#include <unordered_set>
 
using namespace std;
 
int main(){
	unordered_set<int> s1; // declaring unordered_set
	unordered_set<int> :: iterator it; // iterator for unordered_set
	
	for(int i=0;i<5;i++){
		s1.insert(i*10); // inserting using Method1
	}
	
	it= s1.begin();
	s1.insert(it,99);
	
	int ary[]= { 23, 34, 45, 56};
	s1.insert(ary, ary+4); // Inserting using method3
	
	//checking by printing
	for(it= s1.begin(); it!=s1.end(); it++) cout << *it << " ";
	
	// We can observe that output will be print in sorted order. That is the property of Unordered_set
	
	return 0;
}

Output

输出量

56 45 34 23 40 99 0 10 20 30

56 45 34 23 40 99 0 10 20 30

Modifiers: The functions which effect size/data of that container

修饰符:影响该容器大小/数据的函数

erase(): We can erase an element by specifying value or pointing to iterator.

delete():我们可以通过指定值或指向迭代器来擦除元素。

swap(): swaps elements of Unordered_set1 to Unorderedset2 and Unorderedset2 to Unordered_set1.

swap():将 Unordered_set1的元素交换为Unorderedset2,将Unorderedset2的元素交换为Unordered_set1。

clear(): removes all elements in the list. It results list of size 0.

clear():删除列表中的所有元素。 结果列表大小为0。

Example program to show above functions:

显示上述功能的示例程序:

#include<iostream>
#include<unordered_set>
 
using namespace std;
 
int main(){
	unordered_set<int> s1;
	unordered_set<int> :: iterator it;
	
	for(int i=0; i<5; i++) s1.insert(i+10);
	s1.erase(12); // deleting element 12
	cout << "elements after deleting  12 -->";
	for(it= s1.begin(); it!=s1.end(); it++) cout << *it << " ";
	cout << endl;
	
	unordered_set<int> s2;
	for(int i=0;i<4;i++) s2.insert(i);
	
	cout << "unordered_set 1 elements before swapping -->";
	for(it= s1.begin(); it!= s1.end(); it++) cout<< *it << " ";
	cout << endl;
	
	cout << "unordered_set 2 elements before swapping -->";
	for(it= s2.begin(); it!= s2.end(); it++) cout<< *it << " ";
	cout << endl;
	
	s1.swap(s2); // swapping operation
 
	cout << "unordered_set 1 elements after swapping -->";
	for(it= s1.begin(); it!= s1.end(); it++) cout<< *it << " ";
	cout << endl;
	
	cout << "unordered_set 2 elements after swapping -->";
	for(it= s2.begin(); it!= s2.end(); it++) cout<< *it << " ";
	cout << endl;
 
	s1.clear(); // clearing list 1
	cout << "Performing clear() operation on unordered_set1......" << endl;
	s1.empty() ? cout <<"Unordered_set is empty" << endl: cout << "unordered_set is not empty" << endl; //
	// ternary operation which resutls list is empty or not
	
	return 0;
}

Output

输出量

elements after deleting 12 –>14 10 11 13 unordered_set 1 elements before swapping –>14 10 11 13 unordered_set 2 elements before swapping –>3 2 1 0 unordered_set 1 elements after swapping –>3 2 1 0 unordered_set 2 elements after swapping –>14 10 11 13 Performing clear() operation on unordered_set1…… Unordered_set is empty

删除后的元素12 –> 14 10 11 13 交换前的unordered_set 1个元素–> 14 10 11 13 交换前的2个元素–> 3 2 1 0 交换后的unordered_set 1个元素–> 3 2 1 0 交换后的unordered_set 2个元素–> 14 10 11 13 在unordered_set1上执行clear()操作…… Unordered_set为空

Information retrieving functions:

信息检索功能:

empty(): returns a Boolean value whether Unordered_set is empty or not.

empty():无论Unordered_set是否为空,都返回一个布尔值。

size(): returns the size of the Unordered_set.

size():返回Unordered_set的大小。

max_size(): returns the maximum size a Unordered_set can have.

max_size():返回Unordered_set可以具有的最大大小。

And some more operations are:

还有更多的操作是:

find(): It returns iterator to the element.

find():将迭代器返回到元素。

count(x):  Returns how many times elements “x” present in Unordered_set.

count(x):返回Unordered_set中元素“ x”出现的次数。

Example program to show above functions:

显示上述功能的示例程序:

#include<iostream>
#include<unordered_set>
 
using namespace std;
 
int main(){
	unordered_set<int> s1;
	unordered_set<int> :: iterator it;
	
	for(int i=0; i<5; i++) s1.insert(i+10);
	s1.empty() ? cout <<"Unordered_set is empty" << endl: cout << "unordered_set is not empty" << endl;
	cout << "size of the unordered_set is " << s1.size() << endl;
	cout << "maximum size of the unordered_set is " << s1.max_size() << endl;
	cout << "finding elemnt 12 in unordered_set" << endl;
	it= s1.find(12);
	cout << *it << endl;
	
	s1.insert(12);
	if(s1.count(22)) cout << "number 22 is in the unordered_set " << endl;
	else cout << "22 is not in the unordered_set";
	
	return 0;
}

Output

输出量

unordered_set is not empty size of the unordered_set is 5 maximum size of the unordered_set is 1152921504606846975 finding elemnt 12 in unordered_set 12 22 is not in the unordered_set

unordered_set不为空unordered_set的 大小为5 unordered_set的 最大大小为1152921504606846975 发现unordered_set 12中的 元素 12 不在unordered_set中

翻译自: https://www.thecrazyprogrammer.com/2017/10/stl-unordered-set.html

stl set是无序的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值