c++常用代码模板

(以从小到大排为例)

(1)冒泡排序

template<typename T>
void BubbleSort(T a[];T n)
{
    T i,j,item;
    for(i=0;i<n-1;i++)
        for(j=0;j<n-i-1;j++)
            if(a[j]>a[j+1])
            {
                item=a[j];
                a[j]=a[j+1];
                a[j+1]=item;
            }
}

(2)选择排序

template<typename T>
void selection_sort(T a[],n)
{
    T min,item,i,j;
    for(i=0;i<n;i++)
    {
        min=i;
        for(j=i+1;j<n;j++)
            if(a[j]<a[min])
                min=j;
         item=a[i];
         a[i]=a[min];
         a[min]=item;       
    }
}

(3)快速排序

template <typename T>
void Quick_Sort(T *array, int Start_Index, int End_Index) {
    if (End_Index >= Start_Index) {
        int first = Start_Index;
        int last = End_Index;
        T key = array[first];
        while (first < last) {
            while (first < last && array[last] >= key) {
                last--;
            }
            array[first] = array[last];
            while (first < last && array[first] <= key) {
                first++;
            }
            array[last] = array[first];
        }
        array[first] = key;
        Quick_Sort(array, Start_Index, first - 1);
        Quick_Sort(array, first + 1, End_Index);
    }
}

(4)最大公约数

int gcd(int a,int b)
{
    return a%b==0?b:gcd(b,a%b)
}
//最小公倍数:数相乘后除以最大公约数
//如求a,b最小公倍数,先求最大公约数c,所求最小公倍数=a*b/c。
//求a,b,c最小公倍数,先求a,b最小公倍数d,再求c,d最小公倍数即可。

(4)进制转换
1:常见进制(直接输入输出转换)

//以十六进制输入数据x,y
//再以十进制输出数据(C++)
cin>>hex>>x>>y;
cout<<dec<<x<<y;

2:利用函数转换

#include<iomanip>
cout<<setbase(n)<<x<<endl;
//输出为n进制(n貌似只能为8,10,16)

3:相除求余法

(6)素数判断

template<typename T>
bool isPrime(T n)
{
    int i;
    for(i=2;i*i<=n;++i)
        if(n%i==0)
            return 0(false);
    return 1(true);
}

(7)大小写字母转换

//十进制
//如A-a,ASCLL表中A=65,a=97
char(int(A)+32);
//a-A
char(int(a)-32);
//求得对应数字,如A
char A;
int n=A;
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
点云模板匹配是计算机视觉中常用的技术之一,下面为您提供一个基于PCL库实现的点云模板匹配的c++代码示例: ```c++ #include <iostream> #include <pcl/io/pcd_io.h> #include <pcl/point_types.h> #include <pcl/registration/icp.h> #include <pcl/filters/voxel_grid.h> int main(int argc, char** argv) { // 加载目标点云和待匹配点云 pcl::PointCloud<pcl::PointXYZ>::Ptr target_cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::PointCloud<pcl::PointXYZ>::Ptr input_cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile<pcl::PointXYZ>("target_cloud.pcd", *target_cloud); pcl::io::loadPCDFile<pcl::PointXYZ>("input_cloud.pcd", *input_cloud); // 对目标点云和待匹配点云进行下采样 pcl::VoxelGrid<pcl::PointXYZ> sor; sor.setInputCloud(target_cloud); sor.setLeafSize(0.01f, 0.01f, 0.01f); sor.filter(*target_cloud); sor.setInputCloud(input_cloud); sor.filter(*input_cloud); // 初始化ICP算法 pcl::IterativeClosestPoint<pcl::PointXYZ, pcl::PointXYZ> icp; icp.setInputSource(input_cloud); icp.setInputTarget(target_cloud); // 设置ICP算法参数 icp.setMaxCorrespondenceDistance(0.05); icp.setMaximumIterations(100); icp.setTransformationEpsilon(1e-8); icp.setEuclideanFitnessEpsilon(1); // 执行ICP算法 pcl::PointCloud<pcl::PointXYZ>::Ptr output_cloud(new pcl::PointCloud<pcl::PointXYZ>); icp.align(*output_cloud); // 输出匹配结果 std::cout << "ICP has converged:" << icp.hasConverged() << std::endl; std::cout << "score: " << icp.getFitnessScore() << std::endl; std::cout << "Transformation matrix:" << std::endl; std::cout << icp.getFinalTransformation() << std::endl; return 0; } ``` 以上代码中,首先通过PCL库加载目标点云和待匹配点云,并对它们进行下采样,然后初始化ICP算法并设置其参数,最后执行ICP算法并输出匹配结果。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值