stl_vector的一些用法

说明:这是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

  1. Declaration and Initialization:
  • Declaration: std::vector<int> myVector;
  • Initialization: std::vector<int> anotherVector = {1, 2, 3};
  1. Adding Elements:
  • myVector.push_back(42); // Add element at the end
  • myVector.insert(myVector.begin() + 1, 55); // Insert at a specific position
  1. Accessing Elements:
  • int value = myVector[0]; // Access by index
  • int firstElement = myVector.front(); // Access the first element
  • int lastElement = myVector.back(); // Access the last element
  1. Iterating Through Vector:
  • for (int i : myVector) { /* Loop through vector elements */ }
  1. Size and Capacity:
  • size_t size = myVector.size(); // Get the number of elements
  • size_t capacity = myVector.capacity(); // Get the vector’s capacity
  1. Removing Elements:
  • myVector.pop_back(); // Remove the last element
  • myVector.erase(myVector.begin() + 1); // Remove element at a specific position
  1. 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();
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值