这一系列文章的目的是在学习了C++基础后,继续补充一些C++基础和进阶的知识点,包括C++11的相关内容。
以C++11标准为基础。
C++网站:http://www.cplusplus.com/reference/
1 ctime
//包含头文件
#include <ctime>
//获取系统时间
clock_t time = clock();
//获取系统时间对应的秒数
int sec = time/CLOCKS_PER_SEC;
2 string
string value transform
#include <string>
int std::stoi(const string& str, size_t* idx = 0, int base = 10);
int std::stol(const string& str, size_t* idx = 0, int base = 10);
int std::stoul(const string& str, size_t* idx = 0, int base = 10);
int std::stoll(const string& str, size_t* idx = 0, int base = 10);
int std::stoull(const string& str, size_t* idx = 0, int base = 10);
int std::stof(const string& str, size_t* idx = 0);
int std::stod(const string& str, size_t* idx = 0);
int std::stold(const string& str, size_t* idx = 0);
// 以下方法 仅保留6位小数
string std::to_string (int val);
string std::to_string (long val);
string std::to_string (long long val);
string std::to_string (unsigned val);
string std::to_string (unsigned long val);
string std::to_string (unsigned long long val);
string std::to_string (float val);
string std::to_string (double val);
string std::to_string (long double val);
Iterators operate
// 正向迭代器
iterator begin() noexcept;
iterator end() noexcept;
// 反向迭代器
iterator begin() noexcept;
iterator end() noexcept;
// 常量正向迭代器
const_iterator cbegin() noexcept;
const_iterator cend() noexcept;
// 常量反向迭代器
const_iterator crbegin() noexcept;
const_iterator crend() noexcept;
size & resize & empty & clear
//
size_t size() const noexcept;
size_t length() const noexcept;
void resize (size_t n);
void resize (size_t n, char c); // 空闲位置用字符 c 补充
void clear() noexcept;
bool empty() const noexcept;
Element operate
//返回引用,可作为左值修改
char& operator[] (size_t pos);
char& at (size_t pos);
char& back();
char& front();
swap & assign & insert & erase & replace
void push_back (char c);
void pop_back(); //删除最后一个字符
void swap (string& str); //交换
//以下有较多重载函数
string& append (const string& str); //
string& assign (const string& str); //替换
string& insert (size_t pos, const string& str); //插入
string& erase (size_t pos = 0, size_t len = npos);
string& replace (size_t pos, size_t len, const string& str);
copy & find & substr & compare
const char* c_str() const noexcept;
const char* data() const noexcept;
size_t copy (char* s, size_t len, size_t pos = 0) const;
// 一个字符 搜索第一次出现的位置find 和 搜索最后一次出现位置 rfind
size_t find (const string& str, size_t pos = 0) const noexcept; //pos 是搜索起始位置
size_t find (const char* s, size_t pos = 0) const;
size_t rfind (const string& str, size_t pos = npos) const noexcept;
size_t rfind (const char* s, size_t pos = npos) const;
// 字符串任意字符 搜索第一次的位置 和 搜索最后一次出现位置
size_t find_first_of (const string& str, size_t pos = 0) const noexcept;
size_t find_first_of (const char* s, size_t pos = 0) const;
size_t find_last_of (const string& str, size_t pos = npos) const noexcept;
size_t find_last_of (const char* s, size_t pos = npos) const;
size_t find_first_not_of (const string& str, size_t pos = 0) const noexcept;
size_t find_first_not_of (const char* s, size_t pos = 0) const;
size_t find_last_not_of (const string& str, size_t pos = npos) const noexcept;
size_t find_last_not_of (const char* s, size_t pos = npos) const;
// 子串
string substr (size_t pos = 0, size_t len = npos) const;
//比较,重载较多,也可比较其中一部分字符串
int compare (const string& str) const noexcept;
3 cmath
gamma函数
double tgamma ( double x);
float tgamma ( float x);
long double tgamma (long double x);
double tgamma (T x); // additional overloads for integral types
- 举个栗子
/* tgamma example */
#include <stdio.h> /* printf */
#include <math.h> /* tgamma */
int main ()
{
double param, result;
param = 0.5;
result = tgamma (param);
printf ("tgamma(%f) = %f\n", param, result );
return 0;
}
- 输出
tgamma (0.500000) = 1.772454
取整
// floor向下取整
// ceil向上取整
// round四舍五入
// 返回的是double,不是int
cout<<floor(4.4)<<endl;//4
cout<<floor(4.5)<<endl;//4
cout<<ceil(4.4)<<endl;//5
cout<<ceil(4.5)<<endl;//5
cout<<round(4.4)<<endl;//4
cout<<round(4.5)<<endl;//5
4 random
正态分布
std::default_random_engine generator; //定义引擎
std::normal_distribution<double> distribution(均值,标准差); //定义正态分布
double number = distribution(generator); //产生分布值
- 举个栗子
// normal_distribution
#include <iostream>
#include <string>
#include <random>
int main()
{
const int nrolls=10000; // number of experiments
const int nstars=100; // maximum number of stars to distribute
std::default_random_engine generator;
std::normal_distribution<double> distribution(5.0,2.0);
int p[10]={};
for (int i=0; i<nrolls; ++i) {
double number = distribution(generator);
if ((number>=0.0)&&(number<10.0)) ++p[int(number)];
}
std::cout << "normal_distribution (5.0,2.0):" << std::endl;
for (int i=0; i<10; ++i) {
std::cout << i << "-" << (i+1) << ": ";
std::cout << std::string(p[i]*nstars/nrolls,'*') << std::endl;
}
return 0;
}
- 输出
normal_distribution (5.0,2.0):
0-1: *
1-2: ****
2-3: *********
3-4: ***************
4-5: ******************
5-6: *******************
6-7: ***************
7-8: ********
8-9: ****
9-10: *
5 algorithm
sort & reverse & rotate
// 排序原理:快速排序
// 比较函数comp:小于为升序排列,大于为降序排列,不输入比较函数,默认为升序排列
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
// e.g.
bool myfunction (int i,int j)
{
return (i<j);
}
std::sort (myvector.begin(), myvector.end(), myfunction);
// 排序原理:middle之前的元素,为,按顺序,升序排列最小的n个元素,或降序排列最大的n个元素;middle之后没有特定顺序
// 比较函数comp作用同上
void partial_sort (RandomAccessIterator first, RandomAccessIterator middle, RandomAccessIterator last, Compare comp);
// 倒序排列
void reverse (BidirectionalIterator first, BidirectionalIterator last);
// 旋转容器,以middle为第一个元素,之前元素,有序移动到尾部
ForwardIterator rotate (ForwardIterator first, ForwardIterator middle, ForwardIterator last);
//e.g. 1 2 3 4 5 6 7 8 9 ---> 4 5 6 7 8 9 1 2 3
min & max & min_element & max_element
// 比较大小
// 比较函数comp:返回值为 a是否小于b
const T& min (const T& a, const T& b, Compare comp);
// 比较函数comp:返回值为 a是否小于b,是的,还是“是否小于“
const T& max (const T& a, const T& b, Compare comp);
// 找最大/最小值的位置,返回的是指向该值的指针
ForwardIterator min_element (ForwardIterator first, ForwardIterator last, Compare comp);
ForwardIterator max_element (ForwardIterator first, ForwardIterator last, Compare comp);
merge
// 合并两个排序序列
// result 是合并到的容器的某个位置,一般是起始位置
OutputIterator merge (InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2,
OutputIterator result, Compare comp);
copy
// 复制first到last范围的内容 到 起始位置result
OutputIterator copy (InputIterator first, InputIterator last, OutputIterator result);
// 有选择地复制
OutputIterator copy_if (InputIterator first, InputIterator last,
OutputIterator result, UnaryPredicate pred);
//e.g.
std::vector<int> foo = {25,15,5,-5,-15};
std::vector<int> bar (foo.size());
auto it = std::copy_if (foo.begin(), foo.end(), bar.begin(), [](int i){return !(i<0);} );
// 倒着来
BidirectionalIterator2 copy_backward (BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result);
move & swap
// 移动到result后,原位置的元素,处于未指定但有效状态
OutputIterator move (InputIterator first, InputIterator last, OutputIterator result);
BidirectionalIterator2 move_backward (BidirectionalIterator1 first,
BidirectionalIterator1 last,
BidirectionalIterator2 result);
// C++11之后,swap交换基于move
void swap (T& a, T& b);
void swap (T (&a)[N], T (&b)[N]);
//交换一定范围的值
ForwardIterator2 swap_ranges (ForwardIterator1 first1, ForwardIterator1 last1, ForwardIterator2 first2);
replace & fill & generate & remove
// 单值替换单值:将范围内的 old_value 替换成 new_value
void replace (ForwardIterator first, ForwardIterator last, const T& old_value, const T& new_value);
// 单值替换条件值(多值)
void replace_if (ForwardIterator first, ForwardIterator last, UnaryPredicate pred, const T& new_value );
// 范围内填充单值 val
void fill (ForwardIterator first, ForwardIterator last, const T& val);
OutputIterator fill_n (OutputIterator first, Size n, const T& val); //返回最后一个填充值的下一个位置(C++11)
// 范围内填充生成值
void generate (ForwardIterator first, ForwardIterator last, Generator gen);
//e.g.
// function generator:
int RandomNumber () { return (std::rand()%100); }
std::vector<int> myvector (8);
std::generate (myvector.begin(), myvector.end(), RandomNumber);
// 移除范围内的值
// 坑:这是假移除,符合移除要求的值被移动到末尾,最后返回的位置指向 尾部 所有移除元素的第一个元素位置
ForwardIterator remove (ForwardIterator first, ForwardIterator last, const T& val);
find
// 测试所有元素满足条件
bool all_of (InputIterator first, InputIterator last, UnaryPredicate pred);
// 测试有元素满足条件
bool any_of (InputIterator first, InputIterator last, UnaryPredicate pred);
// 测试所有元素不满足条件
bool none_of (InputIterator first, InputIterator last, UnaryPredicate pred);
// 对每个元素执行一次函数fn
Function for_each (InputIterator first, InputIterator last, Function fn);
// 找到元素第一次出现的位置
InputIterator find (InputIterator first, InputIterator last, const T& val);
// 按条件第一次出现的位置
InputIterator find_if (InputIterator first, InputIterator last, UnaryPredicate pred);
// 出现次数
count (InputIterator first, InputIterator last, const T& val);
count_if (InputIterator first, InputIterator last, UnaryPredicate pred);
// 在1号范围内,第一次出现2号范围内元素的位置, 可自定义pred 判断是否符合匹配情况
ForwardIterator1 search (ForwardIterator1 first1, ForwardIterator1 last1,
ForwardIterator2 first2, ForwardIterator2 last2,
BinaryPredicate pred);
6 thread & future
(新开一个博客写多线程…)