Summary of common methods of C++ vector

----------------

user: Summarize the usage of vector in C++

----------------

assistant: In C++, vector is a template class used to define dynamic arrays (also known as variable-size arrays). When using the vector type, you can specify its template parameter to define the type of elements in the vector. Here are some common uses of the vector class: Creating a vector object: You can create an empty vector using the default constructor, or create a vector with an initial value using the constructor with the number of elements and the value of the elements, as follows:

vector<int> myVec; // 创建一个空向量
vector<int> myVec2(5, 0); // 创建一个包含5个元素,每个元素的值为0的向量

Accessing elements in a vector: You can access elements in a vector using the subscript operator [] or the at() function as follows:

myVec[0] = 10; // 将第一个元素的值设置为10
myVec.at(1) = 20; // 将第二个元素的值设置为20
int val = myVec[2]; // 获取第三个元素的值

Adding elements at the end of the vector: You can use the push_back() function to add elements at the end of the vector as follows:

myVec.push_back(30); // 在末尾添加一个值为30的元素

Get the size of the vector: You can use the size() function to get the number of elements in the vector, as follows:

int count = myVec.size(); // 获取向量中元素的数量

Iterating over the elements in a vector: You can use a for loop or an iterator to iterate over the elements in a vector, as follows:

// 使用 for 循环遍历 vector
for (int i = 0; i < myVec.size(); i++) {
    cout << myVec[i] << " ";
}

// 使用迭代器遍历 vector
for (vector<int>::iterator it = myVec.begin(); it != myVec.end(); it++) {
    cout << *it << " ";
}


Empty the elements in the vector: You can use the clear() function to delete all the elements in the vector, as follows:

myVec.clear(); // 删除 vector 中的所有元素

These are some common uses of the vector class. Using vector, you can easily manipulate dynamic arrays without manually managing memory.

------------------------------------------------------------------------------------

user: How to use vector to create a two-dimensional array of integers

-------------------------------------------------------------------------------------

#include <vector>
using namespace std;

int main() {
    // 创建一个 3 行 4 列的二维数组
    vector<vector<int> > myVec(3, vector<int>(4, 0));
    
    // 对二维数组进行赋值
    myVec[0][0] = 1;
    myVec[1][2] = 2;
    myVec[2][1] = 3;
    
    // 输出二维数组
    for (int i = 0; i < myVec.size(); i++) {
        for (int j = 0; j < myVec[i].size(); j++) {
            cout << myVec[i][j] << " ";
        }
        cout << endl;
    }
    
    return 0;
}

In the above code, we first create a two-dimensional array with a size of 3 rows and 4 columns myVec, and initialize all its elements to 0. We then use the subscript operator []to access myVecthe elements in and assign values ​​to them. Finally, use a double loop to traverse the entire two-dimensional array and output all its elements.

It is worth noting that vectornesting of types allows the flexibility to create arrays of arbitrary dimensions. For example, we can create a three-dimensional array, the method is similar to the above two-dimensional array, just need to vectornest another in the nested vector.

Guess you like

Origin blog.csdn.net/weixin_43623488/article/details/129792247