Skip to content

Initialization

  • Empty Vector:
std::vector<int> vec;
  • Pre-allocate Size:
std::vector<int> vec(10);  // Contains 10 elements, all initialized to 0
  • Pre-allocate Size with Value:
std::vector<int> vec(10, 1);  // Contains 10 elements, all initialized to 1
  • Initialize from Array:
int arr[] = {1, 2, 3};
std::vector<int> vec(arr, arr + sizeof(arr) / sizeof(int));
  • Initialize from Another Vector:
std::vector<int> vec2(vec);

Adding Elements

  • Append to End:
vec.push_back(4);
  • Insert at Beginning:
vec.insert(vec.begin(), 4);
  • Insert at Specific Position:
vec.insert(vec.begin() + 1, 4);
  • append vector
vec.insert(vec.end(),vec_2.begin(),vec_2.end())

Access Elements

  • Access Last Element:
int last = vec.back();
  • Access First Element:
int first = vec.front();
  • Access i-th Element (0-based):
int elem = vec[i];

Removing Elements

  • Remove Last Element:
vec.pop_back();    ->void
  • Remove i-th Element:
vec.erase(vec.begin() + i);
  • Remove a Range of Elements:
vec.erase(vec.begin() + i, vec.begin() + j);  // Removes elements from i to j-1

Query Attributes

  • Get Vector Size:
size_t size = vec.size();
  • Check if Vector is Empty:
bool isEmpty = vec.empty();

Other Common Operations

  • Clear Vector:
vec.clear();
  • Resize Vector:
vec.resize(20);  // New elements are initialized to 0
  • Find Element Position:
std::find(vec.begin(), vec.end(), value) != vec.end()
  • Sort Vector:
std::sort(vec.begin(), vec.end());

Examples

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {3, 1, 4, 1, 5, 9, 2, 6, 5};

    // 使用 std::min_element 和 lambda 函数找到最小正整数
    auto it = std::min_element(vec.begin(), vec.end(), [](int a, int b) {
        if (a <= 0) return false;
        if (b <= 0) return true;
        return a < b;
    });

    if (it != vec.end() && *it > 0) {
        std::cout << "最小正整数是:" << *it << std::endl;
    } else {
        std::cout << "没有找到正整数" << std::endl;
    }

    return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<int> vec = {1, 2, 2, 3, 4, 4, 5};

    // 先排序
    std::sort(vec.begin(), vec.end());

    // 使用 std::unique 去重
    auto last = std::unique(vec.begin(), vec.end());

    // 删除多余元素
    vec.erase(last, vec.end());

    // 输出去重后的向量
    for (const auto& elem : vec) {
        std::cout << elem << " ";
    }
    std::cout << std::endl;

    return 0;
}