📜  C++ STL中list和forward_list的映射及示例

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

C++ STL中list和forward_list的映射及示例

地图是以映射方式存储元素的关联容器。每个元素都有一个键值和一个映射值。没有两个映射值可以具有相同的键值。

列表是允许非连续内存分配的序列容器。与向量相比,列表的遍历速度较慢,但一旦找到位置,插入和删除都很快。列表仅表示双向链表,对于单链表,使用前向列表。

STL中的列表映射

列表映射在设计复杂的数据结构时非常有用。

句法:

map> map_of_list 
This stores a list corresponding to a datatype

or

map, datatype> map_of_list
This stores a datatype corresponding to a list

下面是 C++ 中 List 映射的实现-

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
  
void printMapContent1(map,
                          int>& mapOfList)
{
    cout << "   Key         Value"
         << "\n\n";
    for (auto p : mapOfList) {
        // Key is a list of integers
        list ourList = p.first;
  
        // Value is an integer
        int val = p.second;
  
        // Printing list elements
        cout << "[ ";
        for (auto it = ourList.begin();
             it != ourList.end(); it++) {
            // Dereferencing value pointed by
            // iterator
            cout << (*it) << ' ';
        }
  
        cout << ']';
  
        cout << "     ";
  
        // Printing value
        cout << mapOfList[ourList] << '\n';
    }
}
  
void printMapContent2(map >& mapOfList)
{
  
    cout << "   Key         Value"
         << "\n\n";
    for (auto p : mapOfList) {
        // Key is an integer
        int key = p.first;
  
        // Value is a list of integers
        list ourList = p.second;
  
        cout << "   ";
        cout << key << "          ";
  
        // Printing list elements
        cout << "[ ";
        for (auto it = ourList.begin();
             it != ourList.end(); it++) {
            // Dereferencing value pointed by
            // iterator
            cout << (*it) << ' ';
        }
  
        cout << ']';
        cout << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a list of integers
    list ourList1;
  
    // Inserting elements at the
    // back of list
    ourList1.push_back(2);
    ourList1.push_back(10);
    ourList1.push_back(13);
  
    // Declaring another list
    list ourList2;
  
    // Inserting elements at the back
    // of list
    ourList2.push_back(7);
    ourList2.push_back(14);
    ourList2.push_back(22);
  
    // Declaring a map where key is a list
    // and value is integer itself
    map, int> mapOfList1;
  
    mapOfList1[ourList1] = 5;
    mapOfList1[ourList2] = 10;
  
    // Printing the contents of the map
    printMapContent1(mapOfList1);
  
    // Declaring a map where key is integer
    // and value is a list of integers
    map > mapOfList2;
  
    cout << "\n\n";
    mapOfList2[3] = ourList1;
    mapOfList2[7] = ourList2;
  
    printMapContent2(mapOfList2);
  
    return 0;
}


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
  
void printMapContent1(map,
                          int>& mapOfList)
{
    cout << "   Key         Value"
         << "\n\n";
    for (auto p : mapOfList) {
        // Key is a list of integers
        forward_list ourList = p.first;
  
        // Value is an integer
        int val = p.second;
  
        // Printing list elements
        cout << "[ ";
        for (auto it = ourList.begin();
             it != ourList.end(); it++) {
            // Dereferencing value pointed by
            // iterator
            cout << (*it) << ' ';
        }
  
        cout << ']';
        cout << "     ";
  
        // Printing value
        cout << mapOfList[ourList] << '\n';
    }
}
  
void printMapContent2(map >& mapOfList)
{
  
    cout << "   Key         Value"
         << "\n\n";
    for (auto p : mapOfList) {
        // Key is an integer
        int key = p.first;
  
        // Value is a list of integers
        forward_list ourList = p.second;
  
        cout << "   ";
        cout << key << "          ";
  
        // Printing list elements
        cout << "[ ";
        for (auto it = ourList.begin();
             it != ourList.end(); it++) {
            // Dereferencing value pointed by
            // iterator
            cout << (*it) << ' ';
        }
  
        cout << ']';
        cout << "\n";
    }
}
  
// Driver code
int main()
{
    // Declaring forward list
    forward_list forwardList1;
  
    // Declaring another forward list
    forward_list forwardList2;
  
    // Assigning values using assign()
    forwardList1.assign({ 5, 3, 13 });
    forwardList2.assign({ 8, 9, 13 });
  
    map,
        int>
        mapOfList1;
  
    mapOfList1[forwardList1] = 3;
    mapOfList1[forwardList2] = 7;
  
    printMapContent1(mapOfList1);
    cout << "\n\n";
  
    map >
        mapOfList2;
  
    mapOfList2[3] = forwardList1;
    mapOfList2[7] = forwardList2;
  
    printMapContent2(mapOfList2);
    return 0;
}


输出
Key         Value

[ 2 10 13 ]     5
[ 7 14 22 ]     10


   Key         Value

   3          [ 2 10 13 ]
   7          [ 7 14 22 ]

STL 中的转发列表

STL中的前向列表实现了单链表。从 C++11 引入,前向列表在插入、删除和移动操作(如排序)中比其他容器更有用,并且允许时间常数插入和删除元素。 forward_list 也可以与地图容器一起使用。

句法:

map> map_of_list
This stores a forward list corresponding to a datatype

or

map, datatype> map_of_list
This stores a datatype corresponding to a forward list

下面是C++中forward_list的实现——

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
  
void printMapContent1(map,
                          int>& mapOfList)
{
    cout << "   Key         Value"
         << "\n\n";
    for (auto p : mapOfList) {
        // Key is a list of integers
        forward_list ourList = p.first;
  
        // Value is an integer
        int val = p.second;
  
        // Printing list elements
        cout << "[ ";
        for (auto it = ourList.begin();
             it != ourList.end(); it++) {
            // Dereferencing value pointed by
            // iterator
            cout << (*it) << ' ';
        }
  
        cout << ']';
        cout << "     ";
  
        // Printing value
        cout << mapOfList[ourList] << '\n';
    }
}
  
void printMapContent2(map >& mapOfList)
{
  
    cout << "   Key         Value"
         << "\n\n";
    for (auto p : mapOfList) {
        // Key is an integer
        int key = p.first;
  
        // Value is a list of integers
        forward_list ourList = p.second;
  
        cout << "   ";
        cout << key << "          ";
  
        // Printing list elements
        cout << "[ ";
        for (auto it = ourList.begin();
             it != ourList.end(); it++) {
            // Dereferencing value pointed by
            // iterator
            cout << (*it) << ' ';
        }
  
        cout << ']';
        cout << "\n";
    }
}
  
// Driver code
int main()
{
    // Declaring forward list
    forward_list forwardList1;
  
    // Declaring another forward list
    forward_list forwardList2;
  
    // Assigning values using assign()
    forwardList1.assign({ 5, 3, 13 });
    forwardList2.assign({ 8, 9, 13 });
  
    map,
        int>
        mapOfList1;
  
    mapOfList1[forwardList1] = 3;
    mapOfList1[forwardList2] = 7;
  
    printMapContent1(mapOfList1);
    cout << "\n\n";
  
    map >
        mapOfList2;
  
    mapOfList2[3] = forwardList1;
    mapOfList2[7] = forwardList2;
  
    printMapContent2(mapOfList2);
    return 0;
}
输出
Key         Value

[ 5 3 13 ]     3
[ 8 9 13 ]     7


   Key         Value

   3          [ 5 3 13 ]
   7          [ 8 9 13 ]