C++数据类型及标准库容器(Standard Template Library Containers, STL)

185 阅读5分钟

@TOC

Ⅰ. C++基本数据类型:

类型关键字
布尔型bool
字符型char
整型int
浮点型float
双浮点型double
无类型void
宽字符型wchar_t

各种变量类型在内存中存储值时需要占用的内存,以及该类型的变量所能存储的最大值和最小值:

类型存储范围
char1 个字节-128 到 127 或者 0 到 255
unsigned char1 个字节0 到 255
signed char1 个字节-128 到 127
int4 个字节-2147483648 到 2147483647
unsigned int4 个字节0 到 4294967295
signed int4 个字节-2147483648 到 2147483647
short int2 个字节-32768 到 32767
unsigned short int2 个字节0 到 65,535
signed short int2 个字节-32768 到 32767
long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
signed long int8 个字节-9,223,372,036,854,775,808 到 9,223,372,036,854,775,807
unsigned long int8 个字节0 到 18,446,744,073,709,551,615
float4 个字节精度型占4个字节(32位)内存空间,+/- 3.4e +/- 38 (~7 个数字)
double8 个字节双精度型占8 个字节(64位)内存空间,+/- 1.7e +/- 308 (~15 个数字)
long double16 个字节长双精度型 16 个字节(128位)内存空间,可提供18-19位有效数字。
wchar_t2 或 4 个字节1 个宽字符

注意: 枚举类型(enumeration)是C++中的一种派生数据类型,它是由用户定义的若干枚举常量的集合。 如果一个变量只有几种可能的值,可以定义为枚举(enumeration)类型。所谓"枚举"是指将变量的值一 一列举出来,变量的值只能在列举出来的值的范围内。创建枚举,需要使用关键字 enum
注意: 不同系统会有所差异,一字节为 8 位。
注意: 默认情况下,int、short、long都是带符号的,即 signed。
注意: long int 8 个字节,int 都是 4 个字节,早期的 C 编译器定义了 long int 占用 4 个字节,int 占用 2 个字节,新版的 C/C++ 标准兼容了早期的这一设定。
注意,各种类型的存储大小与系统位数有关,但目前通用的以64位系统为主。以下列出了32位系统与64位系统的存储大小的差别(windows 相同):

系统的存储大小.png

Ⅱ. C++标准模板库容器(Standard Template Library Containers, STL):

容器库 是类模板与算法的汇集,允许程序员简单地访问常见数据结构,例如队列、链表和栈。有两 (C++11 前)三 (C++11 起)类容器:

  • 顺序容器
  • 关联容器
  • 无序关联容器

各容器具体信息如下:

STL ContainerHeaderApplications
array< array >顺序容器,封装固定大小数组的容器。
vector< vector >顺序容器,直接访问任意元素,快速插入、删除尾部元素。采用连续的线性空间,可以将其理解为一个“变长数组”。
deque< deque >顺序容器,直接访问任意元素,快速插入、删除头部和尾部元素,基于数组实现。
list< list >顺序性容器,快速插入、删除任意位置元素,以双向链表的形式实现。
set< set >关联容器,快速查询元素,无重复关键字。基于红黑树实现。
multiset< set >关联容器,与set相同,但允许重复关键字。基于红黑树实现。
map< map >关联式容器,Key/value pair mapping(键值对映射)。不允许重复关键字,使用关键字快速查询元素。基于红黑树实现。
multimap< map >关联容器,与map相同,但允许重复关键字。基于红黑树实现。
stack< stack >容器适配器,后进先出容器,基于deque实现。
queue< queue >容器适配器,先进先出容器,基于deque实现。
priority_queue< queue >容器适配器,高优先级元素先删除。基于vector实现,默认实现的就是大根堆。

Ⅲ. C++ STL 常用算法:

std::vector<int> v {3, 1, -4, 1, 5, 9};

std::vector<int>::iterator result = std::min_element(v.begin(), v.end()); // 容器中的最小值
std::vector<int>::iterator result = std::min_element(v.begin(), v.end(), [](int a, int b){ return std::abs(a) > std::abs(b); });
【#include <algorithm>】 https://zh.cppreference.com/w/cpp/algorithm/min_element
std::vector<int>::iterator result = std::max_element(v.begin(), v.end()); // 容器中的最大值
std::vector<int>::iterator result = std::max_element(v.begin(), v.end(), [](int a, int b){ return std::abs(a) < std::abs(b); });

【#include <algorithm>】 https://zh.cppreference.com/w/cpp/algorithm/max_element
int count = std::count(v.cbegin(), v.cend(), int target);  // 计算容器内中 target 出现的个数
// 由 lambda 表达式计算能被 4 整除的元素个数
int count = std::count_if(v.begin(), v.end(), [](int i) { return i % 4 == 0; }); 
【#include <algorithm>】 https://zh.cppreference.com/w/cpp/algorithm/count

std::sort(v.begin(), v.end());                                            // 容器排序
std::sort(v.begin(), v.end(), [](int a, int b){ return a > b; });
【#include <algorithm>】 https://zh.cppreference.com/w/cpp/algorithm/sort
std::for_each(v.begin(), v.end(), [](int &n){ n++; });                  // 遍历操作
【#include <algorithm>】 https://zh.cppreference.com/w/cpp/algorithm/for_each
std::transform(svalue.begin(),svalue.end(), svalue.begin(), ::tolower); // 字母转小写
std::transform(svalue.begin(),svalue.end(), svalue.begin(), ::toupper); // 字母转大写
【#include <algorithm>】 https://zh.cppreference.com/w/cpp/algorithm/transform

long long int sum = std::accumulate(v.begin(), v.end(), 0ll);                 // 累加
// unsigned long long int sum = std::accumulate(v.begin(), v.end(), 0ull);
// 0ll(0ull) 用于防止 std::accumulte 计算结果溢出
int product = std::accumulate(v.begin(), v.end(), 1, std::multiplies<int>()); // 累积
【#include <numeric>】 https://zh.cppreference.com/w/cpp/algorithm/accumulate

end.友情链如有疑问可留言沟通交流end. 友情链接^{如有疑问可留言沟通交流}

  1. 点云文章专栏
  2. Visual studio文章专栏
  3. ...

代码调试不易,转载请标明出处!
如果感觉本文对您有帮助,请留下您的赞,您的支持是我坚持写作分享的最大动力,谢谢!

References
0.Standard C++
1.cppreference.com
2.apiref.com/cpp-zh
3.cplusplus.com
4.C++ 标准库参考 | Microsoft Learn
5.C++ 教程 | 菜鸟教程
6.
7.
可以肯定的是学海无涯,这篇文章也会随着对 C++ 的深入学习而持续更新,
欢迎各位在评论区留言进行探讨交流。