【编程】C++ 常用容器以及一些应用案例

介绍一些我常用的C++容器和使用方法,以及使用案例。blog

1 概述

容器(Container)是一个存储其他对象集合的持有者对象。容器以类模板实现,对支持的元素类型有很大的灵活性。容器管理元素的存储并提供多个成员函数来访问和操作元素。

两个主要类别:

  • 序列容器(Sequence container):将元素维护在线性序列中,通过元素的位置来访问索引某个元素,可以用来存放不需要检索的数据。
  • 关联容器(Associative container):允许基于键(key)而不是位置进行有效检索(即搜索某个key获取其内容)。通常使用二叉搜索树或哈希表实现。可以用来放置频繁检索的数据。

下面,我将介绍几种常用的序列容器无序关联容器以及它们的典型用例。

2 序列容器 Sequence Container

序列容器指的是标准库(STL)中实现数据元素存储的一组容器类模板。包括Array、Vector、List、Forward List、Deque。

  • Array:编译时大小不可调整的容器类型。
  • Vector:具有快速随机访问并在添加元素时自动调整大小的容器类型。
  • Deque:具有相对较快的随机访问的双端队列。
  • List:双向链表。
  • Forward list:单向链表。

2.1 Array

std::array 是封装固定大小数组的容器。

#include <array>
// 声明一个包含 3 个元素的整数array
array<int, 3> arr = {0, 1, 2};
// 使用 size() 成员函数获取array的大小
arr.size();
// 使用下标运算符 [] 访问array中索引为 0 的元素
arr[0];

2.2 Vector

std::vector 是 C++ 标准库中的类模板。它表示可以改变容量大小的序列容器,使用连续的存储位置存储元素。Vector的大小可以动态改变,容器自动处理存储。它动态分配数组以存储元素,与Array相比,Vector消耗更多的内存来管理存储内容。

#include <vector>
// 声明和初始化一个 vector 的方式
vector<int> vec = {1, 2, 4};
vector<int> vec {1, 2, 3};
vector<int> vec(4, 3); // 4 是大小,3 是值 {3, 3,3, 3}
// 将索引为 3 的元素赋值为 5
vec[3] = 5;
// 声明一个字符串向量
vector<string> planets;
// 向向量中添加字符串 "Mercury"
planets.push_back("Mercury");
// 检索向量中的元素数量
planets.size();
// 检索向量当前的容量
planets.capacity();

3 无序关联容器 Unordered Associative Container

关联容器存储由键值(key)和映射值(map)组合形成的元素。基于键快速检索单个元素。键值通常用于唯一标识元素。

3.1 无序映射 Unordered Map

无序映射是 C++ 中高效存储键值对而不维护特定顺序的关联容器。它们提供基于键的快速检索、插入和删除操作,适用于需要快速访问由键标识的元素的场景。与有序容器相比,无序映射优先速度而不是元素顺序,提供更快的元素访问。

#include <unordered_map>
// 声明一个具有整数键和值的无序映射
std::unordered_map<int, int> freq_counter;
// 访问与键 1 关联的值
freq_counter[1];
// 将一个键值对插入到无序映射中
freq_counter.insert(std::make_pair(2, 1));

3.2 无序集合 Unordered Set

无序集合是以无序方式存储一组唯一元素的容器。无序集合不维护其元素之间的特定顺序。它们提供快速的检索、插入和删除操作,通常使用哈希表实现。这使它们适用于需要快速查找唯一元素的场景,而不用担心顺序。

#include <unordered_set>
// 声明并初始化一个包含整数元素的无序集合
std::unordered_set<int> mySet{2, 7, 1, 8, 2, 8};
// 向无序集合中插入值 5
mySet.insert(5);
// 如果存在,从无序集合中删除值 5
mySet.erase(5);

3.3 应用场景

需求描述

工作中遇到的一个使用案例:
设计一个目标车辆速度管理系统,旨在存储和调节交通车辆(障碍物)的速度。如果检测到车辆的速度大小不确定,系统将检索并应用上次该目标已知的速度大小以及其当前的速度方向。目的是维持交通上目标车辆的速度幅度稳定程度,尽量减少速度的突然变化,来弥补感知系统中Tracker自身的不足,因为如果某个车辆突然转向或突然出现到场景中,其速度可信程度不高。

代码片段示例

// 初始化用于存储障碍物对象和障碍物 ID 的关联容器
std::unordered_map<int, Eigen::Vector3d> obstacles_;
std::unordered_set<int> obstacle_ids_;

// 添加障碍物信息
obstacle_ids_.insert(id);
obstacles_[id] = velocity;

// 从容器中移除障碍物
obstacles_.erase(id);
obstacle_ids_.erase(id);

// 获取最后的速度
auto it = obstacles_.find(id);
// 如果找到 ID,则返回速度
if (it != obstacles_.end()) 
{
    return it->second; 
} 
else 
{
    // 如果未找到,则返回零速度
}

// 移除不再需要的障碍物 ID
std::unordered_set<int> ids_to_remove;
for (const auto& obstacle : obstacles_)
{
    int id = obstacle.first;
    if (obstacle_ids_.find(id) == obstacle_ids_.end())
    {
        ids_to_remove.insert(id);
    }
}
for (int id_to_remove : ids_to_remove)
{
    obstacles_.erase(id_to_remove);
}

// 清除信息
obstacle_ids_.clear();

// 使用最后速度的大小以及当前方向
double magnitude = last_velocity.norm();
// 将当前速度归一化以保持其方向
if (current_velocity.norm() > 0.0) // 避免除以零
{
    current_velocity.normalize();
}
else
{
    // 如果当前速度为零,直接返回它
    return current_velocity;
}
// 将归一化后的当前速度按照最后速度的大小进行缩放
current_velocity *= magnitude;
// 得到所需的速度
return current_velocity;

4 结合LeetCode题练习

使用Vector和Unodered map 解决下面这个题:
3005 Count Elements With Maximum Frequency

You are given an array nums consisting of positive integers.
Return the total frequencies of elements in nums such that those elements all have the maximum frequency.
The frequency of an element is the number of occurrences of that element in the array.

Example 1:
Input: nums = [1,2,2,3,1,4]
Output: 4
Explanation: The elements 1 and 2 have a frequency of 2 which is the maximum frequency in the array.
So the number of elements in the array with maximum frequency is 4.

Example 2:
Input: nums = [1,2,3,4,5]
Output: 5
Explanation: All elements of the array have a frequency of 1 which is the maximum.
So the number of elements in the array with maximum frequency is 5.

Constraints:
1 <= nums.length <= 100
1 <= nums[i] <= 100

4.1 使用 Vector

code file

#include <iostream>
#include <vector>

using namespace std;

class Solution 
{
public:
    int maxFrequencyElements(vector<int>& nums) 
    {
        vector<int> frequency (nums.size(), 0);
        for (int i = 0; i < frequency.size(); i ++)
        {
            frequency[i] = countDuplicatedNumber(i, nums);
        }
        for (int element : frequency)
        {
            std::cout << element << " ";
        }
        cout << endl;
        int max_value = checkMaxValue(frequency);
        return sumMaxValue(max_value, frequency);
    }
    int countDuplicatedNumber(const int& index, const vector<int>& vector)
    {
        int number = 1;
        for (int i = 1; i + index < vector.size(); i++)
        {
            if (vector[i + index] == vector[index])
            {
                number ++;
            }
        }
        return number;
    }
    int checkMaxValue(const vector<int>& vector)
    {
        int max = 0;
        for (int i = 0; i < vector.size(); i++)
        {
            if (vector[i] > max)
            {
                max = vector[i];
            }
        }
        cout << "max value in the vec is: " << max << endl;
        return max;
    }
    int sumMaxValue(const int& max, const vector<int>& vector)
    {
        int sum = 0;
        for (int i = 0; i < vector.size(); i++)
        {
            if (vector[i] == max)
            {
                sum += vector[i];
            }
        }
        return sum;
    }
};

int main()
{
    vector<int> num1 {1,2,2,3,4,4,1};
    Solution solution;
    float result = solution.maxFrequencyElements(num1);
    cout << "result is: " << result << endl;
}

4.1 使用 Unnordered map

code file, [reference]

#include <iostream>
#include <vector>
#include <unordered_map>

using namespace std;

class Solution 
{
public:
    int maxFrequencyElements(vector<int>& nums) 
    {
        std::unordered_map<int, int> freq_counter;
        for(int num : nums)
        {
            freq_counter[num]++;
        }
        
        int max_frequency = 0;
        for (const auto& entry : freq_counter)
        {
            max_frequency = std::max(max_frequency, entry.second);
        }

        int max_freq_elements = 0;
        for (const auto& entry : freq_counter)
        {
            if (entry.second == max_frequency)
            {
                max_freq_elements++;
            }
        }

        int total_frequency = max_frequency * max_freq_elements;
        return total_frequency;
    }
};

int main()
{
    vector<int> num1 {7,7,7,1,2,2,3,4,4,1};
    Solution solution;
    int result = solution.maxFrequencyElements(num1);
    cout << "result is: " << result << endl;
}

总结对比Vector和Unordered Map
Vector:

  1. 相对简单:代码逻辑相对直接,易于理解和实现。
  2. 不需要额外的头文件:只使用了 <iostream><vector> 头文件,比较轻量级。
  3. 效率较低:在countDuplicatedNumber函数中,每次都需要遍历数组来计算重复元素的数量,时间复杂度为 O ( n 2 ) O(n^2) O(n2)

Unordered Map:

  1. 更高效的查找和统计:使用unordered_map可以在 O ( n ) O(n) O(n)的时间内完成元素的计数。
  2. 引入了<unordered_map>头文件。


时而蹦出来的点点怀念

如果云朵有颜色 是我写信的过程
  • 26
    点赞
  • 27
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MengWoods

感谢支持!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值