📜  C++ STL 中的向量列表及示例

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

C++ STL 中的向量列表及示例

列表

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

与 List 一起使用的函数:

  • push_front(x):在列表的开头添加一个新元素“x”。
  • push_back(x):在列表末尾添加一个新元素“x”。

转发列表

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

与转发列表一起使用的函数:

  • push_front(x): – 在列表的开头添加一个新元素“x”。

矢量图

向量与动态数组相同,能够在插入或删除元素时自动调整自身大小,其存储由容器自动处理。

与向量一起使用的函数:

  • size():返回向量中的元素个数。
  • push_back():该函数用于将元素从后面推入向量中。

在设计复杂的数据结构时,向量列表可能非常有用。每个节点可以保存一个向量。

下面是向量列表的实现:

C++
// C++ program to implement
// the above approach
#include 
using namespace std;
  
// Function to print list
// contents
void print(list >& listOfVectors)
{
    for (auto vect : listOfVectors) {
        // Each element of the list is
        // a vector itself
        vector currentVector = vect;
  
        cout << "[ ";
  
        // Printing vector contents
        for (auto element : currentVector)
            cout << element << ' ';
  
        cout << ']';
        cout << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a list of vectors
    list > listOfVectors;
  
    // Declaring a vector
    vector vector1;
  
    // Adding elements into the
    // vector
    vector1.push_back(10);
    vector1.push_back(14);
    vector1.push_back(17);
  
    // Push the vector at the back
    // in the list
    listOfVectors.push_back(vector1);
  
    // Declaring another vector
    vector vector2;
  
    // Adding elements in the vector
    vector2.push_back(2);
    vector2.push_back(6);
    vector2.push_back(11);
  
    // Push the vector at the back
    // in the list
    listOfVectors.push_back(vector2);
  
    // Declaring another vector
    vector vector3;
  
    // Adding elements in the
    // vector
    vector3.push_back(11);
    vector3.push_back(16);
    vector3.push_back(29);
  
    // Push the vector at the front
    // in the list
    listOfVectors.push_front(vector3);
  
    // Calling print function
    print(listOfVectors);
    return 0;
}


C++
// C++ program to implement
// the above approach
#include 
using namespace std;
  
// Function to print forward
// list contents
void print(forward_list >& forwardListOfVectors)
{
    for (auto vect : forwardListOfVectors) {
        // Each element of the forward list
        // is a vector itself
        vector currentVector = vect;
  
        cout << "[ ";
  
        // Printing vector contents
        for (auto element : currentVector)
            cout << element << ' ';
  
        cout << ']';
        cout << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a list of vectors
    forward_list > forwardListOfVectors;
  
    // Declaring a vector
    vector vector1;
  
    // Adding elements into the vector
    vector1.push_back(10);
    vector1.push_back(14);
    vector1.push_back(17);
  
    // Push the vector in the forward
    // list
    forwardListOfVectors.push_front(vector1);
  
    // Declaring another vector
    vector vector2;
  
    // Adding elements in the vector
    vector2.push_back(2);
    vector2.push_back(6);
    vector2.push_back(11);
  
    // Push the vector in the forward
    // list
    forwardListOfVectors.push_front(vector2);
  
    // Declaring another vector
    vector vector3;
  
    // Adding elements in the vector
    vector3.push_back(11);
    vector3.push_back(16);
    vector3.push_back(29);
  
    // Push the vector in the forward
    // list
    forwardListOfVectors.push_front(vector3);
  
    // Calling print function
    print(forwardListOfVectors);
  
    return 0;
}


输出
[ 10 14 17 ]
[ 2 6 11 ]
[ 11 16 29 ]

下面是向量前向列表的实现:

C++

// C++ program to implement
// the above approach
#include 
using namespace std;
  
// Function to print forward
// list contents
void print(forward_list >& forwardListOfVectors)
{
    for (auto vect : forwardListOfVectors) {
        // Each element of the forward list
        // is a vector itself
        vector currentVector = vect;
  
        cout << "[ ";
  
        // Printing vector contents
        for (auto element : currentVector)
            cout << element << ' ';
  
        cout << ']';
        cout << '\n';
    }
}
  
// Driver code
int main()
{
    // Declaring a list of vectors
    forward_list > forwardListOfVectors;
  
    // Declaring a vector
    vector vector1;
  
    // Adding elements into the vector
    vector1.push_back(10);
    vector1.push_back(14);
    vector1.push_back(17);
  
    // Push the vector in the forward
    // list
    forwardListOfVectors.push_front(vector1);
  
    // Declaring another vector
    vector vector2;
  
    // Adding elements in the vector
    vector2.push_back(2);
    vector2.push_back(6);
    vector2.push_back(11);
  
    // Push the vector in the forward
    // list
    forwardListOfVectors.push_front(vector2);
  
    // Declaring another vector
    vector vector3;
  
    // Adding elements in the vector
    vector3.push_back(11);
    vector3.push_back(16);
    vector3.push_back(29);
  
    // Push the vector in the forward
    // list
    forwardListOfVectors.push_front(vector3);
  
    // Calling print function
    print(forwardListOfVectors);
  
    return 0;
}
输出
[ 11 16 29 ]
[ 2 6 11 ]
[ 10 14 17 ]