📜  C ++中的无序对集与示例

📅  最后修改于: 2022-05-13 01:55:19.116000             🧑  作者: Mango

C ++中的无序对集与示例

什么是对?

C++ 中的实用程序标头为我们提供了配对容器。一对由两个数据元素或对象组成。

  • 第一个元素被称为“第一”,第二个元素被称为“第二”,并且顺序是固定的(第一,第二)。
  • Pair 用于将类型可能不同的两个值组合在一起。 Pair 提供了一种将两个异构对象存储为一个单元的方法。
  • 可以分配、复制和比较对。在 map 或 hash_map 中分配的对象数组默认为“pair”类型,其中所有“first”元素都是与其“second”值对象关联的唯一键。
  • 要访问元素,我们使用变量名后跟点运算符,然后是关键字 first 或 second。

如何访问一对?

我们可以使用点 (.)运算符访问一对元素,如下面的代码片段所述,

auto fistElement = myPair.first;
auto fistElement = myPair.second;

什么是无序集?

无序集是一个关联容器,类似于集合,但在无序集的情况下,元素不按任何特定顺序排列。无序集合可以像集合一样包含唯一元素。无序集在内部使用散列表实现,键被散列到散列表的索引中。无序集中的插入总是不可预测或随机的。无序集中的大多数操作都需要固定时间 O(1),但在最坏的情况下,时间复杂度可能会上升到 O(n)。

与无序集相关的函数:

  • insert(x) 在无序集合容器中插入一个新元素“x”。
  • begin() 返回一个指向无序集合容器中第一个元素的迭代器。
  • end() 返回一个迭代器,指向紧邻结束元素的假设元素。
  • count() 计算特定元素在无序集合容器中出现的次数。
  • erase() 删除单个元素或从开始到结束的一系列元素(不包括)。
  • size() 返回无序集合容器中的元素个数。
  • swap() 交换两个无序集合容器的值。
  • max_size() 返回无序集容器可以容纳的最大元素数。
  • empty() 检查无序集合容器是否为空。

在实现复杂的数据结构时,一组无序的对非常有用。本文重点介绍如何在 C++ 中创建无序对集。

无序对集

无序对集合是一个无序集合,其中每个元素本身就是一对。默认情况下,C++ 不允许我们直接创建无序的对集合,但可以将散列函数传递给无序集合容器。

句法:

示例 1:下面是 C++ 程序,用于演示无序对集的工作。

C++
// C++ program to demonstrate 
// the working of unordered set 
// of pairs
#include 
using namespace std;
  
// Hash function 
struct hashFunction
{
  size_t operator()(const pair &x) const
  {
    return x.first ^ x.second;
  }
};
  
// Function to print unordered set elements
void print(unordered_set, 
           hashFunction> &myUnorderedSet)
{ 
  // Iterating over unordered set elements
  for (auto currentPair : myUnorderedSet)
  {
    // Each element is a pair itself
    pair pr = currentPair;
  
    cout << "[ ";
  
    // Printing pair elements
    cout << pr.first << "  " << 
            pr.second;
    cout << " ]";
    cout << "\n";
  }
}
  
// Driver code
int main()
{
  // Declaring an unordered set of pairs
  unordered_set, 
  hashFunction> myUnorderedSet;
  
  // Initializing pairs of int type
  pair pair1;
  pair1 = make_pair(4, 2);
  
  pair pair2;
  pair2 = make_pair(2, 3);
  
  pair pair3;
  pair3 = make_pair(2, 3);
  
  pair pair4;
  pair4 = make_pair(5, 8);
  
  pair pair5;
  pair5 = make_pair(9, 5);
  
  
  // Inserting pairs in the unordered set
  myUnorderedSet.insert(pair1);
  myUnorderedSet.insert(pair2);
  myUnorderedSet.insert(pair3);
  myUnorderedSet.insert(pair4);
  myUnorderedSet.insert(pair5);
  
  // Calling print function
  print(myUnorderedSet);
  return 0;
}


C++
// C++ program to demonstrate 
// the working of unordered set 
// of pairs
#include 
using namespace std;
  
// Hash function 
struct hashFunction
{
  size_t operator()(const pair &x) const
  {
    return x.first ^ x.second;
  }
};
  
// Function to print unordered set elements
void print(unordered_set,
           hashFunction> &myUnorderedSet)
{ 
  // Iterating over unordered set elements
  for (auto currentPair : myUnorderedSet)
  {
    // Each element is a pair itself
    pair pr = currentPair;
  
    cout << "[ ";
  
    // Printing pair elements
    cout << pr.first << "  " << 
            pr.second;
    cout << " ]";
    cout << "\n";
  }
}
  
// Driver code
int main()
{
  // Declaring an unordered set of pairs
  // by passing a hash function as a
  // second argument to the unordered set
  unordered_set, 
  hashFunction> myUnorderedSet;
  
  // Initializing pairs of bool type
  pair pair1;
  pair1 = make_pair(0, 0);
  
  pair pair2;
  pair2 = make_pair(0, 1);
  
  pair pair3;
  pair3 = make_pair(1, 0);
  
  pair pair4;
  pair4 = make_pair(1, 1);
  
  pair pair5;
  pair5 = make_pair(0, 0);
  
  
  // Inserting pairs in the unordered set
  myUnorderedSet.insert(pair1);
  myUnorderedSet.insert(pair2);
  myUnorderedSet.insert(pair3);
  myUnorderedSet.insert(pair4);
  myUnorderedSet.insert(pair5);
  
  // Calling print function
  print(myUnorderedSet);
  return 0;
}


输出:

示例 2:下面是 C++ 程序,用于演示无序对集的工作。

C++

// C++ program to demonstrate 
// the working of unordered set 
// of pairs
#include 
using namespace std;
  
// Hash function 
struct hashFunction
{
  size_t operator()(const pair &x) const
  {
    return x.first ^ x.second;
  }
};
  
// Function to print unordered set elements
void print(unordered_set,
           hashFunction> &myUnorderedSet)
{ 
  // Iterating over unordered set elements
  for (auto currentPair : myUnorderedSet)
  {
    // Each element is a pair itself
    pair pr = currentPair;
  
    cout << "[ ";
  
    // Printing pair elements
    cout << pr.first << "  " << 
            pr.second;
    cout << " ]";
    cout << "\n";
  }
}
  
// Driver code
int main()
{
  // Declaring an unordered set of pairs
  // by passing a hash function as a
  // second argument to the unordered set
  unordered_set, 
  hashFunction> myUnorderedSet;
  
  // Initializing pairs of bool type
  pair pair1;
  pair1 = make_pair(0, 0);
  
  pair pair2;
  pair2 = make_pair(0, 1);
  
  pair pair3;
  pair3 = make_pair(1, 0);
  
  pair pair4;
  pair4 = make_pair(1, 1);
  
  pair pair5;
  pair5 = make_pair(0, 0);
  
  
  // Inserting pairs in the unordered set
  myUnorderedSet.insert(pair1);
  myUnorderedSet.insert(pair2);
  myUnorderedSet.insert(pair3);
  myUnorderedSet.insert(pair4);
  myUnorderedSet.insert(pair5);
  
  // Calling print function
  print(myUnorderedSet);
  return 0;
}

输出: