算法文章中涉及的若干基础类的主要API

本文记述了笔者所写算法文章中涉及的若干基础类的主要 API(部分参考 算法(第4版) 实现,包括顺序存储结构、基础类的包装类、链式栈和队列、随机数、标准输入输出等。

◆ Array<_T>

简单的顺序存储容器

公有函数 说明
Array(int = 0) 按指定大小构建
Array(initializer_list<_T>) 按初始化列表构建
Array(Array const&) 拷贝构造
Array(Array &&) 移动构造
~Array() 析构
operator=(Array const&) 拷贝赋值
operator=(Array &&) 移动赋值
int size() 容量大小
_T & operator[] 下标访问
_T * begin() 支持基于范围的循环
_T * end() 支持基于范围的循环
...

◆ Comparable<_T>

实现了此接口的类实例可用于基于比较的操作,如排序。

公有函数 说明
int compare_to(_T const&) 按照指定值构造

◆ Boolean

bool 类型的包装类, 实现 Comparable<> 接口

公有函数 说明
Boolean(bool = false) 按照指定值构造
bool value() 基础类型值
int compare_to(Boolean const&) 与另一个 Boolean 实例相比
friend bool operator==(Boolean const&, Boolean const&) 支持 == 操作
friend bool operator!=(Boolean const&, Boolean const&) 支持 != 操作
...

◆ Character

char 类型的包装类, 实现 Comparable<> 接口

公有函数 说明
Character(char = ' ') 按照指定值构造
char value() 基础类型值
int compare_to(Character const&) 与另一个 Character 实例相比
friend bool operator==(Character const&, Character const&) 支持 == 操作
friend bool operator!=(Character const&, Character const&) 支持 != 操作
...

◆ Double

double 类型的包装类, 实现 Comparable<> 接口

公有函数 说明
Double(double = 0.0) 按照指定值构造
double value() 基础类型值
int compare_to(Double const&) 与另一个 Double 实例相比
friend bool operator==(Double const&, Double const&) 支持 == 操作
friend bool operator!=(Double const&, Double const&) 支持 != 操作
...

◆ Integer

int 类型的包装类, 实现 Comparable<> 接口

公有函数 说明
Integer(int = 0) 按照指定值构造
int value() 基础类型值
int compare_to(Integer const&) 与另一个 Integer 实例相比
friend bool operator==(Integer const&, Integer const&) 支持 == 操作
friend bool operator!=(Integer const&, Integer const&) 支持 != 操作
...

◆ Long

long 类型的包装类, 实现 Comparable<> 接口

公有函数 说明
Long(long = 0L) 按照指定值构造
long value() 基础类型值
int compare_to(Long const&) 与另一个 Long 实例相比
friend bool operator==(Long const&, Long const&) 支持 == 操作
friend bool operator!=(Long const&, Long const&) 支持 != 操作
...

◆ Node<_T>

简单的单向链式存储节点

公有属性 说明
_T item 节点数据
Node * next 指向后续节点的指针

◆ Biway_Node<_T>

简单的双向链式存储节点

公有属性 说明
_T item 节点元素
Biway_Node * prev 指向前续节点的指针
Biway_Node * next 指向后续节点的指针

◆ Stack<_T>

链式栈

公有函数 说明
Stack() 构造空栈
Stack(Stack const&) 拷贝构造栈
bool is_empty() 栈中是否还有元素
int size() 栈中元素个数
void push(_T) 元素入栈
_T pop() 元素出栈
_T peek() 查看栈顶元素

◆ Queue<_T>

链式队列

公有函数 说明
Queue() 构造空队列
Queue(Queue const&) 拷贝构造队列
bool is_empty() 队列中是否还有元素
int size() 队列中元素个数
void enqueue(_T) 元素入队
_T dequeue() 元素出队
_T peek() 查看队首元素

◆ Std_In

从标准输入中读出数据的函数库

公有函数 说明
static bool is_empty() 标准输入中是否还有内容
static int read_int() 从标准输入中读出一个 int 类型数据
static Array<int> read_ints() 把标准输入中的剩余内容都作为 int 类型数据读到一个 Array 中
static double read_double() 从标准输入中读出一个 double 类型数据
static Array<double> read_doubles() 把标准输入中的剩余内容都作为 double 类型数据读到一个 Array 中
static string read_string() 从标准输入中读出一个 string 类型数据
static Array<string> read_strings() 把标准输入中的剩余内容都作为 string 类型数据读到一个 Array 中
static string read_line() 从标准输入中读出一行中剩余的内容
static string read_all() 把标准输入中的剩余内容都读出到一个 string 类型数据中
...

◆ Std_Out

向标准输出中写入数据的函数库

公有函数 说明
static void printf(char const*, ...) 可变参数的格式化输出
static void println() 输出换行
template <class _T> static void print(_T const&) 输出某个类型的数据
template <class _T> static void println<_T>(_T const&) 换行输出某个类型的数据
...

◆ Std_Random

随机数的函数库

公有函数 说明
static void set_seed(long) 设置随机生成器的种子
static double random() [0, 1) 之间的随机实数
static int uniform(int N) [0, N) 之间的随机整数
static long uniform(long N) [0, N) 之间的随机长整数
static double uniform(double lo, double hi) [lo, hi) 之间的随机实数
static int uniform(int lo, int hi) [lo, hi) 之间的随机整数
static long uniform(long lo, long hi) [lo, hi) 之间的随机长整数
static bool bernoulli(double = 0.5) 伯努利分布,true 的概率 p 默认为 0.5
static double gaussian(double m = 0.0, double s = 1.0) 正态分布,期望值 m 默认为 0,标准差 s 默认为 1
static int poisson(double m = 0.5) 泊松分布,μ 分布参数默认为 0.5
static int geometric(double p = 0.5) 几何分布,p 分布参数默认为 0.5
static int discrete(Array<double> const& a) 离散分布,以 a[i] 的概率返回 i
template <class _T> static void shuffle(Array<_T> & a) 随机打乱数组
...

◆ Stopwatch

计时器

公有函数 说明
Stopwatch() 启动计时
double elapsed_time() 自启动计时以来经过的时间,单位秒
posted @ 2024-03-22 10:49  green-cnblogs  阅读(5)  评论(0编辑  收藏  举报