C++ 文件读取技术详解:get ()、getline () 与流提取运算符
一、核心函数原型与参数解析1.1 get () 函数家族get()函数是 C++ 中最基础的字符读取工具,有多个重载版本: 1234567891011// 读取单个字符(包含空白字符)int get();// 读取字符到指定缓冲区,最多n-1个字符,遇到分隔符停止istream& get(char* s, streamsize n);// 带自定义分隔符的版本istream& get(char* s, streamsize n, char delim);// 读取字符到字符对象istream& get(char& c); 关键特性: 不会跳过空白字符(空格、制表符、换行符等) 读取失败时返回 EOF(-1) 保留分隔符在输入流中(不提取) 1.2 getline () 函数getline()专为读取完整行设计,主要有两种形式: 123456789// 从输入流读取一行到字符数组istream& getline(char* s, streamsize n);// 带自定义分隔符的版本istream&...
CONST AUTO 的应用
一、const auto迭代器的适用场景1. 只读访问容器元素时当你只需要读取容器元素而不需要修改它们时,应该使用const auto声明迭代器: 1234567891011121314151617#include <vector>#include <iostream>void printData(const std::vector<int>& data) { // 函数参数为const引用,只能使用const迭代器 for (const auto it = data.begin(); it != data.end(); ++it) { std::cout << *it << " "; // 只读访问 // *it = 100; // 编译错误,不允许修改 } std::cout << std::endl;}int main() { ...
auto 关键字在 C++ 迭代器遍历中的应用
一、auto 关键字简化迭代器遍历在 C++11 之前,迭代器遍历代码往往冗长且可读性差: 类型声明冗长,增加代码量和阅读负担 容易出现类型不匹配错误 当容器类型改变时,需要修改所有相关迭代器声明 代码不够直观,隐藏了真正的业务逻辑 C++11 引入的auto关键字彻底改变了迭代器的使用方式,让我们能够完全摆脱冗长的传统迭代器声明。auto能够自动推导迭代器类型,使代码更加简洁、可读且不易出错。 1.1 基本迭代模式1234567891011121314151617181920#include <vector>#include <iostream>int main() { std::vector<int> numbers = {1, 2, 3, 4, 5, 6, 7, 8, 9}; // 使用auto声明迭代器 for (auto it = numbers.begin(); it != numbers.end(); ++it) { std::cout...
C++ 静态成员与单例模式:从基础到线程安全实现
一、静态成员的本质与特性1.1 静态数据成员:类级别的共享状态静态数据成员不属于任何对象实例,而是属于整个类,其内存空间在程序生命周期内唯一存在。 123456789101112131415161718192021222324252627#include <iostream>class Counter {private: // 静态数据成员声明:类内声明,类外定义 static int s_totalInstances; int m_id; // 非静态成员:每个对象拥有独立副本public: Counter() { m_id = ++s_totalInstances; // 每次创建对象自增计数 } int getId() const { return m_id; } static int getTotalInstances() { return s_totalInstances; }};//...
继承与多态
一、继承机制:代码复用的基石1.1 继承的概念与本质继承是面向对象编程中实现代码复用的核心机制,它允许一个类(派生类)获取另一个类(基类)的成员变量和成员函数。这种关系类似于现实世界中的 "is-a" 关系,例如 "狗是一种动物"。 在 C++ 中,继承通过类定义时的冒号语法实现: 12345678910111213class Animal {public: void speak() { std::cout << "The animal makes a sound." << std::endl; }};class Dog : public Animal {public: void bark() { std::cout << "The dog barks." << std::endl; }}; 在上述示例中,Dog...
C++中class与struct的异同点深度解析
一、基础语法差异1.1 访问修饰符差异 class 默认成员访问权限为 private 声明的成员变量与函数仅对类内部可见,需使用 public/protected 显式开放 struct 默认成员访问权限为 public 结构体成员对所有作用域开放,需使用 private/protected 显式限制 12345678910class MyClass { int privateVar; // 默认私有public: void publicFunc() {}};struct MyStruct { int publicVar; // 默认公有 void publicFunc() {}}; 1.2 成员函数特性 class 支持抽象方法(纯虚函数) 通过 = 0 定义的虚函数使类成为抽象类,如 virtual void func() = 0; struct 不支持抽象方法 ...
Leecode 1023. Camelcase Matching
1023. Camelcase MatchingGiven an array of strings queries and a string pattern, return a boolean array answer where answer[i] is true if queries[i] matches pattern, and false otherwise. A query word queries[i] matches pattern if you can insert lowercase English letters into the pattern so that it equals the query. You may insert a character at any position in pattern or you may choose not to insert any characters at all. Example 1: 12345Input: queries =...
Leecode 0825. Friends Of Appropriate Ages
825. Friends Of Appropriate AgesThere are n persons on a social media website. You are given an integer array ages where ages[i] is the age of the ith person. A Person x will not send a friend request to a person y (x != y) if any of the following conditions is true: age[y] <= 0.5 * age[x] + 7 age[y] > age[x] age[y] > 100 && age[x] < 100 Otherwise, x will send a friend request to y. Note that if x sends a request to y, y will not necessarily send a request to x. Also, a...
Leecode 0826. Most Profit Assigning Work
826. Most Profit Assigning WorkYou have n jobs and m workers. You are given three arrays: difficulty, profit, and worker where: difficulty[i] and profit[i] are the difficulty and the profit of the ith job, and worker[j] is the ability of jth worker (i.e., the jth worker can only complete a job with difficulty at most worker[j]). Every worker can be assigned at most one job, but one job can be completed multiple times. For example, if three workers attempt the same job that pays $1, then...
C++ 析构函数详解:原理、实现
一、析构函数基础概念1.1 析构函数的定义析构函数是 C++ 面向对象编程中用于对象销毁时进行资源清理的特殊成员函数。当对象生命周期结束时,析构函数会被自动调用,负责释放对象所占用的资源。 语法特征: 函数名与类名相同,前面加波浪号~ 没有返回值,也不指定 void 没有参数,因此无法重载 不能被显式调用(编译器自动调用) 123456789101112class MyClass {public: // 构造函数 MyClass() { std::cout << "构造函数被调用" << std::endl; } // 析构函数 ~MyClass() { std::cout << "析构函数被调用" << std::endl; }}; 1.2...
C++ 左值与右值:语义解析与实战应用
一、左值与右值的核心定义在 C++ 中,表达式根据其特性被分为左值(lvalue)和右值(rvalue),这一分类直接影响着变量的存储、引用绑定和资源管理。根据 C++17 标准( IO/IEC 14882:2017),左值是指 "可以取地址且具有身份 (identity) 的表达式",而右值则是 "非左值的表达式",通常是临时的、不具有持久身份的对象。 1.1 左值的核心特征左值具有以下关键特性: 可以通过&运算符获取地址S 具有持久性,在表达式结束后仍然存在 可以出现在赋值运算符的左侧 1234int x = 10; // x是左值int* ptr = &x; // 合法,左值可以取地址x = 20; // 合法,左值可以出现在赋值左侧 1.2 右值的核心特征右值具有以下关键特性: 不能通过&运算符获取地址 临时性,通常在表达式结束后销毁 不能出现在赋值运算符的左侧 1234int y = x + 5; // x + 5是右值// int* ptr = &(x +...
C++ 变量作用域与存储持续度完全解析
导言:变量类型的四维属性体系在 C++ 中,每个变量都具有四个核心属性,这些属性共同决定了变量的行为特征: 作用域 (Scope):变量在程序中可见的区域 存储持续度 (Storage Duration):变量在内存中存在的时间 链接属性 (Linkage):变量在不同编译单元间的可见性 生命周期 (Lifetime):变量从创建到销毁的时间段 一、作用域类型详解1.1 块作用域 (Block Scope)块作用域是由花括号{}界定的区域,包括函数体、循环体、条件语句体等。 12345678910void function() { int a = 0; // 块作用域开始 if (a == 0) { int b = 1; // 内部块作用域 // a和b在此可见 } // b的作用域结束 // 仅a可见,b不可见} // a的作用域结束 1.2 函数作用域 (Function...
Leecode 0611. Valid Triangle Number
611. Valid Triangle NumberGiven an integer array nums, return the number of triplets chosen from the array that can make triangles if we take them as side lengths of a triangle. Example 1: 123456Input: nums = [2,2,3,4]Output: 3Explanation: Valid combinations are: 2,3,4 (using the first 2)2,3,4 (using the second 2)2,2,3 Example 2: 12Input: nums = [4,2,3,4]Output: 4 题目大意给定一个整数数组 nums,返回数组中可作为三角形三条边长度的三元组数量。三角形三条边需满足:任意两边之和大于第三边。 核心解题思路:排序 + 双指针三角形三边的核心条件是:两边之和大于第三边。对于排序后的数组,可简化为: 设三条边为 a ≤...
Leecode 0189. 轮转数组
189. 轮转数组给定一个整数数组 nums,将数组中的元素向右轮转 k 个位置,其中 k 是非负数。 示例 1: 123456输入: nums = [1,2,3,4,5,6,7], k = 3输出: [5,6,7,1,2,3,4]解释:向右轮转 1 步: [7,1,2,3,4,5,6]向右轮转 2 步: [6,7,1,2,3,4,5]向右轮转 3 步: [5,6,7,1,2,3,4] 示例 2: 12345输入:nums = [-1,-100,3,99], k = 2输出:[3,99,-1,-100]解释: 向右轮转 1 步: [99,-1,-100,3]向右轮转 2 步: [3,99,-1,-100] 题目大意给定一个整数数组 nums 和非负整数 k,将数组元素向右轮转 k 个位置(即每个元素向右移动 k 位,末尾元素移动到开头)。要求尽可能优化时间和空间复杂度。 核心解题思路:三次反转法常规思路(如临时数组、多次右移)要么空间复杂度高(O (n)),要么时间复杂度高(O (nk))。三次反转法通过巧妙的反转操作,实现 O (n) 时间复杂度 + O (1)...
Leecode 0148. 排序链表
148. 排序链表给你链表的头结点 head ,请将其按 升序 排列并返回 排序后的链表 。 示例 1: 12输入:head = [4,2,1,3]输出:[1,2,3,4] 示例 2: 12输入:head = [-1,5,3,4,0]输出:[-1,0,3,4,5] 示例 3: 12输入:head = []输出:[] 题目大意给定链表的头节点 head,要求将链表按升序排列并返回排序后的链表头节点。 解题思路对于链表排序,最适合的高效算法是归并排序,原因如下: 归并排序的时间复杂度为 O (n log n),是链表排序的最优选择 链表的归并操作不需要像数组那样额外分配 O (n)...