随笔分类 - [01]【C/C++】
摘要:目录C++偶现问题备录1. 偶现问题源码2. 问题根因分析3. 修复问题源码 C++偶现问题备录 1. 偶现问题源码 源码示例如下: class KZNCalculationException : public std::exception { public: #ifdef KZN_LINUX KZ
阅读全文
摘要:std::vector 比较两个vector是否相等 1. 利用std::vector的operator==函数 1.1 示例代码 #include <vector> #include <iostream> int main() { std::vector<int> vector1, vector2
阅读全文
摘要:std::mem_fn 1. 不支持的场景 1.1 不支持全局函数 1.2 不支持类protected访问权限的成员(函数或数据) 1.3 不支持类private访问权限的成员(函数或数据) 2. 支持的场景 2.1 传入类对象 2.2 传入引用对象 2.3 传入右值 2.4 传入对象指针 2.5
阅读全文
摘要:std::bind 1. 参数重排序和引用传递(argument reordering and pass-by-reference) 1.1 示例代码 #include <iostream> #include <functional> void func(int n1, int n2, int n3
阅读全文
摘要:std::copy、std::copy_if、std::copy_n 【1】应用示例程序 #include <iostream> #include <functional> #include <vector> #include <algorithm> int main() { auto printV
阅读全文
摘要:【1】示例程序以及注释 1 #include <iostream> 2 #include <functional> 3 #include <vector> 4 #include <algorithm> 5 6 std::function<bool(int)> is_even_number = [](
阅读全文
摘要:3.2 exceed.cpp 1 程序清单 3.2 exceed.cpp // exceed.cpp --exceeding some integer limits #include <iostream> #include <limits> #define ZERO 0 // makes ZERO
阅读全文
摘要:3.1 limits.cpp 1 程序清单 3.1 limits.cpp // limits.cpp -- some integer limits #include <iostream> #include <climits> // use limits.h for older systems int
阅读全文
摘要:Modern C++ 模板通用工厂 1 简单应用示例 1.1 示例代码 #include <iostream> #include <string> using namespace std; class Shape { public: virtual void calc_area() { cout <
阅读全文
摘要:CRTP 1 CRTP 1.1 定义 英:The curiously recurring template pattern (CRTP) is a C++ idiom in which a class X derives from a class template instantiation usi
阅读全文
摘要:关于容器交集、并集、差集的算法 不喜欢无实物表演,秉承一贯风格,用代码说事。 1 示例代码 #include <set> #include <vector> #include <string> #include <algorithm> #include <iostream> int main() {
阅读全文
摘要:C++ 求时差的三种方法 【1】标准C库方式 示例代码 1 #include <ctime> 2 #include <iostream> 3 using namespace std; 4 5 void function() 6 { 7 int step = 100000000; 8 while (s
阅读全文
摘要:【1】内存分区模型 各种说法,但都有一定道理,只是划分角度或逻辑不同。 1、三部分 C++程序在执行时,将供用户使用内存大致划分为三个区域: (1)程序存储区:存放函数体的二进制代码,由操作系统进行管理; (2)静态存储区:静态存储区数据在程序开始就已经分配好了内存,执行过程中,它们所占的存储单元是
阅读全文
摘要:【1】应用示例 1 #include <vector> 2 #include <cassert> 3 #include <string> 4 #include <iostream> 5 using namespace std; 6 7 class String 8 { 9 public: 10 St
阅读全文
摘要:【1】设置VS2019 支持C++17标准步骤 体验了一个C++17的特性,找到设置VS2019 支持C++17标准的操作步骤,详细如下图: good good study, day day up. 顺序 选择 循环 总结
阅读全文
摘要:根据业务需求,在编码过程中,经常会遇到switch case表达式是字符串的场景,现支持如下。 【1】实现文件 支持实现的文件 1 #pragma once 2 3 #include <cstddef> 4 #include <cstdint> 5 #include <type_traits> 6
阅读全文
摘要:【1】复现问题 为了更精确的分析,先用最简单的示例复现此错误场景,代码如下: 1 #include <map> 2 #include <string> 3 4 struct Section 5 { 6 int id; 7 std::string code; 8 9 bool operator<(co
阅读全文
摘要:昨天给同事写了一个把自定义类型作为map中key值的示例,结果过了半个小时,同事反馈:不满足需求。 嗯哼?作为一个程序员,不满足需求那可就是BUG呀~ 不行,得尽快给处理一下。 【1】异常示例(不满足需求样例) 源代码如下: 1 #include <map> 2 #include <string>
阅读全文
摘要:【1】std::remove_if 今天突然有同事问下面这段代码应如何理解。 源码如下: void GStiffenerBlockRepoDefaultImpl::destory(int id) noexcept { m_stiffeners.erase(std::remove_if(std::be
阅读全文
摘要:ERROR:C2676 二进制“==”:“Student”不定义该运算符或到预定义运算符可接收的类型的转换 多次被同事问到此类错误,于此备录一下。 【1】复现问题 用最简单代码复现说明此问题,示例如下: 1 #include <iostream> 2 #include <map> 3 #includ
阅读全文