❥ about C + + template classes vector, array VS array

Template classes vector and array are substitutes for arrays.

  1. vector
    The template class vector is similar to the string class and is a dynamic array.
    Basically, it is an alternative to using new to create dynamic arrays.
    In fact, the vector class does use new and delete to manage memory, but this work is done automatically.

vector is a sequence container that represents an array that can be resized.
Like arrays, vector s use contiguous storage locations to store their elements, which means that they can also be accessed using offsets on regular pointers to their elements, with the same efficiency as those in arrays. But unlike arrays, their size can change dynamically, and their storage is automatically processed by the container.

Efficiency: the function of vector class is relatively powerful, but the cost is a little low efficiency. If you need a fixed length array, using the array is a better choice, but the cost is not so convenient and safe.

Create a vector object named vt, which can store n_elem elements of typeName type:
vector<typeName> vt(n_elem);

#include <iostream>
#include <vector>
int main() {
    std::vector<int> myvector;
    int myint;
    std::cout << "Please enter some integers (enter 0 to end):\n";
    do {
        std::cin >> myint;
        myvector.push_back(myint);
    } while (myint);
    std::cout << "myvector stores " << myvector.size() << " numbers.\n";
    return 0;
}
Please enter some integers (enter 0 to end):
|1 2 3<Enter>
|4 5<Enter>
|60<Enter>
|0<Enter>
myvector stores 7 numbers.
  1. array(C++11)
    Efficiency: in view of the efficiency of vector, C++11 adds a template class array. Like arrays, array objects are fixed in length and use stacks (static memory allocation) instead of free storage. Therefore, they are as efficient as arrays, but more convenient and safer.

Arrays are fixed size sequence containers: they contain a specific number of elements sorted in a strictly linear sequence.
Internally, the array does not retain any data (or even its size, which is a template parameter that is fixed at compile time) except the elements it contains. In terms of storage size, it is as efficient as ordinary arrays declared using the language's bracket syntax ([]). This class adds only one layer of members and global functions to it, so arrays can be used as standard containers.

Create an array object named array, which contains n_elem elements of typeName type:
array<typeName, n_elem> arr;

Example:

#include <iostream>
#include <vector>
#include <array>
int main() {
    using namespace std;
    double a1[4] = {1.2, 2.4, 3.6, 4.8};
    vector<double> a2(4);
    // no simple way to initialize in C98
    a2[0] = 1.0 / 3.0;
    a2[1] = 1.0 / 5.0;
    a2[2] = 1.0 / 7.0;
    a2[3] = 1.0 / 9.0;
    // C++11 create and initialize array object
    array<double, 4> a3 = {3.14, 2.72, 1.62, 1.41};
    array<double, 4> a4;
    a4 = a3;// valid for array objects of same size
    cout << "a1[2]: " << a1[2] << " at " << &a1[2] << endl;
    cout << "a2[2]: " << a2[2] << " at " << &a2[2] << endl;
    cout << "a3[2]: " << a3[2] << " at " << &a3[2] << endl;
    cout << "a4[2]: " << a4[2] << " at " << &a4[2] << endl;
    return 0;
}
a1[2]: 3.6 at 0x128e9ffb70
a2[2]: 0.142857 at 0x14f3eac1b90
a3[2]: 1.62 at 0x128e9ffb30
a4[2]: 1.62 at 0x128e9ffb10

First, notice that whether it's an array, vector object, or array object, you can use standard array notation to access individual elements.
Secondly, from the address, the array object and the array are stored in the same memory area (i.e. stack), while the vector object is stored in another area (free storage area or heap).
Third, note that one array object can be assigned to another array object; For arrays, data must be copied element by element.

Misconduct:
If there is in the example code: a1[-2] = 20.2; It is allowed by the program, but the results made by different compilers are different, or even worse, which indicates that the behavior of the array is unsafe. a2[-2] = .5; a3[200] = 1.4; It is also allowed by the program. Like C language, C + + does not check for such out of bounds errors.

However, you have other options: A2 at(1) = 2.3;. The difference between the bracket notation and the member function at() is that when at() is used, illegal indexes will be caught during operation, and the program will break by default. The cost of this extra check is longer running time, which is why C + + allows you to use any representation.
In addition, these classes allow you to reduce the probability of unexpected out of bounds errors. For example, they contain member functions begin() and end(), which enable you to determine the boundary so that you don't accidentally exceed it.

Keywords: C++

Added by brandond on Mon, 28 Feb 2022 21:52:48 +0200