其他操作

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的更多相关文章

  1. C++常用库函数

    C++常用库函数  转自:http://blog.csdn.net/sai19841003/article/details/7957115 1.常用数学函数 头文件 #include <math ...

  2. C语言字符串操作常用库函数

    C语言字符串操作常用库函数 *********************************************************************************** 函数 ...

  3. 转载 C++常用库函数atoi,itoa,strcpy,strcmp的实现

    C++常用库函数atoi,itoa,strcpy,strcmp的实现 C语言字符串操作函数 1. 字符串反转 - strRev2. 字符串复制 - strcpy3. 字符串转化为整数 - atoi4. ...

  4. PHP常用库函数介绍+常见疑难问题解答

    来源:http://www.cnblogs.com/lanxuezaipiao/archive/2013/05/19/3086858.html 虽然PHP在整体功能上不如Java强大,但相比PHP而言 ...

  5. C++常用库函数(1)

    Hello,疯狂的杰克由于大家见面了哦! 今天,给大家介绍一篇很有内涵的文章:C++常用库函数 1.缓冲区操作函数 函数名:memchr 函数原型:void  *memchr(const void * ...

  6. C++之cmath常用库函数一览

    cmath是c++语言中的库函数,其中的c表示函数是来自c标准库的函数,math为数学常用库函数. cmath中常用库函数: 函数 作用 int abs(int i); 返回整型参数i的绝对值 dou ...

  7. 【算法专题】工欲善其事必先利其器—— 常用函数和STL

    一.    常用函数 #include <stdio.h> int getchar( void );               //读取一个字符, 一般用来去掉无用字符 char *ge ...

  8. C语言常用库函数

    一.数学函数 调用数学函数时,要求在源文件中包下以下命令行: #include <math.h> 函数原型说明 功能 返回值 说明 int abs( int x) 求整数x的绝对值 计算结 ...

  9. C 常用库函数memset,编译器宏定义assert

    一. 总览 1.1库函数 函数名 头文件 功能 原型 说明 syslog syslog.h 记录至系统记录(日志) void    syslog(int, const char *, ...) __p ...

随机推荐

  1. Leetcode 318 Maximum Product of Word Lengths 字符串处理+位运算

    先介绍下本题的题意: 在一个字符串组成的数组words中,找出max{Length(words[i]) * Length(words[j]) },其中words[i]和words[j]中没有相同的字母 ...

  2. WPF自定义TextBox及ScrollViewer

    原文:WPF自定义TextBox及ScrollViewer 寒假过完,在家真心什么都做不了,可能年龄大了,再想以前那样能专心坐下来已经不行了.回来第一件事就是改了项目的一个bug,最近又新增了一个新的 ...

  3. angular.js分页代码的实例

    对于大多数web应用来说显示项目列表是一种很常见的任务.通常情况下,我们的数据会比较多,无法很好地显示在单个页面中.在这种情况下,我们需要把数据以页的方式来展示,同时带有转到上一页和下一页的功能.现在 ...

  4. 【Linux计划】XSI IPC

    三种IPC这就是所谓的XSI IPC,每间: 消息队列 信号量 共享存储器 以下分别介绍三种IPC的使用方法. 1.消息队列 消息队列是消息的链接表,具有例如以下函数接口: msgget:创建一个新队 ...

  5. [WPF] PerformClick ?

    原文:[WPF] PerformClick ? [WPF] PerformClick ?  周银辉 WPF没有提供这个方法,还真是让人觉得有些讨厌啊.而关于这个嘛,Google中搜一下,一大堆,但一般 ...

  6. Tab切换效果的实现

    <!--引用jquery和bootstrap--> <link rel="stylesheet" href="~/Content/bootstrap.m ...

  7. 读BeautifulSoup官方文档之html树的搜索(2)

    除了find()和find_all(), 这里还提供了许多类似的方法我就细讲了, 参数和用法都差不多, 最后四个是next, previous是以.next/previous_element()来说的 ...

  8. 毕业设计之感悟 —— UML 与 ER 图

    今天毕业设计答辩,虽然我第一个上场,但是不是特别紧张,因为整个系统都是我写的.我以为自己天衣无缝,能应付所有老师的所有问题.事实上,我被老师教育了一番. 老师说我,毕业论文中没有一个类.我一开始比较懵 ...

  9. WPF WindowChrome 自定义窗口

    1.wpf自定义窗口: WindowChrome类描述:https://msdn.microsoft.com/zh-cn/library/system.windows.shell.windowchro ...

  10. ORA-02085: database link string connect to string

    ORA-02085: database link string connects to string Cause: a database link connected to a database wi ...