📜  C++中的前向列表和无序映射列表及示例

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

C++中的前向列表和无序映射列表及示例

转发列表

STL中的前向列表实现了单链表。从 C++11 引入,前向列表在插入、删除和移动操作(如排序)中比其他容器更有用,并且允许时间常数插入和删除元素。它与列表的不同之处在于,前向列表仅跟踪下一个元素的位置,而列表同时跟踪下一个和前一个元素,从而增加了存储每个元素所需的存储空间。前向列表的缺点是它不能向后迭代,并且它的各个元素不能直接访问。当只需要前向遍历时,前向列表优于列表(就像单链表优于双向链表一样),因为我们可以节省空间。一些示例情况是,散列中的链接,图的邻接表表示等。

与前向列表一起使用的函数:

  • push_front(x):在前向列表的开头添加一个新元素“x”。
  • pop_front():该函数用于删除前向列表的第一个元素。

列表

列表 是允许非连续内存分配的序列容器。与向量相比,列表的遍历速度较慢,但一旦找到位置,插入和删除都很快。通常,当我们说 List 时,我们谈论的是双向链表。为了实现单链表,我们使用前向列表。

与列表一起使用的函数:

  • front():返回列表中第一个元素的值。
  • back():返回列表中最后一个元素的值。
  • push_front(x):在列表的开头添加一个新元素“x”。
  • push_back(x):在列表末尾添加一个新元素“x”。

Unordered_Map

Unordered map是一个关联的容器,它存储由键值和映射值组合形成的元素。键值用于唯一标识元素,映射的值是与键关联的内容。键和值都可以是预定义或用户定义的任何类型。在内部,使用Hash Table实现无序映射。

与无序映射一起使用的函数:

  • at(): C++ unordered_map 中的此函数返回对以元素为键 k 的值的引用。
  • begin():返回一个迭代器,指向unordered_map容器中容器的第一个元素
  • end():返回一个迭代器,指向 unordered_map 容器中容器中最后一个元素之后的位置
  • size():返回无序映射中存在的元素数量。

本文重点介绍如何在 C++ 中使用前向列表和无序映射列表。在设计复杂的数据结构时,列表向量和前向列表非常有用。

无序映射的前向列表

下面是使用无序映射的前向列表的实现:

示例 1:

C++
// C++ program to implement
// the above concept
#include 
using namespace std;
  
// Function to print forward 
// list elements
void print(forward_list >& 
           forwardList1)
{
  cout << "Forward List : \n";
    
  for (auto currentUnorderedMap : forwardList1) 
  {
    // Each element of the forward_list 
    // is a unordered map
  
    cout << "Unordered Map : ";
    cout << "[   ";
  
    // Print unordered map elements
    for (auto it = currentUnorderedMap.begin();
              it != currentUnorderedMap.end(); it++) 
    {
      cout << "First : " << it->first << " , " << 
              "Second : " << it->second << "   ";
    }
    cout << "]\n";
  }
}
  
// Driver code
int main()
{
  // Declaring a forward list of 
  // unordered maps
  forward_list > 
  forwardList1;
  
  // Declaring a unordered map
  unordered_map unorderedMap1;
  
  // Hashing values
  unorderedMap1[2] = 4;
  unorderedMap1[4] = 3;
  unorderedMap1[6] = 9;
  
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap1);
  
  // Declaring another unordered map
  unordered_map unorderedMap2;
  
  // Hashing values
  unorderedMap2[31] = 8;
  unorderedMap2[11] = 3;
  unorderedMap2[23] = 7;
  
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap2);
  
  // Declaring another unordered map
  unordered_map unorderedMap3;
  
  // Hashing values
  unorderedMap3[7] = 3;
  unorderedMap3[18] = 1;
  unorderedMap3[9] = 6;
    
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap3);
  
  // Declaring another unordered map
  unordered_map unorderedMap4;
  
  // Hashing values
  unorderedMap4[32] = 9;
  unorderedMap4[15] = 3;
  unorderedMap4[97] = 5;
  
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap4);
  
  print(forwardList1);
  return 0;
}


C++
// C++ program to implement
// the above concept
#include 
using namespace std;
  
// Function to print forward 
// list elements
void print(forward_list >& 
           forwardList1)
{
  cout << "Forward List : \n";
    
  for (auto currentUnorderedMap : forwardList1)
  {
    // Each element of the forward list is 
    // a unordered map
  
    cout << "Unordered Map : ";
    cout << "[   ";
  
    // Print unordered map elements
    for (auto it = currentUnorderedMap.begin();
              it != currentUnorderedMap.end(); it++) 
    {
      cout << "First : " << it->first << " , " << 
              "Second : " << it->second << "   ";
    }
    cout << "]\n";
  }
}
  
// Driver code
int main()
{
  // Declaring a forward_list of 
  // unordered maps
  forward_list > 
  forwardList1;
  
  // Declaring a unordered map
  unordered_map unorderedMap1;
  
  // Hashing values
  unorderedMap1[2] = "Geeks";
  unorderedMap1[4] = "for";
  unorderedMap1[6] = "Geeks";
  
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap1);
  
  // Declaring another unordered map
  unordered_map unorderedMap2;
  
  // Hashing values
  unorderedMap2[3] = "Python";
  unorderedMap2[11] = "Java";
  unorderedMap2[23] = "C++";
  
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap2);
  
  // Declaring another unordered map
  unordered_map unorderedMap3;
  
  // Hashing values
  unorderedMap3[7] = "C";
  unorderedMap3[18] = "PHP";
  unorderedMap3[9] = "Swift";
    
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap3);
  
  // Declaring another unordered map
  unordered_map unorderedMap4;
  
  // Hashing values
  unorderedMap4[121] = "Hello";
  unorderedMap4[97] = "Coding";
  unorderedMap4[197] = "World";
  
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap4);
  
  print(forwardList1);
  return 0;
}


C++
// C++ program to implement
// the above concept
#include 
using namespace std;
  
// Function to print list elements
void print(list >& 
           List)
{
  cout << "List : \n";
  for (auto currentUnorderedMap : List) 
  {
    // Each element of the list is 
    // a unordered map
    cout << "Unordered Map : ";
    cout << "[   ";
  
    // Print unordered map elements
    for (auto it = currentUnorderedMap.begin();
              it != currentUnorderedMap.end(); it++) 
    {
      cout << "First : " << it->first << " , " << 
              "Second : " << it->second << "   ";
    }
    cout << "]\n";
  }
}
  
// Driver code
int main()
{
  // Declaring a list of unordered maps
  list > List;
  
  // Declaring a unordered map
  unordered_map unorderedMap1;
  
  // Hashing values
  unorderedMap1[2] = 4;
  unorderedMap1[4] = 3;
  unorderedMap1[6] = 9;
  
  // Push back the unordered map 
  // in the list
  List.push_front(unorderedMap1);
  
  // Declaring another unordered map
  unordered_map unorderedMap2;
  
  // Hashing values
  unorderedMap2[31] = 8;
  unorderedMap2[11] = 3;
  unorderedMap2[23] = 7;
  
  // Push back the unordered map 
  // in the list
  List.push_front(unorderedMap2);
  
  // Declaring another unordered map
  unordered_map unorderedMap3;
  
  // Hashing values
  unorderedMap3[7] = 3;
  unorderedMap3[18] = 1;
  unorderedMap3[9] = 6;
    
  // Push back the unordered map 
  // in the list
  List.push_front(unorderedMap3);
  
  // Declaring another unordered map
  unordered_map unorderedMap4;
  
  // Hashing values
  unorderedMap4[32] = 9;
  unorderedMap4[15] = 3;
  unorderedMap4[97] = 5;
  
  // Push back the unordered map 
  // in the list
  List.push_front(unorderedMap4);
  
  print(List);
  return 0;
}


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
  
// Function to print list elements
void print(list >& List)
{
  cout << "List : \n";
  for (auto currentUnorderedMap : List) 
  {
    // Each element of the list is 
    // a unordered map
    cout << "Unordered Map : ";
    cout << "[   ";
  
    // Print unordered map elements
    for (auto it = currentUnorderedMap.begin();
              it != currentUnorderedMap.end(); it++) 
    {
      cout << "First : " << it->first << " , " << 
              "Second : " << it->second << "   ";
    }
    cout << "]\n";
  }
}
  
// Driver code
int main()
{
  // Declaring a list of unordered maps
  list > List;
  
  // Declaring a unordered map
  unordered_map unorderedMap1;
  
  // Hashing values
  unorderedMap1[2] = "Geeks";
  unorderedMap1[4] = "for";
  unorderedMap1[6] = "Geeks";
  
  // Push back the unordered map in 
  // the forward list
  List.push_front(unorderedMap1);
  
  // Declaring another unordered map
  unordered_map unorderedMap2;
  
  // Hashing values
  unorderedMap2[3] = "Python";
  unorderedMap2[11] = "Java";
  unorderedMap2[23] = "C++";
  
  // Push back the unordered map in 
  // the forward list
  List.push_front(unorderedMap2);
  
  // Declaring another unordered map
  unordered_map unorderedMap3;
  
  // Hashing values
  unorderedMap3[7] = "C";
  unorderedMap3[18] = "PHP";
  unorderedMap3[9] = "Swift";
    
  // Push back the unordered map in 
  // the forward list
  List.push_front(unorderedMap3);
  
  // Declaring another unordered map
  unordered_map unorderedMap4;
  
  // Hashing values
  unorderedMap4[121] = "Hello";
  unorderedMap4[97] = "Coding";
  unorderedMap4[197] = "World";
  
  // Push back the unordered map in 
  // the forward list
  List.push_front(unorderedMap4);
  
  print(List);
  return 0;
}


输出:

示例 2:

C++

// C++ program to implement
// the above concept
#include 
using namespace std;
  
// Function to print forward 
// list elements
void print(forward_list >& 
           forwardList1)
{
  cout << "Forward List : \n";
    
  for (auto currentUnorderedMap : forwardList1)
  {
    // Each element of the forward list is 
    // a unordered map
  
    cout << "Unordered Map : ";
    cout << "[   ";
  
    // Print unordered map elements
    for (auto it = currentUnorderedMap.begin();
              it != currentUnorderedMap.end(); it++) 
    {
      cout << "First : " << it->first << " , " << 
              "Second : " << it->second << "   ";
    }
    cout << "]\n";
  }
}
  
// Driver code
int main()
{
  // Declaring a forward_list of 
  // unordered maps
  forward_list > 
  forwardList1;
  
  // Declaring a unordered map
  unordered_map unorderedMap1;
  
  // Hashing values
  unorderedMap1[2] = "Geeks";
  unorderedMap1[4] = "for";
  unorderedMap1[6] = "Geeks";
  
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap1);
  
  // Declaring another unordered map
  unordered_map unorderedMap2;
  
  // Hashing values
  unorderedMap2[3] = "Python";
  unorderedMap2[11] = "Java";
  unorderedMap2[23] = "C++";
  
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap2);
  
  // Declaring another unordered map
  unordered_map unorderedMap3;
  
  // Hashing values
  unorderedMap3[7] = "C";
  unorderedMap3[18] = "PHP";
  unorderedMap3[9] = "Swift";
    
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap3);
  
  // Declaring another unordered map
  unordered_map unorderedMap4;
  
  // Hashing values
  unorderedMap4[121] = "Hello";
  unorderedMap4[97] = "Coding";
  unorderedMap4[197] = "World";
  
  // Push back the unordered map in 
  // the forward list
  forwardList1.push_front(unorderedMap4);
  
  print(forwardList1);
  return 0;
}

输出:

无序地图列表

下面是使用无序映射列表的实现:

示例 1:

C++

// C++ program to implement
// the above concept
#include 
using namespace std;
  
// Function to print list elements
void print(list >& 
           List)
{
  cout << "List : \n";
  for (auto currentUnorderedMap : List) 
  {
    // Each element of the list is 
    // a unordered map
    cout << "Unordered Map : ";
    cout << "[   ";
  
    // Print unordered map elements
    for (auto it = currentUnorderedMap.begin();
              it != currentUnorderedMap.end(); it++) 
    {
      cout << "First : " << it->first << " , " << 
              "Second : " << it->second << "   ";
    }
    cout << "]\n";
  }
}
  
// Driver code
int main()
{
  // Declaring a list of unordered maps
  list > List;
  
  // Declaring a unordered map
  unordered_map unorderedMap1;
  
  // Hashing values
  unorderedMap1[2] = 4;
  unorderedMap1[4] = 3;
  unorderedMap1[6] = 9;
  
  // Push back the unordered map 
  // in the list
  List.push_front(unorderedMap1);
  
  // Declaring another unordered map
  unordered_map unorderedMap2;
  
  // Hashing values
  unorderedMap2[31] = 8;
  unorderedMap2[11] = 3;
  unorderedMap2[23] = 7;
  
  // Push back the unordered map 
  // in the list
  List.push_front(unorderedMap2);
  
  // Declaring another unordered map
  unordered_map unorderedMap3;
  
  // Hashing values
  unorderedMap3[7] = 3;
  unorderedMap3[18] = 1;
  unorderedMap3[9] = 6;
    
  // Push back the unordered map 
  // in the list
  List.push_front(unorderedMap3);
  
  // Declaring another unordered map
  unordered_map unorderedMap4;
  
  // Hashing values
  unorderedMap4[32] = 9;
  unorderedMap4[15] = 3;
  unorderedMap4[97] = 5;
  
  // Push back the unordered map 
  // in the list
  List.push_front(unorderedMap4);
  
  print(List);
  return 0;
}

输出:

示例 2:

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
  
// Function to print list elements
void print(list >& List)
{
  cout << "List : \n";
  for (auto currentUnorderedMap : List) 
  {
    // Each element of the list is 
    // a unordered map
    cout << "Unordered Map : ";
    cout << "[   ";
  
    // Print unordered map elements
    for (auto it = currentUnorderedMap.begin();
              it != currentUnorderedMap.end(); it++) 
    {
      cout << "First : " << it->first << " , " << 
              "Second : " << it->second << "   ";
    }
    cout << "]\n";
  }
}
  
// Driver code
int main()
{
  // Declaring a list of unordered maps
  list > List;
  
  // Declaring a unordered map
  unordered_map unorderedMap1;
  
  // Hashing values
  unorderedMap1[2] = "Geeks";
  unorderedMap1[4] = "for";
  unorderedMap1[6] = "Geeks";
  
  // Push back the unordered map in 
  // the forward list
  List.push_front(unorderedMap1);
  
  // Declaring another unordered map
  unordered_map unorderedMap2;
  
  // Hashing values
  unorderedMap2[3] = "Python";
  unorderedMap2[11] = "Java";
  unorderedMap2[23] = "C++";
  
  // Push back the unordered map in 
  // the forward list
  List.push_front(unorderedMap2);
  
  // Declaring another unordered map
  unordered_map unorderedMap3;
  
  // Hashing values
  unorderedMap3[7] = "C";
  unorderedMap3[18] = "PHP";
  unorderedMap3[9] = "Swift";
    
  // Push back the unordered map in 
  // the forward list
  List.push_front(unorderedMap3);
  
  // Declaring another unordered map
  unordered_map unorderedMap4;
  
  // Hashing values
  unorderedMap4[121] = "Hello";
  unorderedMap4[97] = "Coding";
  unorderedMap4[197] = "World";
  
  // Push back the unordered map in 
  // the forward list
  List.push_front(unorderedMap4);
  
  print(List);
  return 0;
}

输出: