📜  C++中向量优于数组的优点

📅  最后修改于: 2021-09-08 15:09:58             🧑  作者: Mango

我们已经讨论过数组和向量。在这篇文章中,我们将讨论向量相对于普通数组的优势。

Vector 优于数组的优点

  1. Vector 是模板类并且C++ 构造,而数组是内置语言构造并且存在于 C 和 C++ 中。
  2. 向量实现为具有列表接口的动态数组而数组可以静态或动态地实现为具有原始数据类型接口。
    #include 
    using namespace std;
      
    int main()
    {
        int array[100]; // Static Implementation
        int* arr = new int[100]; // Dynamic Implementation
        vector v; // Vector's Implementation
        return 0;
    }
    
  3. 数组的大小是固定的,向量是可调整大小的,即它们可以随着向量在堆内存上分配而增长和收缩。
    #include 
    using namespace std;
      
    int main()
    {
        int array[100]; // Static Implementation
      
        cout << "Size of Array " << sizeof(array) / sizeof(array[0]) << "\n";
      
        vector v; // Vector's Implementation
      
        // Inserting Values in Vector
        v.push_back(1);
        v.push_back(2);
        v.push_back(3);
        v.push_back(4);
        v.push_back(5);
      
        cout << "Size of vector Before Removal=" << v.size() << "\n";
      
        // Output Values of vector
        for (auto it : v)
            cout << it << " ";
      
        v.erase(v.begin() + 2); // Remove 3rd element
      
        cout << "\nSize of vector After removal=" << v.size() << "\n";
      
        // Output Values of vector
        for (auto it : v)
            cout << it << " ";
      
        return 0;
    }
    

    输出

    Size of Array 100
    Size of vector Before Removal=5
    1 2 3 4 5 
    Size of vector After removal=4
    1 2 4 5
    
  4. 如果数组动态定义的,则必须显式释放,而向量会自动从堆内存中释放
    #include 
    using namespace std;
      
    int main()
    {
        int* arr = new int[100]; // Dynamic Implementation
        delete[] arr; // array Explicitly deallocated
      
        vector v; // Automatic deallocation when variable goes out of scope
        return 0;
    }
    
  5. 如果动态分配,无法确定数组的大小,而可以在O(1) 时间内确定向量的大小。
  6. 当数组传递给函数,大小单独参数也会传递,而在将向量传递给函数的情况下,没有这样的需要,因为向量维护始终跟踪容器大小的变量
    #include 
    using namespace std;
      
    int main()
    {
        int* arr = new int[100]; // Dynamic Implementation
      
        cout << "Size of array= ";
        cout << sizeof(arr) / sizeof(*arr) << "\n"; // Pointer cannot be used to get size of
        // block pointed by it
        return 0;
    }
    

    输出

    Size of array= 2
    
  7. 当数组变满并插入新元素时;没有重新分配是隐式完成的,而当向量变得大于其容量时,重新分配是隐式完成的。
  8. 除非从函数动态分配数组,否则无法返回数组,可以从函数返回向量
    // Program to demonstrate arrays cannot be returned
    #include 
    using namespace std;
      
    int* getValues()
    {
      
        int arr[10]; // Array defined locally
        for (int i = 0; i < 10; i++) // Putting Values in array
            arr[i] = i + 1;
      
        return arr; // returning pointer to array
    }
      
    // main function
    int main()
    {
      
        int* array; // pointer of int type
      
        array = getValues(); // Call function to get arr
      
        for (int i = 0; i < 10; i++) { // Printing Values
            cout << "*(array + " << i << ") : ";
            cout << *(array + i) << endl;
        }
      
        return 0;
    }
    

    输出

    warning: address of local variable 'arr' returned [-Wreturn-local-addr]
    Segmentation Fault (SIGSEGV)
    
    // Program to demonstrate vector can be returned
    #include 
    using namespace std;
      
    // Function returning vector
    vector getValues()
    {
      
        vector v; // Vector defined locally
        for (int i = 0; i < 10; i++) // Inserting values in Vector
            v.push_back(i + 1);
      
        return v; // returning pointer to array
    }
      
    // main function
    int main()
    {
      
        vector get;
      
        get = getValues(); // Call function to get v
      
        // Output Values of vector
        for (auto it : get)
            cout << it << " ";
      
        return 0;
    }
    

    输出

    1 2 3 4 5 6 7 8 9 10 
    
  9. 数组不能直接复制或分配,而向量可以直接复制或分配。
    #include 
    using namespace std;
      
    // main function
    int main()
    {
        vector v; // Vector defined locally
        for (int i = 0; i < 10; i++)
            v.push_back(i + 1);
      
        vector get;
      
        get = v; // Copying vector v into vector get
      
        cout << "vector get:\n";
        for (auto it : get)
            cout << it << " ";
      
        int arr[10];
        for (int i = 0; i < 10; i++) // Putting Values in array
            arr[i] = i + 1;
      
        int copyArr[10];
      
        copyArr = arr; // Error
      
        return 0;
    }
    

    输出

    vector get:
    1 2 3 4 5 6 7 8 9 10
    
    error: invalid array assignment
        copyArr=arr;
    

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程