我们这章所学的map / set 系列和后续会讲解的 unordered_map / unordered 系列都属于是关联式容器。
那么什么是关联式容器? 关联式容器用于存储键值对(
<key, value>),与序列式容器不同,关联式容器的元素通过键来查找、插入和删除。它们通常采用平衡二叉搜索树(如map、set的红黑树实现)或哈希表(如unordered_map、unordered_set)来实现,因此在查找、插入和删除等操作时,比序列式容器更高效,时间复杂度通常为对数级别(O(log n)或O(1))。常见的关联式容器包括map、set、multimap、multiset以及使用哈希表实现的unordered_map和unordered_set。 它的底层逻辑结构是非线性的,两个位置的储存值之间是紧密关联的,如果交换他们的值就会破坏其储存结构。
二、键值对(pair)
1.认识
键值对(Key-Value Pair)是指由键(Key)和值(Value)组成的元素结构。键是唯一标识元素的标志,而值则是与该键相关联的数据。常见的关联式容器(如
map、unordered_map)存储的就是这种键值对结构。
- 键(Key):用于唯一标识元素。每个键在容器中是唯一的,不会重复。
- 值(Value):与键相关联的数据,键对应的值可以是任何数据类型。
- 例如,
map<int, string>存储的键值对可能是{1, "apple"},其中1是键,"apple"是与该键相关的值。通过键,我们可以快速查找到对应的值。 - 简而言之,键值对是容器中元素的基本组成单元,键用来查找和访问数据,值存储实际的数据内容。
pair的基本结构
代码语言:javascript
AI代码解释
template <class T1, class T2> struct pair { typedef T1 first_type; // 定义第一个类型的别名 typedef T2 second_type; // 定义第二个类型的别名 T1 first; // 第一个元素 T2 second; // 第二个元素 // 默认构造函数,使用默认构造函数初始化 first 和 second pair() : first(T1()), second(T2()) {} // 带参数的构造函数,用给定的值初始化 first 和 second pair(const T1& a, const T2& b) : first(a), second(b) {} };2.构造函数
在这里插入图片描述
默认构造
代码语言:javascript
AI代码解释
// 默认构造函数:初始化 `first` 和 `second` 成员为各自类型的默认值 pair(); // 实现:通过默认构造函数初始化 `first` 和 `second` 成员 pair() : first(T1()), second(T2()) {} // `T1()` 和 `T2()` 是类型 T1 和 T2 的默认构造函数, // 它们会被调用来初始化 `first` 和 `second`。 // 如果 `T1` 或 `T2` 是类类型,默认构造函数会被调用。 // 如果是基本类型(如 `int`),则 `first` 和 `second` 会被初始化为 0。拷贝构造
代码语言:javascript
AI代码解释
// 拷贝构造函数:通过拷贝另一个 `pair` 对象来初始化当前对象 template<class U, class V> pair(const pair<U, V>& pr); // 实现:用另一个 `pair<U, V>` 对象的 `first` 和 `second` 成员来初始化当前 `pair` 对象 pair(const pair<U, V>& pr) : first(pr.first), second(pr.second) {} // `pr.first` 和 `pr.second` 分别是传入的 `pair` 对象的 `first` 和 `second` 成员。 // 当前对象的 `first` 和 `second` 会被初始化为这些值。 // 注意:`U` 和 `V` 是临时 `pair` 对象的类型,可以不同于当前 `pair` 的类型,但必须能够赋值。带参数构造
代码语言:javascript
AI代码解释
// 带参数构造函数:用指定的值初始化 `first` 和 `second` 成员 pair(const first_type& a, const second_type& b); // `first_type` 和 `second_type` 是 `pair` 中 `first` 和 `second` 成员的类型。 // `a` 和 `b` 分别是初始化这两个成员的值。3.make_pair
- pair是一个模板类,需要我们显示的传入两个类型(T1 和 T2)来实例化 pair<T1 , T2>对象。而使用map或者其他的关联式容器的时候,我们需要为每个元素都传入一个键值对。每次插入到map中,显示构造pair的对象的时候就会显的很繁琐。
代码语言:javascript
AI代码解释
//手动构造 std::map<int, std::string> m; m.insert(std::pair<int, std::string>(1, "apple"));- 为了简化这种操作,C++标准库提供了一个非常方便的工具——std::make_pair 函数模板。使用时,我们不需要显示指定 pair 的类型,它会自动推导类型,减少显示传递类型的繁琐性。
代码语言:javascript
AI代码解释
std::map<int, std::string> m; m.insert(std::make_pair(1, "apple"));总结
- 容器分类
- 序列式容器:线性序列结构,存储元素本身,元素位置独立
- 关联式容器:非线性结构,存储键值对,元素通过键访问,操作效率更高
- 键值对(pair)特性
- 组成:键(key) + 值(value),键用于唯一标识,值存储实际数据
- 优势:提供快速查找、插入和删除操作
- 时间复杂度:通常为O(log n)或O(1)
- pair的实用工具
make_pair函数:自动推导类型,简化pair对象的创建
使用对比:
代码语言:javascript
AI代码解释
// 繁琐的手动构造 m.insert(std::pair<int, std::string>(1, "apple")); // 简化的自动推导 m.insert(std::make_pair(1, "apple"));关联式容器及其键值对机制是C++中实现高效数据检索的重要工具,特别适用于需要快速查找的场景。
m.jtbdb.pro/post/11757.html
m.jtbdb.pro/post/79719.html
m.jtbdb.pro/post/24682.html
m.jtbdb.pro/post/68046.html
m.jtbdb.pro/post/44202.html
m.jtbdb.pro/post/33995.html
m.jtbdb.pro/post/84266.html
m.jtbdb.pro/post/35555.html
m.jtbdb.pro/post/46260.html
m.jtbdb.pro/post/57197.html
m.jtbdb.pro/post/35133.html
m.jtbdb.pro/post/62442.html
m.jtbdb.pro/post/57593.html
m.jtbdb.pro/post/51913.html
m.jtbdb.pro/post/75575.html
m.jtbdb.pro/post/75777.html
m.jtbdb.pro/post/93391.html
m.jtbdb.pro/post/35575.html
m.jtbdb.pro/post/57393.html
m.jtbdb.pro/post/91717.html
m.jtbdb.pro/post/37917.html
m.jtbdb.pro/post/39713.html
m.jtbdb.pro/post/19999.html
m.jtbdb.pro/post/13943.html
m.jtbdb.pro/post/17797.html
m.jtbdb.pro/post/57117.html
m.jtbdb.pro/post/53777.html
m.jtbdb.pro/post/19555.html
m.jtbdb.pro/post/97931.html
m.jtbdb.pro/post/17917.html
m.jtbdb.pro/post/59995.html
m.jtbdb.pro/post/26004.html
m.jtbdb.pro/post/33979.html
m.jtbdb.pro/post/79795.html
m.jtbdb.pro/post/19337.html
m.jtbdb.pro/post/77371.html
m.jtbdb.pro/post/13599.html
m.jtbdb.pro/post/99553.html
m.jtbdb.pro/post/99511.html
m.jtbdb.pro/post/80848.html
m.jtbdb.pro/post/19733.html
m.jtbdb.pro/post/19711.html
m.jtbdb.pro/post/33771.html
m.jtbdb.pro/post/44668.html
m.jtbdb.pro/post/15399.html
m.jtbdb.pro/post/13491.html
m.jtbdb.pro/post/93571.html
m.jtbdb.pro/post/53177.html
m.jtbdb.pro/post/11317.html
m.jtbdb.pro/post/46046.html
m.jtbdb.pro/post/68620.html
m.jtbdb.pro/post/97313.html
m.jtbdb.pro/post/19777.html
m.jtbdb.pro/post/68466.html
m.jtbdb.pro/post/37777.html
m.jtbdb.pro/post/02280.html
m.jtbdb.pro/post/99339.html
m.jtbdb.pro/post/97377.html
m.jtbdb.pro/post/57337.html
m.jtbdb.pro/post/73913.html
m.jtbdb.pro/post/73593.html
m.jtbdb.pro/post/15335.html
m.jtbdb.pro/post/95377.html
m.jtbdb.pro/post/13795.html
m.jtbdb.pro/post/91979.html
m.jtbdb.pro/post/75913.html
m.jtbdb.pro/post/33975.html
m.jtbdb.pro/post/19779.html
m.jtbdb.pro/post/31513.html
m.jtbdb.pro/post/73931.html
m.jtbdb.pro/post/17757.html
m.jtbdb.pro/post/91519.html
m.jtbdb.pro/post/97133.html
m.jtbdb.pro/post/68808.html
m.jtbdb.pro/post/68448.html
m.jtbdb.pro/post/79953.html
m.jtbdb.pro/post/97191.html
m.jtbdb.pro/post/51199.html
m.jtbdb.pro/post/44002.html
m.jtbdb.pro/post/17513.html
m.jtbdb.pro/post/15359.html
m.jtbdb.pro/post/48444.html
m.jtbdb.pro/post/55113.html
m.jtbdb.pro/post/15319.html
m.jtbdb.pro/post/37173.html
m.jtbdb.pro/post/86240.html
m.jtbdb.pro/post/77775.html
m.jtbdb.pro/post/39511.html
m.jtbdb.pro/post/97991.html
m.jtbdb.pro/post/68886.html
m.jtbdb.pro/post/53933.html
m.jtbdb.pro/post/53191.html
m.jtbdb.pro/post/51333.html
m.jtbdb.pro/post/03059.html
m.jtbdb.pro/post/53751.html
m.jtbdb.pro/post/53711.html
m.jtbdb.pro/post/39933.html
m.jtbdb.pro/post/22428.html
m.jtbdb.pro/post/71935.html
m.jtbdb.pro/post/31731.html
m.jtbdb.pro/post/53715.html
m.jtbdb.pro/post/04680.html
m.jtbdb.pro/post/84680.html
m.jtbdb.pro/post/95751.html
m.jtbdb.pro/post/55575.html
m.jtbdb.pro/post/60686.html
m.jtbdb.pro/post/71311.html
m.jtbdb.pro/post/59115.html
m.jtbdb.pro/post/77517.html
m.jtbdb.pro/post/72178.html
m.jtbdb.pro/post/79591.html
m.jtbdb.pro/post/53353.html
m.jtbdb.pro/post/95715.html
m.jtbdb.pro/post/95315.html
m.jtbdb.pro/post/24466.html
m.jtbdb.pro/post/13553.html
m.jtbdb.pro/post/91917.html
m.jtbdb.pro/post/20820.html
m.jtbdb.pro/post/08260.html
m.jtbdb.pro/post/13535.html
m.jtbdb.pro/post/91359.html
m.jtbdb.pro/post/08466.html
m.jtbdb.pro/post/55199.html
m.jtbdb.pro/post/75957.html
m.jtbdb.pro/post/33511.html
m.jtbdb.pro/post/71837.html
m.jtbdb.pro/post/99753.html
m.jtbdb.pro/post/39173.html
m.jtbdb.pro/post/55759.html
m.jtbdb.pro/post/31135.html
m.jtbdb.pro/post/91793.html
m.jtbdb.pro/post/35155.html
m.jtbdb.pro/post/75557.html
m.jtbdb.pro/post/77539.html
m.jtbdb.pro/post/15131.html
m.jtbdb.pro/post/59351.html
m.jtbdb.pro/post/86660.html
m.jtbdb.pro/post/20080.html
m.jtbdb.pro/post/37711.html
m.jtbdb.pro/post/93935.html
m.jtbdb.pro/post/31933.html
m.jtbdb.pro/post/19939.html
m.jtbdb.pro/post/48020.html
m.jtbdb.pro/post/79739.html
m.jtbdb.pro/post/77977.html
m.jtbdb.pro/post/55715.html
m.jtbdb.pro/post/51171.html
m.jtbdb.pro/post/15959.html
m.jtbdb.pro/post/99539.html
m.jtbdb.pro/post/84686.html
m.jtbdb.pro/post/13717.html
m.jtbdb.pro/post/42020.html
m.jtbdb.pro/post/20004.html
m.jtbdb.pro/post/75159.html
m.jtbdb.pro/post/57199.html
m.jtbdb.pro/post/59975.html
m.jtbdb.pro/post/00288.html
m.jtbdb.pro/post/95793.html
m.jtbdb.pro/post/35779.html
m.jtbdb.pro/post/42820.html
m.jtbdb.pro/post/57195.html
m.jtbdb.pro/post/79597.html
m.jtbdb.pro/post/31357.html
m.jtbdb.pro/post/59399.html
m.jtbdb.pro/post/55719.html
m.jtbdb.pro/post/93313.html
m.jtbdb.pro/post/88280.html
m.jtbdb.pro/post/99155.html
m.jtbdb.pro/post/48408.html
m.jtbdb.pro/post/19359.html
m.jtbdb.pro/post/15595.html
m.jtbdb.pro/post/66246.html
m.jtbdb.pro/post/11955.html
m.jtbdb.pro/post/55917.html
m.jtbdb.pro/post/91559.html
m.jtbdb.pro/post/26280.html
m.jtbdb.pro/post/39571.html
m.jtbdb.pro/post/79175.html
m.jtbdb.pro/post/17157.html
m.jtbdb.pro/post/97975.html
m.jtbdb.pro/post/57951.html
m.jtbdb.pro/post/73173.html
m.jtbdb.pro/post/59111.html
m.jtbdb.pro/post/75513.html
m.jtbdb.pro/post/37577.html
m.jtbdb.pro/post/31537.html
m.jtbdb.pro/post/84286.html
m.jtbdb.pro/post/39931.html
m.jtbdb.pro/post/93373.html
m.jtbdb.pro/post/60664.html
m.jtbdb.pro/post/20222.html
m.jtbdb.pro/post/75531.html
m.jtbdb.pro/post/55979.html
m.jtbdb.pro/post/57793.html
m.jtbdb.pro/post/39913.html
m.jtbdb.pro/post/91113.html
m.jtbdb.pro/post/19931.html
m.jtbdb.pro/post/19973.html
m.jtbdb.pro/post/31579.html
m.jtbdb.pro/post/86226.html
m.jtbdb.pro/post/77959.html
m.jtbdb.pro/post/82068.html
m.jtbdb.pro/post/55557.html
m.jtbdb.pro/post/71795.html
m.jtbdb.pro/post/75771.html
m.jtbdb.pro/post/35591.html
m.jtbdb.pro/post/95755.html
m.jtbdb.pro/post/73139.html
m.jtbdb.pro/post/19731.html
m.jtbdb.pro/post/57157.html
m.jtbdb.pro/post/84048.html
m.jtbdb.pro/post/19519.html
m.jtbdb.pro/post/79599.html
m.jtbdb.pro/post/79771.html
m.jtbdb.pro/post/73915.html
m.jtbdb.pro/post/11359.html
m.jtbdb.pro/post/42680.html
m.jtbdb.pro/post/93377.html
m.jtbdb.pro/post/64044.html
m.jtbdb.pro/post/95579.html
m.jtbdb.pro/post/31193.html
m.jtbdb.pro/post/97353.html
m.jtbdb.pro/post/15915.html
m.jtbdb.pro/post/93537.html
m.jtbdb.pro/post/55173.html
m.jtbdb.pro/post/13591.html
m.jtbdb.pro/post/31531.html
m.jtbdb.pro/post/60020.html
m.jtbdb.pro/post/28246.html
m.jtbdb.pro/post/13159.html
m.jtbdb.pro/post/17791.html
m.jtbdb.pro/post/37515.html
m.jtbdb.pro/post/33333.html
m.jtbdb.pro/post/31711.html
m.jtbdb.pro/post/77397.html
m.jtbdb.pro/post/71595.html
m.jtbdb.pro/post/91571.html
m.jtbdb.pro/post/13517.html
m.jtbdb.pro/post/15159.html
m.jtbdb.pro/post/42082.html
m.jtbdb.pro/post/13797.html
m.jtbdb.pro/post/39515.html
m.jtbdb.pro/post/66820.html
m.jtbdb.pro/post/28080.html
m.jtbdb.pro/post/53791.html
m.jtbdb.pro/post/00204.html
m.jtbdb.pro/post/22604.html
m.jtbdb.pro/post/13735.html
m.jtbdb.pro/post/86842.html
m.jtbdb.pro/post/53339.html
m.jtbdb.pro/post/77979.html
m.jtbdb.pro/post/77173.html
m.jtbdb.pro/post/53731.html
m.jtbdb.pro/post/97179.html
m.jtbdb.pro/post/75757.html
m.jtbdb.pro/post/13375.html
m.jtbdb.pro/post/59193.html
m.jtbdb.pro/post/19355.html
m.jtbdb.pro/post/17135.html
m.jtbdb.pro/post/79317.html
m.jtbdb.pro/post/95775.html
m.jtbdb.pro/post/79915.html
m.jtbdb.pro/post/26406.html
m.jtbdb.pro/post/35735.html
m.jtbdb.pro/post/79731.html
m.jtbdb.pro/post/57771.html
m.jtbdb.pro/post/22200.html
m.jtbdb.pro/post/75313.html
m.jtbdb.pro/post/99513.html
m.jtbdb.pro/post/88666.html
m.jtbdb.pro/post/71533.html
m.jtbdb.pro/post/33153.html
m.jtbdb.pro/post/88208.html
m.jtbdb.pro/post/75193.html
m.jtbdb.pro/post/99535.html
m.jtbdb.pro/post/39991.html
m.jtbdb.pro/post/91111.html
m.jtbdb.pro/post/93753.html
m.jtbdb.pro/post/73535.html
m.jtbdb.pro/post/62428.html
m.jtbdb.pro/post/95791.html
m.jtbdb.pro/post/99157.html
m.jtbdb.pro/post/17955.html
m.jtbdb.pro/post/93177.html
m.jtbdb.pro/post/95155.html
m.jtbdb.pro/post/11795.html
m.jtbdb.pro/post/73379.html
m.jtbdb.pro/post/53115.html
m.jtbdb.pro/post/60046.html
m.jtbdb.pro/post/55755.html
m.jtbdb.pro/post/86608.html
m.jtbdb.pro/post/35397.html
m.jtbdb.pro/post/53155.html
m.jtbdb.pro/post/46008.html
m.jtbdb.pro/post/51539.html
m.jtbdb.pro/post/80080.html
m.jtbdb.pro/post/55751.html
m.jtbdb.pro/post/71153.html
m.jtbdb.pro/post/15973.html
m.jtbdb.pro/post/11573.html
m.jtbdb.pro/post/08428.html
m.jtbdb.pro/post/39115.html
m.jtbdb.pro/post/15919.html
m.jtbdb.pro/post/57115.html
m.jtbdb.pro/post/71539.html
m.jtbdb.pro/post/59997.html
m.jtbdb.pro/post/40844.html
m.jtbdb.pro/post/28226.html
m.jtbdb.pro/post/99199.html
m.jtbdb.pro/post/48739.html
m.jtbdb.pro/post/11337.html
m.jtbdb.pro/post/88022.html
m.jtbdb.pro/post/91935.html
m.jtbdb.pro/post/31937.html
m.jtbdb.pro/post/82460.html
m.jtbdb.pro/post/79351.html
m.jtbdb.pro/post/71111.html
m.jtbdb.pro/post/99577.html
m.jtbdb.pro/post/59939.html
m.jtbdb.pro/post/77571.html
m.jtbdb.pro/post/66204.html
m.jtbdb.pro/post/71513.html
m.jtbdb.pro/post/17315.html
m.jtbdb.pro/post/71577.html
m.jtbdb.pro/post/79379.html
m.jtbdb.pro/post/93337.html
m.jtbdb.pro/post/17953.html
m.jtbdb.pro/post/19579.html
m.jtbdb.pro/post/57171.html
m.jtbdb.pro/post/44082.html
m.jtbdb.pro/post/82080.html
m.jtbdb.pro/post/93915.html
m.jtbdb.pro/post/35737.html
m.jtbdb.pro/post/35115.html
m.jtbdb.pro/post/66602.html
m.jtbdb.pro/post/11335.html
m.jtbdb.pro/post/48240.html
m.jtbdb.pro/post/80608.html
m.jtbdb.pro/post/39159.html
m.jtbdb.pro/post/13519.html
m.jtbdb.pro/post/93591.html
m.jtbdb.pro/post/93319.html
m.jtbdb.pro/post/33133.html
m.jtbdb.pro/post/31511.html
m.jtbdb.pro/post/15559.html
m.jtbdb.pro/post/79531.html
m.jtbdb.pro/post/82682.html
m.jtbdb.pro/post/11733.html
m.jtbdb.pro/post/99151.html
m.jtbdb.pro/post/26668.html
m.jtbdb.pro/post/20622.html
m.jtbdb.pro/post/13937.html
m.jtbdb.pro/post/91577.html
m.jtbdb.pro/post/37111.html
m.jtbdb.pro/post/75117.html
m.jtbdb.pro/post/79171.html
m.jtbdb.pro/post/95151.html
m.jtbdb.pro/post/40862.html
m.jtbdb.pro/post/37937.html
m.jtbdb.pro/post/00820.html
m.jtbdb.pro/post/59559.html
m.jtbdb.pro/post/15351.html
m.jtbdb.pro/post/62882.html
m.jtbdb.pro/post/53999.html
m.jtbdb.pro/post/77117.html
m.jtbdb.pro/post/19591.html
m.jtbdb.pro/post/73553.html
m.jtbdb.pro/post/46826.html
m.jtbdb.pro/post/53157.html
m.jtbdb.pro/post/73351.html
m.jtbdb.pro/post/33351.html
m.jtbdb.pro/post/06408.html
m.jtbdb.pro/post/97997.html
m.jtbdb.pro/post/15535.html
m.jtbdb.pro/post/53931.html
m.jtbdb.pro/post/73911.html
m.jtbdb.pro/post/00404.html
m.jtbdb.pro/post/48220.html
m.jtbdb.pro/post/00880.html
m.jtbdb.pro/post/77333.html
m.jtbdb.pro/post/31155.html
m.jtbdb.pro/post/17957.html
m.jtbdb.pro/post/22424.html
m.jtbdb.pro/post/00408.html
m.jtbdb.pro/post/15395.html
m.jtbdb.pro/post/39759.html
m.jtbdb.pro/post/35775.html
m.jtbdb.pro/post/59391.html
m.jtbdb.pro/post/73155.html
m.jtbdb.pro/post/31951.html
m.jtbdb.pro/post/39939.html
m.jtbdb.pro/post/42208.html