【C++ 基础函数/简单易懂】binary_search,pair,next_permutation,swap,memset,fill


这是我第一篇CSDN博客,本人学习C刚刚半年,写的不好或者有错的请见谅!
该篇用于学习一些基础的C++函数(有些属于STL)

博客注重:for萌新(我):简单!一看就懂!有用!复杂的有编程输出!

binary_search 二分查找某个元素是否出现

·头文件:#include < algorithm >
·函数模板:binary_search(arr[],arr[]+size , indx)
·参数说明:
arr[]: 数组首地址
size:数组元素个数
indx:需要查找的值
·函数功能: 在数组中以二分法检索的方式查找,那复杂度为O(log n)
若在数组(要求数组元素非递减)中查找到indx元素则真,若查找不到则返回值为假。

#include <iostream>
#include <algorithm>
using namespace std;
int arr[500050];
int main()
{
    for(int i=0;i<10000;i++)
    {
        arr[i]=i;
    }
    cout << binary_search(arr,arr+10000,55) << endl;	//找得到55
    cout << binary_search(arr,arr+10000,-1) << endl;	//找不到-1
    return 0;
}

输出结果:
55找到了,-1没找到

Pair 数据对

·头文件: #include< utility >
·定义方法:pair<T1, T2> P;
·含义:表示P有两个类型,类型T1与类型T2
例子:
[默认初始化] pair< int , string >My_Pair ;
[自己初始化] pair< int , string >My_Pair2(100,"score") ;
[用别的初始化]pair< int , string >My_Pair3(My_Pair4) ;
[或者这样]pair< int , string >My_Pair3=My_Pair4 ;
默认初始化之后怎么修改P的内容呢?用make_pair(a,b) 函数!

	pair< int , string > My_Pair ;
	My_Pair=make_pair(100,"score") ;

怎么输出P的内容呢? T1与T2相当于P的第一个内容和第二个内容
cout << P.first << endl << P.second << endl;

不过,T1 T2你也可以用结构体或者也是pair类型的进行套娃!
例子一:

	pair < pair < int , int > , string > P1(make_pair(11,12),"A");
	cout << P1.first.first << " " << P1.first.second << " " << P1.second;

例子二:

#include <iostream>
#include <cmath>
#include <utility>
using namespace std;
struct pt{
    int x;
    int y;
};
int main()
{
    pair < pt , double > P1;
    P1.first.x=1;
    P1.first.y=1;
    P1.second = sqrt(P1.first.x*P1.first.x+P1.first.y*P1.first.y);
    cout << P1.first.x << endl << P1.first.y << endl << P1.second;
    return 0;
}

输出:
结果!
·pair的比较:优先比较first,first相同再比较second
使用> , >= , < , <= , == , != 进行pair的比较操作
if(My_Pair1 > My_Pair2)cout << "Something";

next_permutation 全排列作用

·头文件: #include < algorithm >
·用法 next_permutation(arr,arr+size);
·作用:产生该数组的下一全排列

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int n=4;
    int a[4]={0};
    for(int i=0;i<n;i++)a[i]=i;		//赋值
	do{
		for(int i=0;i<n;i++)		//输出该状态下的数组
			printf("%d ",a[i]);
		puts("");
 	}while(next_permutation(a,a+n));//核心函数
    return 0;
}

输出:
全排列
如果数组中有相同的数字会怎么样呢?

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int main()
{
    int n=4;
    int a[4]={1,2,2,3};		//赋值
	do{
		for(int i=0;i<n;i++)
			printf("%d ",a[i]);
		puts("");
 	}while(next_permutation(a,a+n));
    return 0;
}

结果:
结果2
简单好懂,不会出现重复的排列。
当然,它也能为字符串排列,都是按照字典序排序的,因为char类型是以unsigned_int类型储存的

swap 交换函数

·作用:交换变量/数组/容器 等
简单的例子:

int a=1,b=2;
swap(a,b);
int a[]={1,2,3};
int b[]={3,2,1};
swap(a,b);
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
int main()
{
    vector<int>V1(10,10),V2(10,5);	//容器
    for(vector<int>::iterator it=V1.begin();it!=V1.end();it++)
        cout << *it << " ";
    swap(V1,V2);
    puts("");
    for(vector<int>::iterator it=V1.begin();it!=V1.end();it++)
        cout << *it << " ";
    return 0;
}

结果:
结果!

memset 初始化

·头文件:#include < string.h >
·作用:初始化高维数组,速度比for一遍更加快!数组越大越快!
·用法:memset(arr,num,size);
例子:
memset(arr1,0,sizeof(arr1));
memset(arr2,-1,sizeof(int)*10);
注意:只能初始化0与-1!!因为0是全位为0,-1是全位为1,初始化其他数会出错

fill 填充

·头文件:#include < algorithm >
·作用:填充数组或者容器以你想要的内容
·写法
fill(arr+start,arr+end,num);
fill(myvector.begin(), myvector.end() , num);
例子:
int a[100]={0}; fill(a+2,a+10,5);
这样数组a就会变成 0 0 5 5 5 5 5 5 5 5 0 0 0 0 0 0 0……

  • 5
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值