算法
1.find()
查找指定值,并返回迭代器
函数原型
template <class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val);
用法:find (起始位置,结束位置,搜索值)
实现
template<class InputIterator, class T>
InputIterator find (InputIterator first, InputIterator last, const T& val)
{
while (first!=last) {
if (*first==val) return first;
++first;
}
return last;
}
Example:
对于非容器数据类型:搜索区间:[start,end] , end 要在预期的位置+1
如果 end 与 该变量的长度相等,那么无论有没有搜索到最后一个元素,都将放回最后一个元素的迭代器
int main(){
int a[]={1,2,3,4,5,6,7};
auto start = a;
auto end = a + 5;
int* b = find(start,end,7);
if(b==a+5){
cout<<"can't find"<<endl;
}
else{
cout<<"find"<<endl;
}
return 0;
}
对于容器
vector<int>v;
void test(int val){
auto it = find(v.begin(),v.end(),val);
if(it!=v.end()){
cout<<"find"<<endl;
}
else{
cout<<"can't find"<<endl;
}
}
int main(){
for(int i=0;i<5;i++){
v.push_back(i+1);
}
test(6);
test(5);
return 0;
}
can’t find
find
2.count()
在指定区间内对指定值进行计数
函数原型
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type
count (InputIterator first, InputIterator last, const T& val);
用法:count(起始位置,结束位置,需要计数的值)
实现
template <class InputIterator, class T>
typename iterator_traits<InputIterator>::difference_type //告诉编译器这是一个类型,否则编译不通过
count (InputIterator first, InputIterator last, const T& val)
{
typename iterator_traits<InputIterator>::difference_type ret = 0;
while (first!=last) {
if (*first == val) ++ret;
++first;
}
return ret;
}
Example:
int main(){
int a[]={1,2,3,5,5,6};
auto start = a;
auto end = a + 6;
cout<<count(start,end,5)<<endl;
return 0;
}
2
对于容器
vector<int>v;
void test(){
for(auto i = v.begin(); i != v.end(); i++){
cout<<*i<<" : "<<count(v.begin(),v.end(),*i)<<endl;
}
}
int main(){
v.push_back(1);
v.push_back(1);
v.push_back(2);
v.push_back(3);
test();
return 0;
}
1 : 2
1 : 2
2 : 1
3 : 1
泛型为<int,int>
vector<pair<int,int>>v;
void test(){
for(auto i=v.begin();i!=v.end();i++){
cout<<"("<<i->first<<","<<i->second<<")"<<" : "<<count(v.begin(),v.end(),*i)<<endl;
}
}
int main(){
v.push_back(make_pair(1,1));
v.push_back(make_pair(2,2));
v.push_back(make_pair(2,2));
v.push_back(make_pair(3,3));
test();
return 0;
}
(1,1) : 1
(2,2) : 2
(2,2) : 2
(3,3) : 1
3.sort()
将指定区间的元素进行排序
函数原型
//default (1) 使用 < 排序 : 默认从小到大
template <class RandomAccessIterator>
void sort (RandomAccessIterator first, RandomAccessIterator last);
//custom (2) 使用Compare排序
template <class RandomAccessIterator, class Compare>
void sort (RandomAccessIterator first, RandomAccessIterator last, Compare comp);
用法:sort(起始位置,结束位置) : 对 [first , last-1] 的区间进行排序
Example
void print(int* array ,int len){
for(int i=0;i<len;i++){
cout<<array[i]<<" ";
}
cout<<endl;
}
int main(){
int a[]={1,7,5,4,6,2};
print(a,6);
sort(a,a+6);
print(a,6);
return 0;
}
1 7 5 4 6 2
1 2 4 5 6 7
如果是 sort ( a , a+5 )
( 1 4 5 6 7 ) 2
使用 Compare 排序
Compare
Binary function that accepts two elements in the range as arguments, and returns a value convertible to
bool
. The value returned indicates whether the element passed as first argument is considered to go before the second in the specific strict weak ordering it defines.
The function shall not modify any of its arguments.
This can either be a function pointer or a function object.二元函数,接受范围中的两个元素作为参数,并返回可转换为bool的值。 返回的值指示作为第一个参数传递的元素是否被认为在它定义的特定严格弱顺序的第二个之前。
函数不能修改任何参数。
它可以是函数指针,也可以是函数对象。
void print(int* array ,int len){
for(int i=0;i<len;i++){
cout<<array[i]<<" ";
}
cout<<endl;
}
bool com(int a,int b){
return a>b;//只有a>b,b才能排在a前
}
int main(){
int a[]={1,7,5,4,6,2};
print(a,6);
sort(a,a+6,com);
print(a,6);
return 0;
}
1 7 5 4 6 2
7 6 5 4 2 1
4.reverse()
将指定区间的元素反转
函数原型
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last);
实现
template <class BidirectionalIterator>
void reverse (BidirectionalIterator first, BidirectionalIterator last)
{
while ((first!=last)&&(first!=--last)) {
std::iter_swap (first,last);
++first;
}
}
用法:reverse(起始位置,结束位置) : 对 [first , last-1] 的区间进行反转
Example
void print(int* array ,int len){
for(int i=0;i<len;i++){
cout<<array[i]<<" ";
}
cout<<endl;
}
int main(){
int a[] = {1,2,3,4,5};
print(a,5);
reverse(a,a+4);
print(a,5);
return 0;
}
1 2 3 4 5
( 4 3 2 1 ) 5
对于容器
int main(){
vector<int>v;
v.push_back(1);
v.push_back(2);
v.push_back(3);
v.push_back(4);
for(auto i = v.begin(); i != v.end() ; i++){
cout<<*i<<" ";
}
cout<<endl;
reverse(v.begin(),v.end());
for(auto i = v.begin(); i != v.end() ; i++){
cout<<*i<<" ";
}
return 0;
}
1 2 3 4
4 3 2 1