1.sort(a,a+n); 从小到大排序a[n]。
sort()是不稳定排序,stable_sort()是稳定排序。
2.lower_bound和upper_bound (注意数组下标越界)
- lower_bound(a,a+n,x)-a; 返回查找元素的第一个可安插位置,也就是“元素值>=查找值”的第一个元素的位置
- upper_bound(a,a+n,x)-a; 返回查找元素的最后一个可安插位置,也就是“元素值>查找值”的第一个元素的位置
- 如果所有元素都小于val,则返回last的位置,且last的位置是越界的!!
函数upper_bound()返回的在前闭后开区间查找的关键字的上界,如一个数组number序列1,2,2,4.upper_bound(2)后,返回的位置是3(下标)也就是4所在的位置。
3.向量 vector; 不定长数组。
- 声明: vector<int>v; vector<double>v; vector<string>v;
- v.begin(); 返回指向容器最开始位置数据的指针
- v.end(); 返回指向容器最后一个数据单元+1的指针,如果要输出最后一个元素的值应该是 *(--v.end());
- v.size() 读取大小
- v.resize(n) 改变大小
- v.push_back(x) 向尾部添加元素
- v.pop_back() 删除最后一个元素
- v.clear() 清空
- v.empty() 测试是否为空
- sort(v.begin(),v.end()); 从小到大
- reverse(v.begin(),v.end()); 从大到小
- unique 将相邻且重复的数放到vector的尾部,然后返回指向第一个重复元素的迭代器(需要注意的是,被放在尾部的数据有时会产生变化,所以不能继续使用了,需要废弃掉)
- erase 擦除重复的数据
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector <int> v;
v.push_back(5);
v.push_back(7);
v.push_back(2);
v.push_back(1);
vector<int>::iterator it;
for(it = v.begin(); it != v.end(); it++)
cout << *it << endl;
cout << "容器中第一个元素是:" << *v.begin() << endl;
cout << "容器中最后一个元素是:" << *(--v.end()) << endl;
}
v.erase(unique(v.begin(), v.end()), v.end());//去重。
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main()
{
vector<int> v;
for (int i = 0; i < 6; i++)
{
int x; cin >> x;
v.push_back(x); //在vector尾部加入一个数据
}
sort(v.begin(),v.end());//从小到大排序
v.erase(unique(v.begin(), v.end()), v.end());//去重
for (int i = 0; i < v.size(); i++)
cout << v[i] << " ";
}
4.set 集合 自动去重并且从小到大排序
#include <iostream>
#include <algorithm>
#include <set>
using namespace std;
int main()
{
set <int> s;
s.insert(5);
s.insert(7);
s.insert(2);
s.insert(1);
set<int>::iterator it;
for(it = s.begin(); it != s.end(); it++)
cout << *it << endl;
cout << "容器中第一个元素是:" << *s.begin() << endl;
cout << "容器中最后一个元素是:" << *(--s.end()) << endl;
}
1
2
5
7
容器中第一个元素是:1
容器中最后一个元素是:7
- set<int> S; //定义一个set容器(注意 set只能定义一个容器)
- set<int> :: iterator it//定义一个指针,在set容器中我们用指针来定位,类似于数组a[i]中的i ,指针只能++或者--
- S.clear() //清除所有元素
- S.insert() //在集合中插入元素
- S.begin() //返回指向第一个元素的迭代器
- S.end() //返回指向最后一个元素的迭代器
- S.count() //返回某个值元素的个数,虽然是个数,但是set会自动去重,所以count只能判断元素是否存在,也许吧。
- S.swap() //交换两个集合变量
- S.empty() //如果集合为空,返回true
- S.equal_range() //返回集合中与给定值相等的上下限的两个迭代器
- S.erase() //删除集合中的元素
- S.find() //返回一个指向被查找到元素的迭代器
- S.get_allocator() //返回集合的分配器
- S.lower_bound() //返回指向大于(或等于)某值的第一个元素的迭代器
- S.key_comp() //返回一个用于元素间值比较的函数
- S.max_size() //返回集合能容纳的元素的最大限值
- S.rbegin() //返回指向集合中最后一个元素的反向迭代器
- S.rend() //返回指向集合中第一个元素的反向迭代器
- S.size() //集合中元素的数目
- S.upper_bound() //返回大于某个值元素的迭代器
- S.value_comp() //返回一个用于比较元素间的值的函数
- for(it=s.begin();it!=s.end();it++) printf("%d ",*it); //从头到尾输出集合内的值( 注意 不能用<=,>=,<,> 不过可以用指针it++或it--控制)
5.multiset 多重集合
multiset所有用法和set基本相同,但multiset和set不同点就是multiset不去重,set去重
6.memcpy 内存拷贝函数,功能是从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中。
把a复制到b:
#include<cstring>
int a[5] = {1, 2, 3, 4, 5}, b[5];
memcpy(b, a, sizeof(a));
7.fill(arr, arr + n, 要填入的内容); //充填数组
#include<algorithm>
fill(arr, arr + 10, 2);//数组
fill(v.begin(), v.end(), -1);//vector
8.accumulate()
- 累加求和
accumulate带有三个形参:头两个形参指定要累加的元素范围[first,last),第三个形参则是累加的初值。
T accumulate( InputIt first, InputIt last, T init )
#include<numeric>
int sum=accumulate(b,b+n,0);//数组 初值0
int sum=accumulate(v.begin(),v.end(),42);//vector,初值42
- 另一个作用是串联字符串
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
char a[5]={'a','b','c','d','e'};
string sum = accumulate(a,a+5, string(""));
cout<<sum<<endl;
}
#include <iostream>
#include <algorithm>
#include <vector>
#include <numeric>
using namespace std;
int main()
{
vector <char> v;
v.push_back('a');
v.push_back('b');
v.push_back('c');
v.push_back('d');
v.push_back('e');
string sum = accumulate(v.begin() , v.end() , string(""));
cout<<sum<<endl;
}
结果都是abcde
9.<cctype>
isalnum //检查字符是否为字母或数字
isalpha //检查字符是否为字母
islower //检查字符是否为小写
isupper //检查字符是否为大写字符
isdigit //检查字符是否为数字
isxdigit //检查字符是为十六进制字符
iscntrl //检查字符是否为控制字符
isgraph //检查字符是否为图形字符
isspace //检查字符是否为空白符
isblank //(C++11) 检查字符是否为空格符
isprint //检查字符是否为打印字符
ispunct //检查字符是否为标点符
tolower //转换字符为小写
toupper //转换字符为大写