说明:这是c++面向对象oop考试前整理的,由于我们是全英文试卷,所以做了中英文版本,顺便复习下一些编程上的英语名词。
中文版本(Chinese version)
总的来看
push_back():
myVector.push_back(42);
: 在向量末尾添加元素。
insert():
myVector.insert(myVector.begin() + 1, 55);
: 在特定位置插入元素。
front():
int firstElement = myVector.front();
: 获取第一个元素。
back():
int lastElement = myVector.back();
: 获取最后一个元素。
Iteration (for loop):
for (int i : myVector) { /* 循环内的代码 */ }
: 遍历向量元素。
size():
size_t size = myVector.size();
: 获取向量中的元素数量。
capacity():
size_t capacity = myVector.capacity();
: 获取向量的容量。
pop_back():
myVector.pop_back();
: 移除向量中的最后一个元素。
erase():
myVector.erase(myVector.begin() + 1);
: 移除向量中特定位置的元素。
clear():
myVector.clear();
: 移除向量中的所有元素。
具体样例
声明和初始化:
// 声明:
std::vector<int> myVector;
// 初始化:
std::vector<int> anotherVector = {1, 2, 3};
添加元素:
// 在末尾添加元素
myVector.push_back(42);
// 在特定位置插入元素
myVector.insert(myVector.begin() + 1, 55);
访问元素:
// 通过索引访问
int value = myVector[0];
// 访问第一个元素
int firstElement = myVector.front();
// 访问最后一个元素
int lastElement = myVector.back();
遍历向量:
// 遍历向量元素
for (int i : myVector) {
// 循环内的代码
}
大小和容量:
// 获取元素数量
size_t size = myVector.size();
// 获取向量的容量
size_t capacity = myVector.capacity();
移除元素:
// 移除最后一个元素
myVector.pop_back();
// 移除特定位置的元素
myVector.erase(myVector.begin() + 1);
清空向量:
// 移除所有元素
myVector.clear();
English version(英文版本)
Overrall
- Declaration and Initialization:
- Declaration:
std::vector<int> myVector;
- Initialization:
std::vector<int> anotherVector = {1, 2, 3};
- Adding Elements:
myVector.push_back(42);
// Add element at the endmyVector.insert(myVector.begin() + 1, 55);
// Insert at a specific position
- Accessing Elements:
int value = myVector[0];
// Access by indexint firstElement = myVector.front();
// Access the first elementint lastElement = myVector.back();
// Access the last element
- Iterating Through Vector:
for (int i : myVector) { /* Loop through vector elements */ }
- Size and Capacity:
size_t size = myVector.size();
// Get the number of elementssize_t capacity = myVector.capacity();
// Get the vector’s capacity
- Removing Elements:
myVector.pop_back();
// Remove the last elementmyVector.erase(myVector.begin() + 1);
// Remove element at a specific position
- Clearing Vector:
myVector.clear();
// Remove all elements
Details
Declaration and Initialization:
// Declaration:
std::vector<int> myVector;
// Initialization:
std::vector<int> anotherVector = {1, 2, 3};
Adding Elements:
// Add element at the end
myVector.push_back(42);
// Insert at a specific position
myVector.insert(myVector.begin() + 1, 55);
Accessing Elements:
// Access by index
int value = myVector[0];
// Access the first element
int firstElement = myVector.front();
// Access the last element
int lastElement = myVector.back();
Iterating Through Vector:
// Loop through vector elements
for (int i : myVector) {
// Code inside the loop
}
Size and Capacity:
// Get the number of elements
size_t size = myVector.size();
// Get the vector's capacity
size_t capacity = myVector.capacity();
Removing Elements:
// Remove the last element
myVector.pop_back();
// Remove element at a specific position
myVector.erase(myVector.begin() + 1);
Clearing Vector:
// Remove all elements
myVector.clear();