📜  C ++中的列表数组和示例

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

C ++中的列表数组和示例

什么是数组?

任何编程语言中的数组都是一种数据结构,用于将类似数据类型的元素或数据项存储在连续的内存位置,并且可以使用数组的索引随机访问元素。当我们想要存储大量具有相似数据类型的元素时,数组是有效的。

什么是列表?

在 C++ 中,列表是允许非连续内存分配的序列容器。如果我们将向量与列表进行比较,那么与向量相比,列表的遍历速度较慢,但是一旦找到位置,插入和删除就会很快。通常,C++ 中的列表是双向链表。

与 List 一起使用的函数:

  • front() 返回列表中第一个元素的值。
  • back() 返回列表中最后一个元素的值。
  • push_front(x) 在列表的开头添加一个新元素“x”。
  • push_back(x) 在列表末尾添加一个新元素“x”。
  • pop_front() 删除列表的第一个元素,并将列表的大小减少 1。
  • pop_back() 删除列表的最后一个元素,并将列表的大小减少 1。

列表数组

C++ 允许我们创建一个列表数组。列表数组是一个数组,其中每个元素都是一个单独的列表。

句法:

列表数组

示例 1:下面是实现列表数组的 C++ 程序。

C++
// C++ program to demonstrate the
// working of array of lists in C++
#include 
using namespace std;
  
// Function to print list elements
// specified at the index, "index"
void print(list& mylist,
           int index)
{
    cout << "The list elements stored at the index " << 
             index << ": \n";
  
    // Each element of the list is a pair on 
    // its own
    for (auto element : mylist) 
    {
        // Each element of the list is a pair 
        // on its own
        cout << element << '\n';
    }
    cout << '\n';
}
  
// Function to iterate over all the array
void print(list* myContainer, int n)
{
    cout << "myContainer elements:\n\n";
  
    // Iterating over myContainer elements
    // Each element is a list on its own
    for (int i = 0; i < n; i++) 
    {
        print(myContainer[i], i);
    }
}
  
// Driver code
int main()
{
    // Declaring an array of lists
    // In list each element is of type int
    list myContainer[3];
  
    // listing values to the list stored
    // at the index 0
    // 15 <-> 5 <-> 10 <-> 20
    myContainer[0].push_front(5);
    myContainer[0].push_back(10);
    myContainer[0].push_front(15);
    myContainer[0].push_back(20);
  
    // listing values to the list stored
    // at the index 1
    // 40 <-> 30 <-> 35 <-> 45
    myContainer[1].push_front(30);
    myContainer[1].push_back(35);
    myContainer[1].push_front(40);
    myContainer[1].push_back(45);
  
    // listing values to the list stored
    // at the index 2
    // 60 <-> 50 <-> 55 <-> 65
    myContainer[2].push_front(50);
    myContainer[2].push_back(55);
    myContainer[2].push_front(60);
    myContainer[2].push_back(65);
  
    // Calling print function to iterate
    // over myContainer elements
    print(myContainer, 3);
  
    return 0;
}


C++
// C++ program to demonstrate the
// working of array of lists in C++
#include 
using namespace std;
  
// Function to print list elements
// specified at the index, "index"
void print(list& mylist,
           int index)
{
    cout << "The list elements stored at the index " << 
             index << ": \n";
  
    // Each element of the list is a pair 
    // on its own
    for (auto element : mylist) 
    {
        // Each element of the list is a pair 
        // on its own
        cout << element << '\n';
    }
    cout << '\n';
}
  
// Function to iterate over all the array
void print(list* myContainer, int n)
{
    cout << "myContainer elements:\n\n";
  
    // Iterating over myContainer elements
    // Each element is a list on its own
    for (int i = 0; i < n; i++) 
    {
        print(myContainer[i], i);
    }
}
  
// Driver code
int main()
{
    // Declaring an array of lists
    // In list each element is of type string
    list myContainer[3];
  
    // listing values to the list stored
    // at the index 0
    // "GeeksforGeeks" <-> "C++" <-> 
    // "Python" <-> "C"
    myContainer[0].push_front("C++");
    myContainer[0].push_back("Python");
    myContainer[0].push_front("GeeksforGeeks");
    myContainer[0].push_back("C");
  
    // listing values to the list stored
    // at the index 1
    // "Nainwal" <-> "Java" <-> "C#" <-> "GFG"
    myContainer[1].push_front("Java");
    myContainer[1].push_back("C#");
    myContainer[1].push_front("Nainwal");
    myContainer[1].push_back("GFG");
  
    // listing values to the list stored
    // at the index 2
    // "HTML" <-> "Swift" <-> "R" <-> "CSS"
    myContainer[2].push_front("Swift");
    myContainer[2].push_back("R");
    myContainer[2].push_front("HTML");
    myContainer[2].push_back("CSS");
  
    // Calling print function to iterate
    // over myContainer elements
    print(myContainer, 3);
  
    return 0;
}


输出

示例 2:下面是实现列表数组的 C++ 程序。

C++

// C++ program to demonstrate the
// working of array of lists in C++
#include 
using namespace std;
  
// Function to print list elements
// specified at the index, "index"
void print(list& mylist,
           int index)
{
    cout << "The list elements stored at the index " << 
             index << ": \n";
  
    // Each element of the list is a pair 
    // on its own
    for (auto element : mylist) 
    {
        // Each element of the list is a pair 
        // on its own
        cout << element << '\n';
    }
    cout << '\n';
}
  
// Function to iterate over all the array
void print(list* myContainer, int n)
{
    cout << "myContainer elements:\n\n";
  
    // Iterating over myContainer elements
    // Each element is a list on its own
    for (int i = 0; i < n; i++) 
    {
        print(myContainer[i], i);
    }
}
  
// Driver code
int main()
{
    // Declaring an array of lists
    // In list each element is of type string
    list myContainer[3];
  
    // listing values to the list stored
    // at the index 0
    // "GeeksforGeeks" <-> "C++" <-> 
    // "Python" <-> "C"
    myContainer[0].push_front("C++");
    myContainer[0].push_back("Python");
    myContainer[0].push_front("GeeksforGeeks");
    myContainer[0].push_back("C");
  
    // listing values to the list stored
    // at the index 1
    // "Nainwal" <-> "Java" <-> "C#" <-> "GFG"
    myContainer[1].push_front("Java");
    myContainer[1].push_back("C#");
    myContainer[1].push_front("Nainwal");
    myContainer[1].push_back("GFG");
  
    // listing values to the list stored
    // at the index 2
    // "HTML" <-> "Swift" <-> "R" <-> "CSS"
    myContainer[2].push_front("Swift");
    myContainer[2].push_back("R");
    myContainer[2].push_front("HTML");
    myContainer[2].push_back("CSS");
  
    // Calling print function to iterate
    // over myContainer elements
    print(myContainer, 3);
  
    return 0;
}
输出