【C/C++】OJ笔试常用的函数
来源:互联网 发布:朴廷桓柯洁网络十番棋 编辑:程序博客网 时间:2024/06/07 06:36
unique函数
删除连续重复的范围
template <class ForwardIterator> ForwardIterator unique ( ForwardIterator first, ForwardIterator last );template <class ForwardIterator, class BinaryPredicate> ForwardIterator unique ( ForwardIterator first, ForwardIterator last, BinaryPredicate pred );
返回值,返回的是迭代器,指向新的末端
测试
// unique algorithm example#include <iostream>#include <algorithm>#include <vector>using namespace std;bool myfunction (int i, int j) { return (i==j);}int main () { int myints[] = {10,20,20,20,30,30,20,20,10}; // 10 20 20 20 30 30 20 20 10 vector<int> myvector (myints,myints+9); vector<int>::iterator it; // using default comparison: it = unique (myvector.begin(), myvector.end()); // 10 20 30 20 10 ? ? ? ? // ^ myvector.resize( it - myvector.begin() ); // 10 20 30 20 10 // using predicate comparison: unique (myvector.begin(), myvector.end(), myfunction); // (no changes) // print out content: cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; return 0;}
sort函数
注:参数必须是RandomAccessIterator类型的迭代器,即:只有vector,string,array,deque类型,可以用sort。
参数:区间是左闭右开的。默认是从小到大排序
template <class RandomAccessIterator> void sort ( RandomAccessIterator first, RandomAccessIterator last );template <class RandomAccessIterator, class Compare> void sort ( RandomAccessIterator first, RandomAccessIterator last, Compare comp );
测试
// sort algorithm example#include <iostream>#include <algorithm>#include <vector>using namespace std;bool myfunction (int i,int j) { return (i<j); }struct myclass { bool operator() (int i,int j) { return (i<j);}} myobject;int main () { int myints[] = {32,71,12,45,26,80,53,33}; vector<int> myvector (myints, myints+8); // 32 71 12 45 26 80 53 33 vector<int>::iterator it; // using default comparison (operator <): sort (myvector.begin(), myvector.begin()+4); //(12 32 45 71)26 80 53 33 // using function as comp sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80) // using object as comp sort (myvector.begin(), myvector.end(), myobject); //(12 26 32 33 45 53 71 80) // print out content: cout << "myvector contains:"; for (it=myvector.begin(); it!=myvector.end(); ++it) cout << " " << *it; cout << endl; //myvector contains: 12 26 32 33 45 53 71 80 return 0;}
reverse函数
翻转函数,将[first,last)范围的元素,进行翻转。
Reverse range
Reverses the order of the elements in the range [first,last).
template <class BidirectionalIterator> void reverse ( BidirectionalIterator first, BidirectionalIterator last);
测试代码
vector<int> myvector; // set some values: for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 reverse(myvector.begin(),myvector.end()); // 9 8 7 6 5 4 3 2 1
lower_bound函数和upper_bound 函数
template <class ForwardIterator, class T> ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last,const T& value );template <class ForwardIterator, class T, class Compare> ForwardIterator lower_bound ( ForwardIterator first, ForwardIterator last, const T& value, Compare comp );
lower_bound函数 返回一个迭代器,指向范围内[first,end)的不小于value的值。
upper_bound 函数 返回一个迭代器,指向范围内[first,end)的大于value的值。
测试代码
// lower_bound/upper_bound example#include <iostream>#include <algorithm>#include <vector>using namespace std;int main () { int myints[] = {10,20,30,30,20,10,10,20}; vector<int> v(myints,myints+8); // 10 20 30 30 20 10 10 20 vector<int>::iterator low,up; sort (v.begin(), v.end()); // 10 10 10 20 20 20 30 30 low=lower_bound (v.begin(), v.end(), 20); // ^ up= upper_bound (v.begin(), v.end(), 20); // ^ return 0;}
阅读全文
0 0
- 【C/C++】OJ笔试常用的函数
- 常用的C函数
- 常用的C函数
- 做OJ题常用的C语言函数——数字相关 (不断更新......)
- [C#] .net的常用函数
- C常用函数的实现
- C常用函数的实现
- C中常用的函数
- C语言常用的函数
- C语言---常用的函数
- C++string常用的函数
- C语言的常用函数
- 使用c函数实现的常用函数
- 一些不安全的常用的C函数
- C#.net常用函数
- c#.net常用函数
- c 常用函数说明
- Linux 常用C函数
- android的线程池,你了解这些就够了
- 关于学习NDK基础知识这些就够了(五)
- 深入理解Java之线程池
- springboot项目发布到tomcat容器
- windows操作系统之跨进程边界共享内核对象
- 【C/C++】OJ笔试常用的函数
- 初学python+selenium之路1:启动&关闭浏览器
- c++ primer plus阅读笔记5---指针const函数指针数组
- 大数阶乘方法(1!+2!+3!+……+n!)
- 括号匹配(栈的应用code)
- 网易2018校招内推编程题
- win10禁用笔记本键盘
- Substrings(截取字符串)
- (UVA