C++容器数据类型定义、测试用例

C++11 标准库提供了多种容器类型,每种容器定义了多个成员类型(如 value_typeiterator 等),用于与容器交互。以下详细说明各容器的数据类型定义、测试用例及注意事项。


一、顺序容器

1. std::vector<T>

类型定义

  • value_type: T
  • allocator_type: Allocator
  • size_type: size_t
  • difference_type: ptrdiff_t
  • reference: T&
  • const_reference: const T&
  • iterator: 随机访问迭代器
  • const_iterator: 常量随机访问迭代器

测试用例

复制代码
#include <vector>
#include <type_traits>

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

    // 使用类型定义声明变量
    std::vector<int>::value_type x = vec[0]; // x 是 int
    std::vector<int>::iterator it = vec.begin();
    std::vector<int>::const_iterator cit = vec.cbegin();
    std::vector<int>::size_type size = vec.size();

    static_assert(std::is_same<decltype(x), int>::value, "Error: x should be int");
    static_assert(std::is_same<decltype(it), std::vector<int>::iterator>::value, "Error");
    
    return 0;
}

2. std::list<T>

类型定义 :同 vector,但迭代器为双向迭代器。

测试用例

复制代码
#include <list>
#include <type_traits>

int main() {
    std::list<double> lst = {1.1, 2.2, 3.3};
    std::list<double>::iterator it = lst.begin();
    ++it; // 双向迭代器支持递增
    static_assert(std::is_same<decltype(it), std::list<double>::iterator>::value, "Error");
    return 0;
}

二、关联容器

1. std::map<Key, T>

类型定义

  • key_type: Key
  • mapped_type: T
  • value_type: std::pair<const Key, T>
  • key_compare: 比较函数类型
  • allocator_type: Allocator

测试用例

复制代码
#include <map>
#include <type_traits>

int main() {
    std::map<int, std::string> m = {{1, "one"}, {2, "two"}};

    std::map<int, std::string>::key_type k = 3; // k 是 int
    std::map<int, std::string>::mapped_type v = "three"; // v 是 std::string
    m.insert(std::map<int, std::string>::value_type(k, v));

    static_assert(std::is_same<decltype(m)::value_type, std::pair<const int, std::string>>::value, "Error");
    return 0;
}

三、无序容器

1. std::unordered_set<T>

类型定义

  • key_type: T
  • value_type: T
  • hasher: 哈希函数类型
  • key_equal: 键相等比较类型

测试用例

复制代码
#include <unordered_set>
#include <type_traits>

int main() {
    std::unordered_set<int> s = {1, 2, 3};
    std::unordered_set<int>::hasher h = s.hash_function();
    static_assert(std::is_same<decltype(s)::key_type, int>::value, "Error");
    return 0;
}

四、使用注意事项

  1. 迭代器失效

    • vector/deque 插入/删除可能导致迭代器失效。
    • list/关联容器 的插入不会使迭代器失效,删除仅影响被删元素对应的迭代器。
  2. 类型差异

    • map::value_typepair<const Key, T>,不可修改 Key
    • 无序容器的 key_type 必须支持哈希和相等比较。
  3. 分配器类型

    • 默认使用 std::allocator,高级场景可自定义分配器,需确保与容器兼容。
  4. 性能考量

    • vector 适合随机访问,list 适合频繁插入/删除。
    • 无序容器在键哈希均匀时提供 O(1) 访问。
  5. C++11 新特性

    • 使用 emplace 直接构造元素,避免拷贝。

    • 基于范围的 for 循环简化遍历:

      复制代码
      for (const auto& pair : map) { /* ... */ }

通过理解容器的数据类型定义,可以更安全高效地使用 STL 容器,避免类型错误和性能陷阱。

相关推荐
孞㐑¥8 分钟前
C++之红黑树
开发语言·c++·经验分享·笔记
今天也要早睡早起22 分钟前
代码随想录算法训练营Day32| 完全背包问题(二维数组 & 滚动数组)、LeetCode 518 零钱兑换 II、377 组合总数 IV、爬楼梯(进阶)
数据结构·c++·算法·leetcode·动态规划·完全背包
字节旅行者37 分钟前
C++中如何使用STL中的list定义一个双向链表,并且实现增、删、改、查操作
开发语言·数据结构·c++·链表
搞程序的心海39 分钟前
用Scala玩转Flink:从零构建实时处理系统
开发语言·flink·scala
x66ccff1 小时前
[特殊字符] Pandas 常用操作对比:Python 运算符 vs Pandas 函数
开发语言·python·pandas
ChoSeitaku1 小时前
NO.66十六届蓝桥杯备战|基础算法-贪心-区间问题|凌乱的yyy|Rader Installation|Sunscreen|牛栏预定(C++)
c++·算法·蓝桥杯
逆风优雅1 小时前
python 爬取网站图片的小demo
开发语言·python
OneQ6661 小时前
C++自学笔记---指针在数组遍历中的应用
c++·笔记·算法
EnigmaCoder1 小时前
蓝桥杯刷题周计划(第四周)
c++·算法·蓝桥杯
m0_616188491 小时前
PDF预览-搜索并高亮文本
开发语言·javascript·ecmascript