An array is a continuous ordered collection of the same type and fixed size. It is a contiguous block of memory

Several implementations of arrays

The original array

C++ natively provides data structures in the form of:

type name[size]
Copy the code

Type indicates the data type, name indicates the name, and size indicates the initial size

Initialization:

char name[4];
Copy the code

Initialize and assign:

char name[4] = {'A','B','C','D'};
Copy the code

Vector

A vector is an array that can be added or deleted at will. It is a dynamic array, similar to a Java ArrayList.

Use the required import header file:

#include <vector>
Copy the code

Initialization:

vector<int> test;
Copy the code

Common methods:

Pop_back removes the last data in the array. 3. At gets the numbered data. 4 Max_size specifies the maximum size of the vector 9. Capacity Specifies the current allocated size of the vector 10. Size Specifies the current used data size 11. If it is larger than the one currently in use, Erase deletes the item to which the vector points 14. Clear the current vector 15. Rbegin Returns the beginning of the vector reversed. Rend returns the end of the vector inversion pointer (begin-1). Empty Checks whether the vector is emptyCopy the code

Example:

#include <iostream> #include <vector> #include <array> using namespace std; int main(){ vector<int> test; for (size_t i = 0; i < 100; i++) { test.push_back(i); } for (size_t i = 0; i < 50; i++) { test.pop_back(); } // test[10] = 123; test[11] = 112; Test.insert (test.begin()+12,110); Erase (test.begin()+1); erase(test.begin()+1); For (size_t I = 0; i < test.size(); i++) { cout << test[i] << endl; } system("pause"); return 0; }Copy the code

Pointers and iterators in arrays:

cout << "test begin:" << *test.begin() <<endl; Cout << "test end:" << *test.end() <<endl; cout << "test front:" << test.front() <<endl; cout << "test back:" << test.back() <<endl; cout << "test &front:" << &test.front() <<endl; cout << "test &back:" << &test.back() <<endl; Vector <int>::iterator for (auto I = test.begin(); i ! = test.end(); i++) { cout<< *i <<endl; }Copy the code

Note here that end() refers to the last address of the array +1

Copy array:

// Copy the array: vector<int> test3; test3.insert(test3.end(),test.begin(),test.end()); cout <<"test3.size:" << test3.size() <<endl; // vector<int> test4(test); cout <<"test4.size:" << test4.size() <<endl;Copy the code

Swap arrays:

// Swap arrays: vector<int> test2; // swap(test2,test); Test2. swap(test); cout <<"test2.size:" << test2.size() <<endl;Copy the code

Array

Arrays, added in C++11, are more secure than arrays, but no less efficient. Array adds more manipulation functions, similar to vector, except that the array size is fixed and cannot be dynamically added or deleted. The size must be set at initialization.

Initialization:

// initialize: array<int,5> array1; array<int,5> array2{}; 5 > array < int, array3,1,2,3,4 {0}. for (size_t i = 0; i < 5; i++) { cout << array1[i] << endl; } cout<<"-----------------"<<endl; for (size_t i = 0; i < 5; i++) { cout << array2[i] << endl; } cout<<"-----------------"<<endl; for (size_t i = 0; i < 5; i++) { cout << array3[i] << endl; }Copy the code

The three initializations are declared above. If you look at the default values, the output will be:

225317808, 462-1887955703, 32758 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 0 0 0 0 0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 0 1 2 3 4Copy the code

Array of commonly used functions similar to the vector, reference: c.biancheng.net/view/6688.h…

Common operations:

    int size = array2.size();
    cout << "size:" << size << endl;

    bool empty = array1.empty();
    cout << "is empty:" << empty << endl;
Copy the code

Swap replication:

Cout << "--------- array swap --------" << endl; Array2. Swap (array3); for (auto i : array2) { cout << i << endl; } cout << "-------- array copy 1---------" << endl; // array1 = array2; for (auto i : array1) { cout << i << endl; } cout << "------- array copy 2----------" << endl; // array<int, 5> array4; copy(array2.begin(),array2.end(),array4.begin()); for (auto i : array4) { cout << i << endl; }Copy the code

The amazing journey of arrays

Array matching function

As a function argument:

Void function(int *p){} void function2(int p[10]){} void function3(int p[]){}Copy the code

As a function return value:

C++ cannot return arrays directly. We can use Pointers instead:

int* function4(int size){
    int *arr;
    arr = new int[size];
    for (size_t i = 0; i < size; i++)
    {
        arr[i] = i * 10;
    }
    return arr;
}


int main(){

        
    int *p;
    p = function4(10);

    for (size_t i = 0; i < 10; i++)
    {
        cout<< p[i] <<endl;
    }
    
    system("pause");
    return 0;
}
Copy the code

Arrays and Pointers

In C++ we can use Pointers instead of array names. Pointers refer to the address of the first element of the array, as follows:

Int arr [3] = {1, 2, 3}; int *p; p = arr; cout << "p0:" << p[0] << endl; cout << "p1:" << p[1] << endl; cout << "p2:" << p[2] << endl; // cout << "p3:" << p[3] << endl;Copy the code

The pointer p points to &arr[0]. We use p[I] directly to access the ith element of the array, but we need to be careful not to go beyond the array itself.

Pointer arrays and array Pointers

Are you a little dizzy? What the hell is this?

  • Array pointer: Pointer to an array
  • Pointer array: An array of Pointers whose objects are Pointers

Array pointer:

Int arr4 [3] = {12123123} 324; Int (*cp)[3] = &arr4; for (size_t i = 0; i < 3; i++) { cout<<"error usage "<<*cp[i] <<" success usage "<<(*cp)[i]<<endl; }Copy the code

Output result:

error usage 12  success usage 12
error usage 1  success usage 123
error usage 0  success usage 123324
Copy the code

It looks like an ordinary pointer, but we’ll see what array Pointers are for in the next multi-dimensional array.

Pointer array:

And this makes sense, too, because everything in the array is in a pointer.

A:

/ / pointer array: an array of inside every native is pointer char * tp [3] = {" asd ", "as", "f"}; for(auto i:tp){ cout<<"value:"<<i<<" address:"<<&i<<endl; } Output: value: asD address: 0x6FD53FFCF8 value:as address: 0x6FD53FFCF8 value:f address: 0x6FD53FFcf8Copy the code

Method 2:

Int * TTP [3]; Int arr4 [3] = {12123123} 324; for (size_t i = 0; i < 3; i++) { ttp[i] = &arr4[i]; cout<<"address:"<<ttp[i] << " value:"<<*ttp[i] << endl; } Output: address: 0x6FD53FFd38 value:12 address: 0x6FD53FFD3c value:123 Address: 0x6FD53FFd40 value:123324Copy the code

Multidimensional arrays and multilevel Pointers

We’ve been talking about one-dimensional arrays, we’ve been talking about multidimensional arrays, and the most common one is two-dimensional arrays.

Initialization:

int test[3][4]; An int array [3] [4] = {{1, 2, 3}, {123123234}, {12313123, 123123, 123}};Copy the code

Let’s see what array[0] looks like

cout << "array[0]:" << array[0] << endl; Output: array [1] : 0 x3529fff9a0Copy the code

It points to an array pointer

. There are multilevel Pointers, omit, do not want to write…..

Reference: www.cnblogs.com/chenyangyao…