📜  C++ STL容器的时间和空间复杂度分析

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

C++ STL容器的时间和空间复杂度分析

在本文中,我们将讨论一些 C++ STL 类的时间和空间复杂度。

C++ STL 的特点

  • 与其他编程语言相比,C++ 的执行时间较短。
  • 这使得 C++ 中的 STL 具有优势且功能强大。
  • 使 STL 强大的原因在于它包含各种各样的类,这些类是流行和标准算法的实现,以及具有使它们在进行竞争性编程或解决问题时得到很好优化的功能的预定义类。

STL中的功能分析

  • 使用 STL 时需要做的主要事情是对 STL 的分析。
  • 如果不了解问题中使用的 STL 类的复杂性分析,就无法进行问题分析。
  • 回答所提出的面试问题需要 STL 的实施和复杂性分析。

下面是一些STL Containers的分析:

优先队列

优先队列用于许多流行的算法。 Priority Queue 是Max Heap的实现 默认。 Priority Queue 甚至优化了一些主要的操作。

句法:

Min Heap也可以使用以下语法来实现。
句法:

下表包含不同函数的时间和空间复杂度:

FunctionTime ComplexitySpace Complexity
Q.top()

O(1)

O(1)

Q.push()

O(log n)

O(1)

Q.pop()

O(log n)

O(1)

Q.empty()

O(1)

O(1)

下面是说明优先级队列的 C++ 程序:

C++
// C++ program illustrating the
// priority queue
#include 
using namespace std;
 
// Function illustrating the
// priority queue
void priorityQueue()
{
    int Array[5] = { 1, 2, 3, 4, 5 };
    // Max heap
 
    int i;
    priority_queue Q;
 
    for (i = 0; i < 5; i++) {
 
        // Pushes array elements to
        // priority queue and rearranges
        // to form max heap;
        Q.push(Array[i]);
    }
 
    // Maximum element in the priority
    // queue.
    cout << "The maximum element is "
         << Q.top() << endl;
 
    i = 1;
    while (Q.empty() != 1) {
        int peek = Q.top();
 
        cout << "The " << i++
             << " th max element is "
             << peek << endl;
 
        // Pops the maximum element
        // out of priority queue
        Q.pop();
    }
 
    cout << " Is priority queue "
         << "Q empty() ?" << endl
         << "check -->" << endl;
 
    // Checks whether priority
    // queue is empty
    if (Q.empty() == 1)
        cout << "The priority queue"
             << " is empty" << endl;
    else
        cout << "The priority queue"
             << " is not empty." << endl;
}
 
// Driver Code
int main()
{
    // Function Call
    priorityQueue();
 
    return 0;
}


C++
// C++ program illustrating the map
 
#include 
using namespace std;
 
// Function illustrating the map
void Map()
{
    int i;
 
    // Declaring maps
    map M;
    unordered_map UM;
    multimap MM;
    unordered_multimap UMM;
 
    // Inserting  pairs of key
    // and value
    for (i = 101; i <= 105; i++) {
 
        // Inserted the Key and
        // value twice
        M.insert(
            pair(i - 100, i));
        UM.insert(
            pair(i - 100, i));
        M.insert(
            pair(i - 100, i));
        UM.insert(
            pair(i - 100, i));
    }
    for (i = 101; i <= 105; i++) {
 
        // Inserted the key and
        // value twice
        MM.insert(
            pair(i - 100, i));
        UMM.insert(
            pair(i - 100, i));
        MM.insert(
            pair(i - 100, i));
        UMM.insert(
            pair(i - 100, i));
    }
 
    // Iterators for accessing
    map::iterator Mitr;
    unordered_map::iterator UMitr;
    multimap::iterator MMitr;
    unordered_multimap::iterator UMMitr;
 
    // Output
    cout << "In map" << endl;
    cout << "Key"
         << " "
         << "Value" << endl;
 
    for (Mitr = M.begin();
         Mitr != M.end();
         Mitr++) {
        cout << Mitr->first << "   "
             << Mitr->second
             << endl;
    }
 
    // Unsorted and is unordered output
    cout << "In unordered_map" << endl;
    cout << "Key"
         << " "
         << "Value" << endl;
    for (UMitr = UM.begin();
         UMitr != UM.end();
         UMitr++) {
        cout << UMitr->first
             << "   "
             << UMitr->second
             << endl;
    }
 
    // Sorted output
    cout << "In multimap" << endl;
    cout << "Key"
         << " "
         << "Value" << endl;
    for (MMitr = MM.begin();
         MMitr != MM.end();
         MMitr++) {
        cout << MMitr->first
             << "   "
             << MMitr->second
             << endl;
    }
 
    // Unsorted and is unordered
    // output
    cout << "In unoredered_multimap"
         << endl;
    cout << "Key"
         << " "
         << "Value" << endl;
 
    for (UMMitr = UMM.begin();
         UMMitr != UMM.end();
         UMMitr++) {
        cout << UMMitr->first
             << "   " << UMMitr->second
             << endl;
    }
 
    cout << "The erase() function"
         << " erases respective key:"
         << endl;
    M.erase(1);
 
    cout << "Key"
         << " "
         << "Value" << endl;
 
    for (Mitr = M.begin();
         Mitr != M.end(); Mitr++) {
        cout << Mitr->first
             << "   " << Mitr->second
             << endl;
    }
 
    cout << "The find() function"
         << " finds the respective key:"
         << endl;
    if (M.find(1) != M.end()) {
        cout << "Found!" << endl;
    }
    else {
        cout << "Not Found!" << endl;
    }
 
    cout << "The clear() function "
         << "clears the map:" << endl;
    M.clear();
 
    // Returns the size of the map
    cout << "Now the size is :"
         << M.size();
}
 
// Driver Code
int main()
{
    // Function Call
    Map();
 
    return 0;
}


C++
// C++ program illustrating the set
#include 
using namespace std;
 
// Function illustrating the set
void Set()
{
    // Set declaration
    set s;
    unordered_set us;
    multiset ms;
    unordered_multiset ums;
    int i;
 
    for (i = 1; i <= 5; i++) {
 
        // Inserting elements
        s.insert(2 * i + 1);
        us.insert(2 * i + 1);
        ms.insert(2 * i + 1);
        ums.insert(2 * i + 1);
        s.insert(2 * i + 1);
        us.insert(2 * i + 1);
        ms.insert(2 * i + 1);
        ums.insert(2 * i + 1);
    }
 
    // Iterator to access values
    // in set
    set::iterator sitr;
    unordered_set::iterator uitr;
    multiset::iterator mitr;
    unordered_multiset::iterator umitr;
 
    cout << "The difference: "
         << endl;
    cout << "The output for set "
         << endl;
 
    for (sitr = s.begin();
         sitr != s.end(); sitr++) {
        cout << *sitr << " ";
    }
    cout << endl;
 
    cout << "The output for "
         << "unordered set " << endl;
 
    for (uitr = us.begin();
         uitr != us.end(); uitr++) {
        cout << *uitr << " ";
    }
    cout << endl;
 
    cout << "The output for "
         << "multiset " << endl;
 
    for (mitr = ms.begin();
         mitr != ms.end();
         mitr++) {
        cout << *mitr << " ";
    }
    cout << endl;
 
    cout << "The output for "
         << "unordered multiset "
         << endl;
 
    for (umitr = ums.begin();
         umitr != ums.end();
         umitr++) {
        cout << *umitr << " ";
    }
    cout << endl;
}
 
// Driver Code
int main()
{
    // Function Call
    Set();
 
    return 0;
}


C++
// C++ program illustrating the stack
#include 
using namespace std;
 
// Function illustrating stack
void Stack()
{
    stack s;
    int i;
    for (i = 0; i <= 5; i++) {
        cout << "The pushed element"
             << " is " << i << endl;
        s.push(i);
    }
 
    // Points to top element of stack
    cout << "The top element of the"
         << " stack is: " << s.top()
         << endl;
 
    // Return size of stack
    cout << "The size of the stack"
         << " is: " << s.size()
         << endl;
 
    // Pops the elements of the
    // stack in the LIFO manner
    // Checks whether the stack
    // is empty or not
    while (s.empty() != 1) {
        cout << "The popped element"
             << " is " << s.top()
             << endl;
        s.pop();
    }
}
 
// Driver Code
int main()
{
    // Function Call
    Stack();
 
    return 0;
}


C++
// C++ program illustrating the queue
#include 
using namespace std;
 
// Function illustrating queue
void Queue()
{
    queue q;
    int i;
    for (i = 101; i <= 105; i++) {
 
        // Inserts into the queue
        // in the FIFO manner
        q.push(i);
 
        cout << "The first and last"
             << " elements of the queue "
             << "are " << q.front()
             << " " << q.back()
             << endl;
    }
 
    // Check whether the queue is
    // empty or not
    while (q.empty() != 1) {
 
        // Pops the first element
        // of the queue
        cout << "The Element popped"
             << " following FIFO is "
             << q.front() << endl;
        q.pop();
    }
}
 
// Driver Code
int main()
{
    // Function Call
    Queue();
 
    return 0;
}


C++
// C++ program illustrating vector
 
#include 
using namespace std;
 
// Function displaying values
void display(vector v)
{
    for (int i = 0;
         i < v.size(); i++) {
        cout << v[i] << " ";
    }
}
 
// Function illustrating vector
void Vector()
{
    int i;
    vector v;
    for (i = 100; i < 106; i++) {
 
        // Inserts an element in vector
        v.push_back(i);
    }
 
    cout << "The vector after "
         << "push_back is :" << v.size()
         << endl;
    cout << "The vector now is :";
    display(v);
    cout << endl;
 
    // Deletes an element at the back
    v.pop_back();
    cout << "The vector after "
         << "pop_back is :" << v.size()
         << endl;
    cout << "The vector now is :";
    display(v);
    cout << endl;
 
    // Reverses the vector
    reverse(v.begin(), v.end());
    cout << "The vector after "
         << "reversing is :" << v.size()
         << endl;
 
    cout << "The vector now is :";
    display(v);
    cout << endl;
 
    // Sorts vector using Quick Sort
    sort(v.begin(), v.end());
    cout << "The vector after "
         << "sorting is :" << v.size()
         << endl;
 
    cout << "The vector now is :";
    display(v);
    cout << endl;
 
    // Erases ith position element
    v.erase(v.begin() + 2);
 
    cout << "The size of vector "
         << "after erasing at position "
            "3 is :"
         << v.size() << endl;
    cout << "The vector now is :";
    display(v);
    cout << endl;
 
    // Deletes the vector completely
    v.clear();
 
    cout << "The size of the vector"
         << " after clearing is :"
         << v.size() << endl;
    cout << "The vector now is :";
    display(v);
    cout << endl;
}
 
// Driver Code
int main()
{
    // Function Call
    Vector();
 
    return 0;
}


输出:
The maximum element is 5
The 1 th max element is 5
The 2 th max element is 4
The 3 th max element is 3
The 4 th max element is 2
The 5 th max element is 1
 Is priority queue Q empty() ?
check -->
The priority queue is empty

地图

它是著名的 STL 类,它以键值对的模式存储值。

  • 它使用键值映射值,并且没有相同的键将具有不同的值。
  • 可以将其修改为 multimap 以使其适用于具有不同值的相同键。
  • 该映射甚至可以用于不同数据类型的键和值。

句法:

  • map M是自平衡红黑树的实现
  • unordered_map MHash Table的实现,它使得
    Theta(1)的插入、删除和搜索等操作的复杂性。
  • multimap M红黑树的实现,它是自平衡树,使得操作成本与地图相同。
  • unordered_multimap M的实现与无序映射的实现相同,即哈希表。
  • 唯一的区别是它跟踪另一个变量,该变量跟踪出现次数。
  • 这些对使用pair (x, y)插入到映射中,并且可以使用映射iterator.first和映射iterator.second访问。
  • 默认情况下,地图会根据键进行排序,在无序地图的情况下,它可以是任何顺序。

下表包含不同函数的时间和空间复杂度(n 是地图的大小):

FunctionTime ComplexitySpace Complexity
M.find(x)

O(log n)

O(1)

M.insert(pair (x, y)

O(log n)

O(1)

M.erase(x)

O(log n)

O(1)

M.empty( )

O(1)

O(1)

M.clear( )

Theta(n)

O(1)

M.size( )

O(1)

O(1)

下面是说明地图的 C++ 程序:

C++

// C++ program illustrating the map
 
#include 
using namespace std;
 
// Function illustrating the map
void Map()
{
    int i;
 
    // Declaring maps
    map M;
    unordered_map UM;
    multimap MM;
    unordered_multimap UMM;
 
    // Inserting  pairs of key
    // and value
    for (i = 101; i <= 105; i++) {
 
        // Inserted the Key and
        // value twice
        M.insert(
            pair(i - 100, i));
        UM.insert(
            pair(i - 100, i));
        M.insert(
            pair(i - 100, i));
        UM.insert(
            pair(i - 100, i));
    }
    for (i = 101; i <= 105; i++) {
 
        // Inserted the key and
        // value twice
        MM.insert(
            pair(i - 100, i));
        UMM.insert(
            pair(i - 100, i));
        MM.insert(
            pair(i - 100, i));
        UMM.insert(
            pair(i - 100, i));
    }
 
    // Iterators for accessing
    map::iterator Mitr;
    unordered_map::iterator UMitr;
    multimap::iterator MMitr;
    unordered_multimap::iterator UMMitr;
 
    // Output
    cout << "In map" << endl;
    cout << "Key"
         << " "
         << "Value" << endl;
 
    for (Mitr = M.begin();
         Mitr != M.end();
         Mitr++) {
        cout << Mitr->first << "   "
             << Mitr->second
             << endl;
    }
 
    // Unsorted and is unordered output
    cout << "In unordered_map" << endl;
    cout << "Key"
         << " "
         << "Value" << endl;
    for (UMitr = UM.begin();
         UMitr != UM.end();
         UMitr++) {
        cout << UMitr->first
             << "   "
             << UMitr->second
             << endl;
    }
 
    // Sorted output
    cout << "In multimap" << endl;
    cout << "Key"
         << " "
         << "Value" << endl;
    for (MMitr = MM.begin();
         MMitr != MM.end();
         MMitr++) {
        cout << MMitr->first
             << "   "
             << MMitr->second
             << endl;
    }
 
    // Unsorted and is unordered
    // output
    cout << "In unoredered_multimap"
         << endl;
    cout << "Key"
         << " "
         << "Value" << endl;
 
    for (UMMitr = UMM.begin();
         UMMitr != UMM.end();
         UMMitr++) {
        cout << UMMitr->first
             << "   " << UMMitr->second
             << endl;
    }
 
    cout << "The erase() function"
         << " erases respective key:"
         << endl;
    M.erase(1);
 
    cout << "Key"
         << " "
         << "Value" << endl;
 
    for (Mitr = M.begin();
         Mitr != M.end(); Mitr++) {
        cout << Mitr->first
             << "   " << Mitr->second
             << endl;
    }
 
    cout << "The find() function"
         << " finds the respective key:"
         << endl;
    if (M.find(1) != M.end()) {
        cout << "Found!" << endl;
    }
    else {
        cout << "Not Found!" << endl;
    }
 
    cout << "The clear() function "
         << "clears the map:" << endl;
    M.clear();
 
    // Returns the size of the map
    cout << "Now the size is :"
         << M.size();
}
 
// Driver Code
int main()
{
    // Function Call
    Map();
 
    return 0;
}
输出:
In map
Key Value
1   101
2   102
3   103
4   104
5   105
In unordered_map
Key Value
5   105
4   104
3   103
1   101
2   102
In multimap
Key Value
1   101
1   101
2   102
2   102
3   103
3   103
4   104
4   104
5   105
5   105
In unoredered_multimap
Key Value
5   105
5   105
4   104
4   104
1   101
1   101
2   102
2   102
3   103
3   103
The erase() function erases respective key:
Key Value
2   102
3   103
4   104
5   105
The find() function finds the respective key:
Not Found!
The clear() function clears the map:
Now the size is :0

解释:

  • m.begin():将迭代器指向起始元素。
  • m.end():将迭代器指向最后一个元素之后的元素,这是理论上的。

设置

  • 集合的第一个有用属性是它只包含不同的元素,当然变体多重集合甚至可以包含重复的元素。
  • Set 以有序的方式包含不同的元素,而无序的 set 以无序的方式包含不同的元素,而 multimap 包含重复的元素。

句法:

  • Set ( set s ) 是 二叉搜索树
  • 无序集( unordered_set S)Hash Table的实现。
  • Multiset ( multiset S ) 是红黑树的实现。
  • Unordered_multiset( unordered_multiset S ) 的实现方式与无序集相同,但使用了一个额外的变量来跟踪计数。
  • 当使用 unordered 时,复杂性变为Theta(1)O(n) ,由于Hash Table实现,访问变得更容易。

下表包含不同函数的时间和空间复杂度(n 是集合的大小):

FunctionTime ComplexitySpace Complexity
s.find( )

O(log n)

O(1)

s.insert(x)

O(log n)

O(1)

s.erase(x)

O(log n)

O(1)

s.size()

O(1)

O(1)

s.empty( )

O(1)

O(1)

下面是说明集合的 C++ 程序:

C++

// C++ program illustrating the set
#include 
using namespace std;
 
// Function illustrating the set
void Set()
{
    // Set declaration
    set s;
    unordered_set us;
    multiset ms;
    unordered_multiset ums;
    int i;
 
    for (i = 1; i <= 5; i++) {
 
        // Inserting elements
        s.insert(2 * i + 1);
        us.insert(2 * i + 1);
        ms.insert(2 * i + 1);
        ums.insert(2 * i + 1);
        s.insert(2 * i + 1);
        us.insert(2 * i + 1);
        ms.insert(2 * i + 1);
        ums.insert(2 * i + 1);
    }
 
    // Iterator to access values
    // in set
    set::iterator sitr;
    unordered_set::iterator uitr;
    multiset::iterator mitr;
    unordered_multiset::iterator umitr;
 
    cout << "The difference: "
         << endl;
    cout << "The output for set "
         << endl;
 
    for (sitr = s.begin();
         sitr != s.end(); sitr++) {
        cout << *sitr << " ";
    }
    cout << endl;
 
    cout << "The output for "
         << "unordered set " << endl;
 
    for (uitr = us.begin();
         uitr != us.end(); uitr++) {
        cout << *uitr << " ";
    }
    cout << endl;
 
    cout << "The output for "
         << "multiset " << endl;
 
    for (mitr = ms.begin();
         mitr != ms.end();
         mitr++) {
        cout << *mitr << " ";
    }
    cout << endl;
 
    cout << "The output for "
         << "unordered multiset "
         << endl;
 
    for (umitr = ums.begin();
         umitr != ums.end();
         umitr++) {
        cout << *umitr << " ";
    }
    cout << endl;
}
 
// Driver Code
int main()
{
    // Function Call
    Set();
 
    return 0;
}
输出:
The difference: 
The output for set 
3 5 7 9 11 
The output for unordered set 
11 9 7 3 5 
The output for multiset 
3 3 5 5 7 7 9 9 11 11 
The output for unordered multiset 
11 11 9 9 3 3 5 5 7 7

堆栈

它是一种遵循后进先出(LIFO)规则的数据结构,这类STL也是
在许多算法的实现过程中使用。
例如,许多递归解决方案使用系统堆栈来回溯递归函数的挂起调用,同样可以使用 STL 堆栈迭代地实现。

句法:

  • 它是使用堆栈的链表实现来实现的。
FunctionTime ComplexitySpace Complexity
s.top( )

O(1)

O(1)

s.pop( )

O(1)

O(1)

s.empty( ) 

O(1)

O(1)

s.push(x )

O(1)

O(1)

下面是说明堆栈的 C++ 程序:

C++

// C++ program illustrating the stack
#include 
using namespace std;
 
// Function illustrating stack
void Stack()
{
    stack s;
    int i;
    for (i = 0; i <= 5; i++) {
        cout << "The pushed element"
             << " is " << i << endl;
        s.push(i);
    }
 
    // Points to top element of stack
    cout << "The top element of the"
         << " stack is: " << s.top()
         << endl;
 
    // Return size of stack
    cout << "The size of the stack"
         << " is: " << s.size()
         << endl;
 
    // Pops the elements of the
    // stack in the LIFO manner
    // Checks whether the stack
    // is empty or not
    while (s.empty() != 1) {
        cout << "The popped element"
             << " is " << s.top()
             << endl;
        s.pop();
    }
}
 
// Driver Code
int main()
{
    // Function Call
    Stack();
 
    return 0;
}
输出:
The pushed element is 0
The pushed element is 1
The pushed element is 2
The pushed element is 3
The pushed element is 4
The pushed element is 5
The top element of the stack is: 5
The size of the stack is: 6
The popped element is 5
The popped element is 4
The popped element is 3
The popped element is 2
The popped element is 1
The popped element is 0

队列

它是一个遵循的数据结构 先进先出 (FIFO)规则。

  • 在代码中包含队列 STL 类队列减少了对基本操作的函数调用。
  • 队列通常用于树和图的 BFS 遍历以及许多流行的算法中。  
  • STL 中的队列是使用链表实现的。  

句法:

包含不同函数的时间和空间复杂度的表格如下:

FunctionTime ComplexitySpace Complexity
q.push(x)

O(1)

O(1)

q.pop( )

O(1)

O(1)

q.front( )

O(1)

O(1)

q.back( )

O(1)

O(1)

q.empty( )

O(1)

O(1)

q.size( )

O(1)

O(1)

下面是说明队列的 C++ 程序:

C++

// C++ program illustrating the queue
#include 
using namespace std;
 
// Function illustrating queue
void Queue()
{
    queue q;
    int i;
    for (i = 101; i <= 105; i++) {
 
        // Inserts into the queue
        // in the FIFO manner
        q.push(i);
 
        cout << "The first and last"
             << " elements of the queue "
             << "are " << q.front()
             << " " << q.back()
             << endl;
    }
 
    // Check whether the queue is
    // empty or not
    while (q.empty() != 1) {
 
        // Pops the first element
        // of the queue
        cout << "The Element popped"
             << " following FIFO is "
             << q.front() << endl;
        q.pop();
    }
}
 
// Driver Code
int main()
{
    // Function Call
    Queue();
 
    return 0;
}
输出:
The first and last elements of the queue are 101 101
The first and last elements of the queue are 101 102
The first and last elements of the queue are 101 103
The first and last elements of the queue are 101 104
The first and last elements of the queue are 101 105
The Element popped following FIFO is 101
The Element popped following FIFO is 102
The Element popped following FIFO is 103
The Element popped following FIFO is 104
The Element popped following FIFO is 105

向量

Vector是动态数组的实现,使用新的 用于堆中的内存分配。
句法:

二维向量也可以使用以下语法实现:

句法:

下表包含不同函数的时间和空间复杂度:

FunctionTime ComplexitySpace Complexity
sort(v.begin( ), v.end( ))Theta(nlog(n))

Theta(log n)

reverse(v.begin( ), v.end( ))

 O(n)

O(1)

v.push_back(x)

O(1)

O(1)

v.pop_back(x)

O(1)

O(1)

v.size()

O(1)

O(1)

v.clear()

 O(n)

O(1)

v.erase()

O(n)

O(1)

下面是说明矢量的 C++ 程序:

C++

// C++ program illustrating vector
 
#include 
using namespace std;
 
// Function displaying values
void display(vector v)
{
    for (int i = 0;
         i < v.size(); i++) {
        cout << v[i] << " ";
    }
}
 
// Function illustrating vector
void Vector()
{
    int i;
    vector v;
    for (i = 100; i < 106; i++) {
 
        // Inserts an element in vector
        v.push_back(i);
    }
 
    cout << "The vector after "
         << "push_back is :" << v.size()
         << endl;
    cout << "The vector now is :";
    display(v);
    cout << endl;
 
    // Deletes an element at the back
    v.pop_back();
    cout << "The vector after "
         << "pop_back is :" << v.size()
         << endl;
    cout << "The vector now is :";
    display(v);
    cout << endl;
 
    // Reverses the vector
    reverse(v.begin(), v.end());
    cout << "The vector after "
         << "reversing is :" << v.size()
         << endl;
 
    cout << "The vector now is :";
    display(v);
    cout << endl;
 
    // Sorts vector using Quick Sort
    sort(v.begin(), v.end());
    cout << "The vector after "
         << "sorting is :" << v.size()
         << endl;
 
    cout << "The vector now is :";
    display(v);
    cout << endl;
 
    // Erases ith position element
    v.erase(v.begin() + 2);
 
    cout << "The size of vector "
         << "after erasing at position "
            "3 is :"
         << v.size() << endl;
    cout << "The vector now is :";
    display(v);
    cout << endl;
 
    // Deletes the vector completely
    v.clear();
 
    cout << "The size of the vector"
         << " after clearing is :"
         << v.size() << endl;
    cout << "The vector now is :";
    display(v);
    cout << endl;
}
 
// Driver Code
int main()
{
    // Function Call
    Vector();
 
    return 0;
}


输出:
The vector after push_back is :6
The vector now is :100 101 102 103 104 105 
The vector after pop_back is :5
The vector now is :100 101 102 103 104 
The vector after reversing is :5
The vector now is :104 103 102 101 100 
The vector after sorting is :5
The vector now is :100 101 102 103 104 
The size of vector after erasing at position 3 is :4
The vector now is :100 101 103 104 
The size of the vector after clearing is :0
The vector now is :