C++ Unordered Set

本文翻译自: C++ Unordered SetC++ STL Containers

先弄清楚什么是C++中的容器:

容器是一种用于存储特定类型对象集合的对象。C++的STL基于不同的需要提供了不同类型的容器。

C++的STL提供了3种容器:
(1) 顺序容器
(2) 关联容器
(3) 无序关联容器

1.C++的顺序容器
顺序容器允许我们存储可以以顺序访问的元素。在内部,顺序容器以数组或链表的方式实现。包括:
Array
Vector
Deque
List
Forward List

接下来以vector为例:

#include <iostream>
#include <vector>
using namespace std;

int main() {

  // initialize a vector of int type
  vector<int> numbers = {1, 100, 10, 70, 100};

  // print the vector
  cout << "Numbers are: ";
  for(auto &num: numbers) {
    cout << num << ", ";
  }

  return 0;
}

输出:

Numbers are: 1, 100, 10, 70, 100,

这里面C++使用了一种特殊的for循环,具体参考C++ Ranged for Loop

2.C++的关联容器
在C++中,关联容器允许我们以有序的方式存储元素,其顺序不取决于元素插入的时间。在内部,它们以二叉树结构实现,它们包括:
Set
Map
Multiset
Multimap

接下来我们就以set类为例:

#include <iostream>
#include <set>
using namespace std;

int main() {

  // initialize a set of int type
  set<int> numbers = {1, 100, 10, 70, 100};

  // print the set
  cout << "Numbers are: ";
  for(auto &num: numbers) {
    cout << num << ", ";
  }

  return 0;
}

输出:

Numbers are: 1, 10, 70, 100,

在输出中,我们可以看到数字按升序排列,并且重复的数字已被移除。

3.C++中的无序关联容器
该容器为关联容器的无序版本,内部通过散列表实现,包括:
Unordered Set
Unordered Map
Unordered Multiset
Unordered Multimap

接下来以unordered_set为例:

#include <iostream>
#include <unordered_set>
using namespace std;

int main() {

  // initialize an unordered_set of int type
  unordered_set<int> numbers = {1, 100, 10, 70, 100};

  // print the set
  cout << "Numbers are: ";
  for(auto &num: numbers) {
    cout << num << ", ";
  }

  return 0;
}

输出:

Numbers are: 70, 10, 100, 1,

从输出中我们可以看出,重复的元素被去掉了,但是元素并没有被排成升序。

4.C++中的容器适配器
在C++中,容器适配器是指采用现有的STL容器,并提供受限接口以使其表现出不同行为的一种机制。例如,stack就是一个容器适配器,它使用顺序容器deque,并提供了受限接口来支持push()和pop()操作。

容器适配器有:
Stack
Queue
Priority Queue

接下来以stack为例:

#include <iostream>
#include <stack>
using namespace std;

int main() {

  // create a stack of ints
  stack<int> numbers;

  // push into stack
  numbers.push(1);
  numbers.push(100);
  numbers.push(10);

  cout << "Numbers are: ";

  // we can access the element by getting the top and popping
  // until the stack is empty
  while(!numbers.empty()) {
    // print top element
    cout << numbers.top() << ", ";

    // pop top element from stack
    numbers.pop();
  }

  return 0;
}

输出:

Numbers are: 10, 100, 1,

与其他的容器不同,我们没有使用直接的初始化,因为容器适配器限制直接对其赋值。类似,我们也不允许直接遍历容器。在上述程序中:

numbers.empty() —— 判断stack是否为空
numbers.top() —— 返回stack顶部的元素
numbers.pop() —— 移除stack顶部的元素


以上就是C++ STL Containers的内容,然后具体看看这unordered_set

STL中的unordered_set是一种无序关联容器,提供了无序集合数据结构的函数。与常规的集合相反,无序集合中的数值的顺序是未定义的。

并且这个无序集合是以散列表数据结构实现的。

1.创建C++ STL的unordered_set
首先需要头文件:

#include <unordered_set>

随后我们根据以下句法创建一个unordered set:

unordered_set<data_type> ust;

2.初始化一个unordered set:
类似下面这样既可:

unordered_set<int> unordered_set1 = {1, 100, 2, 9};

也可以不用等号

// Uniform Initialization
unordered_set<int> unordered_set2 {1, 100, 2, 9};

3.案例
如下代码:

#include <iostream>
#include <unordered_set>
using namespace std;

int main() {

  // uniform initialization
  unordered_set<int> numbers {1, 100, 10, 70, 100};

  // loop across the unordered set
  // display the value
  cout << "numbers = ";
  for(auto &num: numbers) {
    cout << num << ", ";
  }

  return 0;
}

输出如下:

numbers = 70, 10, 100, 1, 

从输出来看,没有重复元素,且没有任何特定顺序。

4.unordered set的方法
在这里插入图片描述

5.往unordered set中插入元素
下面的示例:

#include <iostream>
#include <unordered_set>
using namespace std;

int main() {

  unordered_set<string> countries;

  // inserts "Nepal" into countries
  countries.insert("Nepal");

  // inserts "Nepal" , "India", "USA", "Korea" into countries
  countries.insert({"Nepal", "India", "USA", "Korea"});

  cout << "Countries = ";
  // display elements of countries
  for(const auto& country: countries) {
    cout << country << ", ";
  }
  return 0;
}

输出:

Countries = Korea, USA, India, Nepal, 

6.删除unordered set中的某一个特定元素:
实例如下:

#include <iostream>
#include <unordered_set>
using namespace std;

// function prototype for display_unordered_set()
void display_unordered_set(const unordered_set<string> &);

int main() {

  unordered_set<string> languages {"C++", "Python", "Java", "PHP"};

  cout << "Initial unordered set:\n";
  display_unordered_set(languages);

  // remove element with value: "Python"  
  languages.erase("Python");

  cout << "\n\nFinal unordered set: \n";
  display_unordered_set(languages);

  return 0;
}

// utility function to print unordered_set elements
void display_unordered_set(const unordered_set<string> &uset) {
  for(const auto& value: uset) {
    cout << value << ", ";
  }
}

输出:

Initial unordered set:
PHP, Java, Python, C++, 

Final unordered set: 
PHP, Java, C++,

7.判断在unordered set中是否有特定的数值
有两种方法可用:
(1) find() —— returns the iterator to the element if the value is found, else returns the end() iterator
(2) count() —— returns 1 if the value exists and 0 if not

案例如下:

#include <iostream>
#include <unordered_set>
using namespace std;

int main() {

  unordered_set<int> primes {2, 3, 5, 7, 11, 13};

  cout << "Using find():" << endl;
  cout << "Does number " << 12 << " exist? ";

  // find() returns primes.end() if the value is not found 
  if (primes.find(12) != primes.end()) {
    cout << "Yes";
  }
  else {
    cout << "No";
  }

  cout << "\n\nUsing count():" << endl;
  cout << "Does number " << 7 << " exist? ";

  // count() returns 0 if the value doesn't exist
  if (primes.count(7)) {
    cout << "Yes";
  }
  else {
    cout << "No";
  }

  return 0;
}

输出:

Using find():
Does number 12 exist? No

Using count():
Does number 7 exist? Yes
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值