这篇文章整理了常用STL函数(用途、参数的意义,必要时也会列出该函数所在的头文件)以及一些其它关于STL库的知识。
排序
有类别和头文件两种排序方式。类别排序,例如容器类,使用方式相近,因此把所有容器放在一起。排序均以英文版为准,例如容器
按照英文container
排序。相同类别的头文件排序,例如
char ch='qwqewqe'; // 单个字符;单引号;前面的值被覆盖,结果为“e”;
char *src="qwqewqe"; // 字符串;双引号
char text[] = "corner image"; // 字符串
substr
std::string images_path;
std::string name(images_path.substr(images_path[i].length( ) - 6, 2)); // (start index, length of substr)
images_path.compare(images_path.length()-name.length(),name.length(),name);
std::equal(name.begin(),name.end(),images_path.begin(),compare); // compare 是自定义的用于判定相等的函数
随着std::string
增大,push_back
函数的耗时会逐渐增加。
一、数值和数学计算
<cmath>
1. hypot
hypot(x,y);
hypot()函数是cmath标头的库函数,用于查找给定数字的斜边,接受两个数字并返回斜边的计算结果,即sqrt(x * x + y * y) 。 参数: x,y –要计算的斜边数(或sqrt(x * x + y * y) ). 返回值: double-它返回double值,该值是表达式sqrt(x * x + y * y)的结果 。
2. numeric
\#include <numeric>
// 求和 sum: interval and initial sum; orisum=0 means it's int and orisum=0.0 means float or double
accumulate(v.begin(), v.end(), orisum);
3. cstdlib
\#include <cstdlib>
// return a integ from 0 to RAND_MAX, which is defined in same header
rand();
round(value)
: round half away from zero
二、container
因为容器的函数用法和意义基本相似,因此介绍函数时一般不刻意区分是哪个容器的函数。
// 常见的容器
#incldue <list>
#include <map>
#include <vector>
1. erase
删除容器中的元素
erase(pos,n); // 删除从pos开始的n个字符,比如erase(0,1)就是删除第一个字符
erase(position); // 删除position处的一个字符(position是个string类型的迭代器)
erase(first,last);// 删除从first到last之间的字符(first和last都是迭代器)
1. iterator
#include <iterator>
// generate a iterator for container c and this iterator will add element to c. c must support push_back
back_inserter(c);
// generate a iterator for container c and this iterator will add element to c. c must support push_front
front_inserter(c);
// generate a iterator for container c and add elemnet before it
inserter(c,it);
2 initailize vector
here.
三、
1. clock_gettime
/* Get current value of clock CLOCK_ID and store it in TP. */
extern int clock_gettime (clockid_t __clock_id, struct timespec *__tp) __THROW;
0: CLOCK_REALTIME:系统实时时间,随系统实时时间改变而改变,即从UTC1970-1-1 0:0:0开始计时,中间时刻如果系统时间被用户改成其他,则对应的时间相应改变。 1: CLOCK_MONOTONIC:从系统启动这一刻起开始计时,不受系统时间被用户改变的影响。 2: CLOCK_PROCESS_CPUTIME_ID:本进程到当前代码系统CPU花费的时间。需要注意是不是进程开始到当前代码的时间。 3: CLOCK_THREAD_CPUTIME_ID:本线程到当前代码系统CPU花费的时间。需要注意是不是线程开始到当前代码的时间。
2
sleep(5); // 5s
usleep(3000000); // 3s
四、字符串
const char* filepath) {
// 找到 ‘/’ 最后一次在filepath中出现的位置
const char* base = std::strrchr(filepath, ‘/’); }
五、算法
1. algotithm
#include <algotithm>
// to find and return special value t in interval be
find(b,e,t);
find_if(b,e, func);
search(b,e,b2,e2); // value is a sequence
// copy whole sequence from intercal be to d
copy(v.begin(),v.end(),v1);
// won't delete elements but copy elements that aren't same with inv to v1
remove_copy(v.begin(), v.end(), back_inserter(v1), inv);
// won't delete elements but save all elements meet func
remove_copy_if(v.begin(), v.end(), back_inserter(v1), func);
// source and destination are same; copy all elements don't meet func to the start of this interval; return first place after last uncopied element
remove_if(v.begin(), v.end(), func);
// transform values to other values; (interval, save every processed value in this destination, function to process every value)
transform(v.begin(), v.end(), back_inserter(v1), func);
// copy elements that meet func to start and return a iterator, which points to the first place after last element that doesn't meet func
partition(v.begin(), v.end(), func);
stable_partition(v.begin(), v.end(), func);
// replace element that lay in this interval and equals to t1 with t2
replace(v.begin(), v.end(), t1, t2);
2. cctype
#include <cctype>
isspace(c); // space
isalpha(c); // character
isdigit(c); // num
isalnum(c); // num or character
ispunct(c); // punctuation
isupper(c);
islower(c);
toupper(c); // generate a upper character same with c
tolower(c); // generate a lower character same with c
六、流
1.
#include <iomanip>
// re
setprecision(n);
七、其它
- 在使用std::bind来包装成员函数时,第二个参数必须要设定为一个相应对象的地址(第一个对象是需要被bind的成员函数的地址),否则是不能通过编译的。