![](https://img-blog.csdnimg.cn/20201014180756738.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
C++ Primer
小飞将
一切过往,皆为序章
展开
-
C++实现内存池Memory Manager,并可以合并空闲内存空间
这个内存池的设计思想是使用链表(link list)来管理用户从内存池里开辟出的memory block,allocate操作从内存池中取出所需size的memory block,它不断从未使用的空间中通过指针偏移从已经分配的内存池中取内存,而无需直接使用malloc或者new。这个内存池的缺陷是无法整理不相邻的内存库,最后内存可能碎片化,这需要复杂的内存分配策略,例如最佳适配(best fit)、最差适配(worst fit)或伙伴系统(buddy system)。原创 2023-11-28 20:59:26 · 208 阅读 · 0 评论 -
基于C++ 11的简单线程池实现
这个线程池的实现十分简单,缺陷是没有超时机制。测试main.cpp。原创 2023-11-16 21:21:33 · 187 阅读 · 0 评论 -
如何正确的在C++中实现单例SingleTone模式
单例模式保证该类始终只有一个实例对象,以表现唯一资源的设计(不可拷贝)。原创 2023-10-11 19:52:53 · 119 阅读 · 0 评论 -
C++中如何描述table表格类型数据结构
C++实现table类型数据结构原创 2023-09-26 19:35:39 · 489 阅读 · 0 评论 -
disable MSVC/GCC编译warning
【代码】disable MSVC编译warning。原创 2023-09-25 10:59:22 · 72 阅读 · 0 评论 -
std::thread传入类成员函数指针报错failed to specialize function template ‘unknown-type invoke
这里使用一个myclass实例c放入thread构造函数,以指定使用的函数指针是c所属。如果在类内部创建线程函数,并且函数指针是类成员函数指针,则需要传入this指针。原创 2023-09-22 11:04:58 · 233 阅读 · 0 评论 -
基于C++标准库的整形、浮点数随机数生成工具,以及从已有集合中随机取出一个
【代码】基于C++标准库的整形、浮点数随机数生成工具,以及从已有集合中随机取出一个。原创 2023-08-30 14:15:57 · 87 阅读 · 0 评论 -
C++ 设置等待超时功能
【代码】C++ 设置等待超时功能。原创 2022-10-26 20:39:36 · 1779 阅读 · 0 评论 -
C++多线程的使用,返回值和加锁
使用C++多线程可以进行并行计算,提高执行速率。在创建thread对象时,要注意如果返回参数是引用类型,如果不使用std::ref前缀,会发生:Error C2672 'std::invoke': no matching overloaded function found;Error C2893 Failed to specialize function template 'unknown-type std::invoke(_Callable &&,_Types &&.原创 2022-04-13 17:43:09 · 1764 阅读 · 1 评论 -
C++多线程的基本使用
在这里编写一个程序,后台线程不断更新天气温度信息,并打印:#include <thread>#include <map>#include <chrono>using namespace std::chrono_literals;void RefreshForecase(std::map<std::string, int> forecaseMap) { while (true) { for (auto &i : forec原创 2021-10-28 20:46:03 · 205 阅读 · 0 评论 -
virtual函数和虚函数表vtable
example:#include <iostream>class B{public: virtual void bar(); virtual void qux();};void B::bar(){ std::cout << "This is B's implementation of bar" << std::endl;}void B::qux(){ std::cout << "This is B's implemen原创 2020-11-29 12:13:07 · 341 阅读 · 0 评论 -
构造子类时,不可以在子类构造函数初始化列表中直接初始化父类的成员变量
问题class Base{public: int m_id; Base(int id=0) : m_id{ id } {} int getId() const { return m_id; }}; // 正确的写法class Derived: public Base{public: double m_cost; Derived(double cost=0.0) : m_cost{ cost } {}原创 2020-11-26 19:03:28 · 637 阅读 · 0 评论 -
自定义Shared_ptr和Unique_ptr,引用计数和内存管理
Shared_ptr.h#pragma oncetemplate <typename T>class Shared_ptr {public: Shared_ptr(); Shared_ptr(T *p); Shared_ptr(const Shared_ptr &sp); Shared_ptr &operator=(const ...原创 2019-11-16 17:11:14 · 300 阅读 · 0 评论 -
使用C++随机数引擎和分布类对象设计老虎机
随机数库random中随机数引擎random-number engins和随机数分布类random-number distribution。param_gen.h#pragma once#include <random>class Random {public: Random(unsigned min, unsigned max) : min(min...原创 2019-11-16 13:04:23 · 412 阅读 · 0 评论 -
DebugDelete 可调用对象模板
class DebugDelete {public: DebugDelete(ostream &os = cerr) : m_os(os) {} template <typename T> void operator()(T *ptr) { m_os << "Delete the point" << endl;...原创 2019-11-09 22:42:48 · 121 阅读 · 0 评论 -
16.1.3 使用容器size_type或者迭代器来遍历容器
#include <iostream>#include <vector>#include <string>using namespace std;template <typename T>void print(const T& c) { for (typename T::size_type i = 0; i < c...原创 2019-11-08 23:34:42 · 205 阅读 · 0 评论 -
C++ Template
dd原创 2019-11-03 09:57:06 · 104 阅读 · 0 评论 -
16.16 Vec template class
Vec.h#pragma once#include <string>#include <memory>template <typename T>class Vec{public: Vec(); Vec(const Vec<T> &); Vec &operator=(const Vec<T>...原创 2019-11-02 17:49:53 · 113 阅读 · 0 评论 -
16.14 编写Screen模板
Screen.h#pragma once#include <iostream>#include <string>template <typename pos>class Screen {public: Screen() = default; Screen(pos ht, pos wd) : content(ht * wd,...原创 2019-11-02 17:18:11 · 149 阅读 · 0 评论 -
16.1 Blob and BlobPtr class template
#include <vector>#include <memory>#include <stdexcept>#include <string>template <typename T> class Blob {public: typedef T value_type; typedef typename std:...原创 2019-11-01 00:33:54 · 179 阅读 · 0 评论 -
16.4 compare and find function template
function_template.htemplate <typename T>T compare(const T &v1, const T &v2) { if (v1 < v2) return v2; if (v2 < v1) return v1; return v1;}template <typename Itr,...原创 2019-10-29 23:07:37 · 126 阅读 · 0 评论 -
15.9 文本查询程序再探(继承)
本节中使用类的继承方式重写了文本查询程序,支持多种查询策略:或,与,非。其中Query是提供给用户使用的类,含有两个接口:eval和rep,eval用于查找对应的单词出现的文本,rep用于输出用户指定的查询内容。Query_base是抽象基类,WordQuery是普通查询策略,NotQuery是取非查询策略。对于双目查询策略,定义了一个新的抽象基类BinaryQuery,AndQuery和O...原创 2019-10-27 21:03:11 · 369 阅读 · 1 评论 -
15.30 Basket类实现
Basket class:Basket.h#pragma once#include <iostream>#include <memory>#include <set>#include "Quote.h"class Basket {public: void add_item(const Quote &); void add...原创 2019-10-25 00:51:31 · 381 阅读 · 0 评论 -
15.7 添加拷贝控制的Quote类
#pragma once#include <iostream>#include <string>class Quote {public: Quote() = default; Quote(const std::string &bookNo, double book_price); Quote(const Quote &);...原创 2019-10-23 23:50:53 · 191 阅读 · 0 评论 -
15.4 抽象基类Disc_quote
Quote class保持不变定义抽象基类Disc_quote继承自Quote,它代表一个折扣概念,由它的继承类代表不同的折扣策略。Disc_quote.h#pragma once#include "Quote.h"// 抽象基类class Disc_quote : public Quote {public: Disc_quote() = default; Disc_...原创 2019-10-21 20:25:41 · 193 阅读 · 0 评论 -
15 Quote and Bulk_quote
Quote classQuote.h#pragma once#include <iostream>#include <string>class Quote {public: Quote() = default; Quote(const std::string &bookNo, double book_price); std::s...原创 2019-10-20 21:49:05 · 197 阅读 · 0 评论 -
14.44 桌面计算器
使用函数指针#include <iostream>#include <string>#include <map>#include <functional>using namespace std;enum cal_type {addop, minusop, multiop, divop};map<const string, do...原创 2019-10-20 11:10:57 · 154 阅读 · 0 评论 -
14.81 列出特定长度单词的个数
#include <iostream>#include <fstream>#include <sstream>#include <vector>#include <string>#include <algorithm>using namespace std;class check_length{public...原创 2019-10-19 22:53:11 · 156 阅读 · 0 评论 -
14.37 replace_if equal
#include <iostream>#include <vector>#include <algorithm>class equ{public: equ(int val) : value(val) {} bool operator()(int newval) { return newval == value; ...原创 2019-10-19 21:57:24 · 112 阅读 · 0 评论 -
14.36 读取输入存入vector
class fun{public: fun(std::ostream &out = std::cout) :out(out), s(std::string()) {} std::string operator()(std::istream &in) { if (in >> s) { return ...原创 2019-10-19 21:44:43 · 189 阅读 · 0 评论 -
14.43 if-then-else function objects
class fun{public: fun(std::ostream &os = std::cout, char sep = '\n') : out(os), sep(sep) {}void operator()(int a, int b, int c) { out << a ? b:c << sep;}private: ...原创 2019-10-19 21:23:36 · 272 阅读 · 0 评论 -
14.1 Sales_item class
Sales_item.h#pragma once#include <iostream>#include <string>class Sales_item { friend std::ostream &operator<<(std::ostream &out, const Sales_item &s); frien...原创 2019-10-19 11:35:15 · 178 阅读 · 0 评论 -
13.6 重载引用函数
Foo class:#include <vector>#include <algorithm>using namespace std;class Foo {public: Foo() { std::cout << "default construct" << std::endl;} Foo(const Foo &...原创 2019-10-16 23:47:33 · 92 阅读 · 0 评论 -
13.6.3 为HasPtr定义移动构造函数
HasPtr class:#pragma once#include <iostream>#include <string>class HasPtr {public: HasPtr(const std::string &s, int a) : ps(new std::string(s)), i(a) { std::cout &...原创 2019-10-14 17:27:35 · 185 阅读 · 0 评论 -
13.5 String的实现以及章节总结
String class:String.h#include <iostream>#include <memory>#include <string>class String {public: String(); String(const char *); String(const String &); Str...原创 2019-10-13 19:15:29 · 183 阅读 · 0 评论 -
13.5 StrVec的实现
StrVec.h:#pragma once#include <string>#include <memory>class StrVec{public: StrVec(); StrVec(const StrVec &); StrVec &operator=(const StrVec &); ~StrVec();...原创 2019-10-11 21:06:43 · 223 阅读 · 1 评论 -
几种智能指针的使用
shared_ptr初始化方式:make_shared方法:template <class T, class... Args> shared_ptr<T> make_shared (Args&&... args);std::shared_ptr<int> foo = std::make_shared<int> (10...原创 2019-09-29 17:03:36 · 620 阅读 · 0 评论 -
12.6 使用动态分配的vector<int>输入和打印vector
// Demo.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include <iostream>#include <string>#include <vector>using namespace std;vector<i...原创 2019-09-29 15:14:20 · 350 阅读 · 0 评论 -
动态内存的使用
new运算符自由空间分配的内存是无名的,因此new无法为其分配的对象命名,而是发挥一个指向该对象的指针:int *ip = new int;string *sp = new string;默认情况下,动态分配的对象是默认初始化的,意味着int型的值是未定义的,类对象将使用默认构造函数进行初始化。可以使用直接初始化或者构造方式(使用圆括号)来初始化一个动态分配的对象:int *ip...原创 2019-09-29 14:48:38 · 132 阅读 · 0 评论 -
12.1 StrBlob
#include <iostream>#include <vector>#include <string>#include <memory>class StrBlob {public: typedef std::vector<std::string>::size_type size_type; StrBlob(...原创 2019-09-28 22:30:42 · 149 阅读 · 0 评论