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 ...
随机推荐
- Mybatis 一对多 配置文件
当一个Entity中包含的属性有对象和对象集合时,用mybatis映射时要在Entity中添加一个字段来唯一标识当前的Entity对象.否则查询的Entity集合中的对象会被覆盖掉. 如下一个POJO ...
- DB First EF中的存储过程、函数、视图
视图约等于表(属性)存储过程变为方法,方法中调用存储过程 EF可以调用存储过程,DB First的流程是刷新模型,获取存储过程,调用参考:http://blog.csdn.net/sudazf/art ...
- C# 自定义泛型类,并添加约束
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- passed into methods by value java专题
java没有引用传递只有按值传递,没有引用传递只有按值传递,值传递.因为Primitive类型的值不能改变,所以method不能更改调用方传的primitive 值.因为method更改的是Primi ...
- xml 封装类
public static class XmlHelper { public static T FromXmlFile<T>(this string filePath) where T : ...
- Keil5生成bin文件
进入“Options for Target”设置界面如下: 如图所示方框中输入: fromelf.exe --bin -o "$L@L.bin" "#L" 生成 ...
- SQLServer 不允许保存更改的解决办法
启动SQL Server Management Studio 工具菜单----选项----Designers(设计器)----阻止保存要求重新创建表的更改 取消勾选即可.
- Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法
原文:Win8 Metro(C#)数字图像处理--2.45图像雾化效果算法 [函数名称] 图像雾化 AtomizationProcess(WriteableBitmap src,i ...
- win10 uwp ApplicationView
原文:win10 uwp ApplicationView 本文和大家介绍一个重要的类,他可以用来设置窗口,如设置启动大小,设置是否允许截图,是否进入全屏,所有和窗口有关的,都可以在他这里设置. 可以使 ...
- Win10《芒果TV》商店版更新v3.2.5:新增会员频道,修复多处细节问题,小年快乐
听因乐不凡,尽在芒果TV,湖南卫视大型音乐竞技节目<歌手>,每周六晚22:30在芒果TV与湖南卫视同步直播,1月20日周五晚七点半,2016-2017湖南卫视<小年夜春晚>会员 ...