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 风格函数的参数。

posted @ 2019-07-05 09:08  HeoLis  阅读(2172)  评论(0编辑  收藏  举报