CPP常用库函数以及STL
其他操作
memset
void * memset ( void * ptr, int value, size_t num );
memset(ptr,0xff,sizeof(ptr));
使用memset初始化vector
vector<int> vec(10,1);
memset(vec.data(),0,vec.size()*sizeof(int));
#include<bits/stdc++.h>

需要注意的是:对于set和map而言,find并不是第一个满足条件的对象位置,而是其中的任意一个对象。
Standard Template Library: Algorithms
全排列
序列升序
next_permutation(a.begin(), a.end())
序列降序
prev_permutation(b.begin(), b.end())
64int
long long ll;
scanf("%I64d", &ll);
printf("%I64d", ll);
lower_bound(a,a+n,x)
二分查找,查找大于或等于x的第一个位置,只能查找vector<>数组,返回值为vector<>::iterator指针
unique(vector1.begin(),vector1.end())
unique就是让连续的相同值变成一个
binary_search (v.begin(), v.end(), 6, myfunction)
bool myfunction (int i,int j) { return (i<j); }
reverse(vector1.begin(),vector1.end())
find (myvector.begin(), myvector.end(), 30);
An iterator to the first element in the range that compares equal to val.
If no elements match, the function returns last.
equal_range
bounds=std::equal_range (v.begin(), v.end(), 20, mygreater);
bounds.first:is an iterator to the lower bound of the subrange of equivalent values,
bounds.second:its upper bound.
// 30 30 20 20 20 10 10 10
// ^ ^
Non-modifying sequence operations:
for_each
void out(int i){
cout << i << endl;
}
int main(){
int a[] = {1, 2, 3, 5, 4};
for_each(a, a+5, out);
vector<int> b;
b.push_back(1);
b.push_back(2);
b.push_back(3);
for_each(b.begin(), b.end(), out);
return 0;
}
find
std::vector<int>::iterator it;
it = find (myvector.begin(), myvector.end(), 30);
find_if
bool IsOdd (int i) {
return ((i%2)==1);
}
std::vector<int>::iterator it = std::find_if (myvector.begin(), myvector.end(), IsOdd);
find_first_of
bool comp_case_insensitive (char c1, char c2) {
return (std::tolower(c1)==std::tolower(c2));
}
int main () {
int mychars[] = {'a','b','c','A','B','C'};
std::vector<char> haystack (mychars,mychars+6);
std::vector<char>::iterator it;
int needle[] = {'D'};
// using default comparison:
it = find_first_of (haystack.begin(), haystack.end(), needle, needle+1);
if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n';
// using predicate comparison:
it = find_first_of (haystack.begin(), haystack.end(),
needle, needle+1, comp_case_insensitive);
if (it!=haystack.end())
std::cout << "The first match is: " << *it << '\n';
return 0;
}
常用库函数
sort
位置:algorithm
功能:给一个数组(或者一个 STL,这个会在第三章介绍)排序。
格式:sort(a+1,a+n+1,cmp);
说明:
a 是数组的名称,同时也是指向数组首地址的指针。
+1 或者+n+1 为地址偏移量,表示需要排序的范围。
也可以替换为其他 STL 迭代器。
cmp 是自己写的函数,格式如下:
bool cmp(Type a, Type b)
{
//比较方法,如果 a 应该在 b 前则返回 true。
}
unique
位置:algorithm
功能:去除一个容器(也可以是数组)内的所有重复元素。
格式:unique(a+1,a+n+1);
说明:
与 sort 函数类似。
__gcd
位置:algorithm
功能:求两个整数的最大公约数。
格式:__gcd(a,b);
说明:两个参数的类型必须相同。
next_permutation
位置:algorithm
功能:求下一个(字典序)排列
格式:next_permutation(s+1,s+n+1);
说明:
一定要保证参数 s 是一个排列。
strcmp
位置:cstring
功能:比较两个字符串
格式:strcmp(s1,s2)
说明:
相等返回 0,s1 字典序较小返回-1,较大返回 1。
memset
位置:cstring
功能:将内存区间的每一个字节(注意是字节而不是变量)赋值为给定数。
格式:memset(a,0,sizeof(a));
说明:
只能为整数数组赋值为 0/-1。
可以对字符数组任意赋值。
memcpy
位置:cstring
功能:将一个内存区间复制。
格式:memcpy(to,from,sizeof(to));
STL
lower_bound
功能:返回一个非递减序列[first, last)中的第一个大于等于值val的位置。
声明:lower_bound(ForwardIter first, ForwardIter last,const _Tp& val) -arraylistname
upper_bound
功能:算法返回一个非递减序列[first, last)中第一个大于val的位置。
声明:upper_bound(ForwardIter first, ForwardIter last, const _Tp& val)
vector
功能:一个可变大小的数组。
声明:vector<类型> 变量名;
访问:变量名[位置](当数组用即可)
插入:变量名.push_back(变量);
说明:
它的本体是一个对象。
priority_queue
功能:堆
声明:priority_queue<类型> 变量名;
访问:变量名.top();(仅能访问堆顶元素)
插入:变量名.push(变量);
删除:变量名.pop();
说明:
类型需要定义<运算符。
注意 pq 实现的是反人类的大根堆,自定义<号时需要注意实际上是>。
set
功能:集合
声明:set<类型> 变量名;
访问:变量名.find(值);
插入:变量名.insert(值);
删除:变量名.erase(迭代器);
变量名.erase(值);
说明:
单次操作复杂度 O(logn)。
map
功能:映射
声明:map<源类型,目标类型> 变量名;
访问:变量名[源类型值](如果不存在该值则会进行插入。)
说明:
单次操作复杂度 O(logn)。
string
功能:灵活的字符串对象
声明:string 变量名;
赋值:变量名=”C 风格字符串常量”;
合并:变量名+变量名 2(例如 s1=”a”,s2=”b”,s1+s2=”ab”)
求长:变量名.length();(其余 STL 求大小均为变量名.size())
访问:变量名[位置](当数组用)
说明:不能作为 C 风格函数的参数。
CPP常用库函数以及STL的更多相关文章
- C++常用库函数
C++常用库函数 转自:http://blog.csdn.net/sai19841003/article/details/7957115 1.常用数学函数 头文件 #include <math ...
- C语言字符串操作常用库函数
C语言字符串操作常用库函数 *********************************************************************************** 函数 ...
- 转载 C++常用库函数atoi,itoa,strcpy,strcmp的实现
C++常用库函数atoi,itoa,strcpy,strcmp的实现 C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. 字符串转化为整数 - atoi4. ...
- PHP常用库函数介绍+常见疑难问题解答
来源:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/19/3086858.html 虽然PHP在整体功能上不如Java强大,但相比PHP而言 ...
- C++常用库函数(1)
Hello,疯狂的杰克由于大家见面了哦! 今天,给大家介绍一篇很有内涵的文章:C++常用库函数 1.缓冲区操作函数 函数名:memchr 函数原型:void *memchr(const void * ...
- C++之cmath常用库函数一览
cmath是c++语言中的库函数,其中的c表示函数是来自c标准库的函数,math为数学常用库函数. cmath中常用库函数: 函数 作用 int abs(int i); 返回整型参数i的绝对值 dou ...
- 【算法专题】工欲善其事必先利其器—— 常用函数和STL
一. 常用函数 #include <stdio.h> int getchar( void ); //读取一个字符, 一般用来去掉无用字符 char *ge ...
- C语言常用库函数
一.数学函数 调用数学函数时,要求在源文件中包下以下命令行: #include <math.h> 函数原型说明 功能 返回值 说明 int abs( int x) 求整数x的绝对值 计算结 ...
- C 常用库函数memset,编译器宏定义assert
一. 总览 1.1库函数 函数名 头文件 功能 原型 说明 syslog syslog.h 记录至系统记录(日志) void syslog(int, const char *, ...) __p ...
随机推荐
- Visual C# 2010 实现菜单项和状态栏
演练:向窗体提供标准菜单项 Visual Studio 2010 其他版本 此主题尚未评级 - 评价此主题 可以通过 MenuStrip 控件为窗体提供标准菜单. 此演练演示如何使 ...
- WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮
原文:WPF实用指南一:在WPF窗体的边框中添加搜索框和按钮 在边框中加入一些元素,在应用程序的界面设计中,已经开始流行起来.特别是在浏览器(Crome,IE,Firefox,Opera)中都有应用. ...
- CF 455A(Boredom-dp)
A. Boredom time limit per test 1 second memory limit per test 256 megabytes input standard input out ...
- LAN公布java web项目的方法的外侧
1.进入路由器设置页面:http://192.168.1.1 2.找到"转发规则"选项,我现在的路由器tp-link,不同型号tp-link"转发规则"选项位置 ...
- sql in(inner join)
查看执行任务可知,使用in关键字,会进行inner join,找出匹配项
- SqlServer 复制中将大事务分成小事务分发
原文:SqlServer 复制中将大事务分成小事务分发 在sql server 复制中,当在发布数据库执行1个大事务时,如一次性操作 十万或百万以上的数据.当操作数据在发布数据库执行完成后 ,日志读取 ...
- Android零基础入门第15节:掌握Android Studio项目结构,扬帆起航
原文:Android零基础入门第15节:掌握Android Studio项目结构,扬帆起航 经过前面的学习,Android Studio开发环境已准备OK,运行Android应用程序的原生模拟器和Ge ...
- RedHat 7.3 修改ASM磁盘绑定路径
RedHat 7中,很多命令发生了改变,绑定磁盘不再是start_udev,而是udevadm,具体绑定方式,请看另一篇博文: http://www.cnblogs.com/zx3212/p/6757 ...
- mysq练习(二)
Mysql练习(二) 1. delete,drop,truncate 的区别? 可以参考这位的: https://www.cnblogs.com/zhizhao/p/7825469.html 2. ...
- CoolFormat(Qt Creator也可管理VC的Project)
http://download.csdn.net/download/akof1314/8457593 https://github.com/akof1314/CoolFormat http://dow ...