C++ 关联容器(map 与 set)解析
一、关联容器的底层数据结构特性C++ 标准库中的map和set均属于关联容器,其底层实现基于红黑树(Red-Black Tree)—— 一种自平衡的二叉搜索树。这种数据结构具有以下核心特性: 有序性:元素按照键值的比较规则进行排序存储 自平衡性:通过特定的旋转和着色操作,保证树的高度始终保持在 O (log n) 级别 迭代效率:支持高效的顺序访问和范围查询 红黑树的平衡机制确保了所有基本操作(插入、删除、查找)都能在O(log n) 的时间复杂度内完成,这使得关联容器在需要频繁查找和有序遍历的场景中表现优异。 二、map 与 set 的存储机制差异虽然map和set共享相同的底层实现机制,但它们的存储方式存在本质区别: 特性 set map 存储内容 仅存储键(key) 存储键值对(key-value) 元素唯一性 键值唯一,不允许重复 键值唯一,不允许重复 访问方式 只能通过键访问元素 可通过键访问对应的值 模板参数 std::set<Key, Compare, Allocator> std::map<Key,...
C++ Map 容器
一、核心数据结构:红黑树C++ 标准库中std::map的底层实现采用红黑树(Red-Black Tree),这是一种自平衡的二叉搜索树(BST)。红黑树通过特定的规则保证树的高度始终维持在 O (log n) 级别,从而确保各种操作的高效性。 1具体见前一篇 C++ 标准库中的 set 容器 二、map 容器的核心组件2.1 迭代器实现std::map的迭代器是双向迭代器,其实现本质上是红黑树节点的指针封装,并提供了遍历树的操作。 2.2 内存管理机制std::map通常使用内存池(memory pool)或分配器(allocator)管理节点内存,而非频繁调用new和delete: 采用 slab 分配策略,预先分配一批节点内存 节点释放时不直接归还给系统,而是放入空闲列表 下次分配时优先从空闲列表获取,减少系统调用开销 三、核心操作实现3.1...
C++ 标准库中的 set 容器
一、set 容器的本质与底层数据结构C++ 标准库中的std::set是一种关联式容器,其核心特性是自动排序和唯一性。与序列式容器(如 vector、list)不同,set 中的元素是按照特定的排序规则进行存储的,这使得 set 具有高效的查找、插入和删除操作。 std::set的底层实现采用了红黑树(Red-Black Tree) 这一自平衡二叉搜索树数据结构。红黑树通过一系列规则保证了树的平衡性,从而确保了基本操作(插入、删除、查找)的时间复杂度均为 O (logN),其中 N 为树中元素的数量。 在 C++ 标准库中,红黑树的实现通常被封装在_Rb_tree类模板中,而 set 则是该类模板的一个特化应用,专门用于存储键值对中的键(在 set 中,键和值是同一个概念) 二、红黑树的结构解析2.1 红黑树节点的构成红黑树的每个节点通常包含以下几个部分: 键(key):即存储的元素值,用于节点间的比较 左孩子指针(left child):指向当前节点的左子节点 右孩子指针(right...
Leecode 0020. Valid Parentheses
20. Valid ParenthesesGiven a string s containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. An input string is valid if: Open brackets must be closed by the same type of brackets. Open brackets must be closed in the correct order. Every close bracket has a corresponding open bracket of the same type. 题目大意给定一个只包含 '('、')'、'{'、'}'、'[' 和...
Leecode 1047. Remove All Adjacent Duplicates In String
1047. Remove All Adjacent Duplicates In StringYou are given a string s consisting of lowercase English letters. A duplicate removal consists of choosing two adjacent and equal letters and removing them. We repeatedly make duplicate removals on s until we no longer can. Return the final string after all such duplicate removals have been made. It can be proven that the answer is unique. Example 1: 1234Input: s = "abbaca"Output: "ca"Explanation: For example, in...
Leecode 0155. Min Stack
155. Min StackDesign a stack that supports push, pop, top, and retrieving the minimum element in constant time. Implement the MinStack class: MinStack() initializes the stack object. void push(int val) pushes the element val onto the stack. void pop() removes the element on the top of the stack. int top() gets the top element of the stack. int getMin() retrieves the minimum element in the stack. You must implement a solution with O(1) time complexity for each function. 题目大意设计一个支持...
C++ 标准库中 pair 的实现原理与应用解析
一、pair 的模板定义与类型参数设计1.1 基本模板定义C++ 标准库中的std::pair是一个模板类,用于存储两个异构对象作为一个单元。其基本定义如下: 123456789101112namespace std { template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type; T1 first; T2 second; // 构造函数及其他成员函数... };} 这个定义展示了pair的核心特征: 两个模板类型参数T1和T2,分别指定了两个成员的类型 公开的成员变量first和second,分别存储第一个和第二个元素 类型别名first_type和second_type,用于获取元素类型 1.2 类型推导机制在 C++11...
C++ 短字符串优化(SSO)
引言:字符串性能的关键挑战在 C++ 开发中,std::string作为最常用的容器之一,其性能直接影响整体程序效率。传统字符串实现采用动态内存分配策略,无论字符串长度如何,都需要在堆上分配空间,这会带来: 内存分配 / 释放的开销 缓存局部性不佳 小字符串场景下的效率低下 短字符串优化(Short String Optimization, SSO)正是为解决这些问题而生的关键技术,已成为现代 C++ 标准库(如 libstdc++、libc++)的标配实现策略。 一、SSO 的核心原理1.1 传统字符串实现的缺陷传统std::string(C++11 之前)通常采用 "胖指针" 结构: 123456// 传统字符串实现(概念模型)struct String { char* data; // 指向堆内存的指针 size_t length; // 字符串长度 size_t capacity; // 已分配容量}; 这种结构对短字符串极不友好,例如存储 "hello"...
C++ 写时复制 (Copy-on-Write)
一、写时复制核心概念写时复制 (简称 COW) 是一种资源管理优化技术,其核心思想是:当多个对象需要共享同一资源时,直到其中一个对象需要修改资源前,都不需要真正复制资源,仅在修改时才创建资源的私有副本。 这种机制通过延迟复制操作,减少了不必要的内存分配和数据拷贝,从而提高程序性能,尤其适用于: 频繁复制但很少修改的场景 内存资源宝贵的环境 大型数据结构的共享访问 二、写时复制实现三要素1. 共享数据存储需要一个独立的共享数据结构,存储实际的数据内容。 1234567template <typename T>struct SharedData { T* data; // 实际数据指针 size_t size; // 数据大小 size_t ref_count; // 引用计数 // 其他元数据...}; 2. 引用计数机制通过引用计数跟踪当前有多少对象共享该资源: 当新对象共享资源时,引用计数 + 1 当对象销毁或不再共享时,引用计数 - 1 当引用计数为 0...
Leecode 0227. Basic Calculator II
227. Basic Calculator IIGiven a string s which represents an expression, evaluate this expression and return its value. The integer division should truncate toward zero. You may assume that the given expression is always valid. All intermediate results will be in the range of [-231, 231 - 1]. Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval(). 题目大意给定一个字符串 s 表示一个算术表达式,要求计算该表达式的值并返回。表达式仅包含非负整数、+、-、*、/...
Leecode 0150. Evaluate Reverse Polish Notation
150. Evaluate Reverse Polish NotationYou are given an array of strings tokens that represents an arithmetic expression in a Reverse Polish Notation. Evaluate the expression. Return an integer that represents the value of the expression. Note that: The valid operators are '+', '-', '*', and '/'. Each operand may be an integer or another expression. The division between two integers always truncates toward zero. There will not be any division by zero. The input...
Leecode 0071. Simplify Path
71. Simplify PathYou are given an absolute path for a Unix-style file system, which always begins with a slash '/'. Your task is to transform this absolute path into its simplified canonical path. The rules of a Unix-style file system are as follows: A single period '.' represents the current directory. A double period '..' represents the previous/parent directory. Multiple consecutive slashes such as '//' and '///' are treated as a single slash...
C++ 中->和*运算符重载的全面解析
引言在 C++ 编程中,->和*运算符最初设计用于指针操作。然而,通过运算符重载机制,我们可以让自定义类型也支持这些操作,从而实现类似指针的行为,同时添加额外功能。 一、为什么需要重载->和*运算符1. 扩展指针功能的核心需求原生指针 (T*) 虽然简洁高效,但存在明显局限: 无法自动管理资源生命周期 缺乏访问控制机制 不支持额外的调试信息 不能实现代理或间接访问模式 通过重载->和*,我们可以创建 "智能指针" 或 "代理对象",在保持指针操作语法的同时,添加所需功能。 2. 关键应用场景资源自动管理智能指针通过运算符重载实现自动内存管理: 1234567891011121314151617181920// 简化的shared_ptr实现思路template<typename T>class SharedPtr {private: T* ptr; int* ref_count; void release() { if...
C++ 中的 Pimpl 模式:封装实现细节的艺术
一、什么是 Pimpl 模式?Pimpl 模式(Pointer to Implementation)通过私有指针隔离类的接口与实现,是 C++ 封装原则的高级应用。其核心目标为:隐藏实现细节、提升编译效率、保障二进制兼容及简化接口。 二、Pimpl 模式的实现机制传统类设计将接口与实现混在头文件,Pimpl 模式则通过以下方式分离: 头文件声明含私有指针的公共接口类 源文件定义包含实现细节的实现类 接口类借指针间接访问实现类成员 传统设计 Pimpl 模式设计 接口与实现一体 接口类仅含指针 头文件暴露所有细节 头文件隐藏实现 实现变更需重编译所有依赖 仅需重编译源文件 三、Pimpl 模式的代码实现3.1 头文件(widget.h)123456789101112131415161718192021222324252627#include <memory>#include <string>// Widget 类是对外暴露的公共接口类class Widget {public: // 构造函数,用于创建...
C++ 单例模式的四种自动释放方式详解
导言单例模式的自动释放是解决资源泄漏问题的关键,不同场景下可以选择不同的实现方式。下面详细解析四种典型的自动释放机制,包括其实现原理、代码示例及适用场景。 一、方式一:利用栈对象的生命周期进行管理实现原理利用栈对象 "出作用域自动析构" 的特性,将单例指针存储在栈对象中,当栈对象被销毁时,自动释放单例资源。 代码实现12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758#include <iostream>class Singleton {private: // 私有构造函数 Singleton() { std::cout << "Singleton 构造函数被调用" << std::endl; } // 私有析构函数 ~Singleton() { ...
友元机制与其他访问控制机制的区别与联系
导言在 C++ 面向对象编程中,访问控制机制是实现封装性的核心手段。友元机制和继承是两种主要的访问控制方式,它们既有联系又有显著区别。 一、本质区别 特性 友元机制 继承机制 关系性质 单向的 "授权" 关系 父子间的 "派生" 关系 访问目的 临时突破封装边界 实现代码复用与扩展 关系方向 非对称(A 是 B 的友元≠B 是 A 的友元) 可传递(间接继承) 生命周期 编译期静态确定 运行期动态体现(多态) 代码耦合度 低到中等(仅需声明) 高(子类依赖父类实现) 二、访问权限差异1. 友元机制的访问特点 可以直接访问所有私有成员(private)和保护成员(protected) 无需通过类的接口(public 成员)进行访问 访问权限是单向且不可传递的 示例: 12345678910111213141516171819class A {private: int x; friend class B; // B可以直接访问A的私有成员};class B...
Leecode 0225. Implement Stack using Queues
225. Implement Stack using QueuesImplement a last-in-first-out (LIFO) stack using only two queues. The implemented stack should support all the functions of a normal stack (push, top, pop, and empty). Implement the MyStack class: void push(int x): Pushes element x to the top of the stack. int pop(): Removes the element on the top of the stack and returns it. int top(): Returns the element on the top of the stack. boolean empty(): Returns true if the stack is empty, false...
Leecode 0232. Implement Queue using Stacks
232. Implement Queue using StacksImplement a first in first out (FIFO) queue using only two Stacks. The implemented queue should support all the functions of a normal queue (push, peek, pop, and empty). Implement the MyQueue class: void push(int x) Pushes element x to the back of the queue. int pop() Removes the element from the front of the queue and returns it. int peek() Returns the element at the front of the queue. boolean empty() Returns true if the queue is empty, false...
Leecode 1048. Longest String Chain
1048. Longest String ChainYou are given an array of words where each word consists of lowercase English letters. wordA is a predecessor of wordB if and only if we can insert exactly one letter anywhere in wordA without changing the order of the other characters to make it equal to wordB. For example, "abc" is a predecessor of "abac", while "cba" is not a predecessor of "bcad".A word chain is a sequence of words [word1, word2, ..., wordk] with k...
C++ 友元机制深度解析:突破封装边界的艺术
一、友元机制概述C++ 面向对象编程中,封装通过public、private和protected确保数据安全。但特定场景需突破封装,友元(friend)机制由此诞生,它允许指定外部函数或类访问当前类私有、保护成员,同时维持其他实体的封装性,核心价值在于受控突破封装、支持高效数据访问及解决权限问题。 二、友元函数详解2.1 友元函数的定义与实现友元函数是类中声明为friend的非成员函数,可访问类所有成员。如Circle类中,calculateArea和isSameSize通过声明为友元,直接访问私有成员计算圆面积和比较大小。 1234567891011121314151617181920#include <iostream>#include <cmath>class Circle {private: double radius; const double PI = 3.1415926;public: Circle(double r) : radius(r) {} friend double...