精简C++运算符重载

运算符重载

作用:用于自定义运算
形式:如:
1 类名 operator+( 类名 形参名, 类名 形参名 ){}
2 类名 operator+( 类名 形参名 ){}

代码实现1

#include<iostream>
using namespace std;

class complex{
    friend complex operator+( const complex &A, const complex &B );
    private:
        int real, imag;
    public:
        complex( const int &r = 0, const int &i = 0 ):real(r), imag(i){};
        void getComplex(){
            cout << real << "+" << imag << "i" << endl;
        }
};
complex operator+( const complex &A, const complex &B ){
    complex C;
    C.real = A.real + B.real;
    C.imag = A.imag + B.imag;
    return C;
}

int main(){
    complex c1( 1, 1 ), c2( 2, 2 ), c3;
    
    c3 = c1 + c2;
    c3.getComplex();
    
    return 0;
}

运行结果1
在这里插入图片描述
代码实现2

#include<iostream>
using namespace std;

class complex{
    private:
        int real, imag;
    public:
        complex( const int &r = 0, const int &i = 0 ):real(r), imag(i){};
        void getComplex(){
            cout << real << "+" << imag << "i" << endl;
        }
        complex operator+( const complex &A ){
            complex B;
            B.real = this->real + A.real;
            B.imag = this->imag + A.imag;
            return B;
        }
};

int main(){
    complex c1( 1, 1 ), c2( 1, 1 ), c3;

    c3 = c1 + c2;
    c3.getComplex();

    return 0;
}

运行结果
在这里插入图片描述
总结
重载实际就是自己定义一个现有的函数,+运算符不可以对自定义类进行加法运算,否则将会出现错误,故我们自己定义一个函数使其相加变得合理;
重载的另一层含义则是输入的参数不同即为重载;
同理,-*/都可进行重载;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
K-means算法是一种基于聚类的机器学习算法,可以将一组数据分成K个不同的类别。在C++中,可以使用PCL库来实现K-means算法。 以下是一个简单的K-means算法实现示例: ```c++ #include <iostream> #include <vector> #include <pcl/kdtree/kdtree_flann.h> #include <pcl/features/normal_3d.h> #include <pcl/filters/extract_indices.h> #include <pcl/segmentation/extract_clusters.h> #include <pcl/segmentation/region_growing.h> int main() { // Load point cloud data pcl::PointCloud<pcl::PointXYZ>::Ptr cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::io::loadPCDFile("input_cloud.pcd", *cloud); // Apply region growing segmentation pcl::search::Search<pcl::PointXYZ>::Ptr tree(new pcl::search::KdTree<pcl::PointXYZ>); pcl::PointCloud<pcl::Normal>::Ptr normals(new pcl::PointCloud<pcl::Normal>); pcl::NormalEstimation<pcl::PointXYZ, pcl::Normal> normal_estimator; normal_estimator.setSearchMethod(tree); normal_estimator.setInputCloud(cloud); normal_estimator.setKSearch(50); normal_estimator.compute(*normals); pcl::RegionGrowing<pcl::PointXYZ, pcl::Normal> reg; reg.setSearchMethod(tree); reg.setInputCloud(cloud); reg.setInputNormals(normals); reg.setMinClusterSize(50); reg.setMaxClusterSize(1000000); reg.setNumberOfNeighbours(30); reg.setSmoothnessThreshold(3.0 / 180.0 * M_PI); reg.setCurvatureThreshold(1.0); std::vector<pcl::PointIndices> clusters; reg.extract(clusters); // Apply K-means clustering to each region for (int i = 0; i < clusters.size(); i++) { pcl::PointCloud<pcl::PointXYZ>::Ptr cluster_cloud(new pcl::PointCloud<pcl::PointXYZ>); pcl::ExtractIndices<pcl::PointXYZ> extract_indices; extract_indices.setInputCloud(cloud); extract_indices.setIndices(boost::make_shared<const pcl::PointIndices>(clusters[i])); extract_indices.filter(*cluster_cloud); // Perform K-means clustering pcl::Kmeans kmeans; kmeans.setInputCloud(cluster_cloud); kmeans.setK(3); kmeans.compute(); // Print centroid coordinates for each cluster std::vector<pcl::PointXYZ> centroids = kmeans.getCentroids(); for (int j = 0; j < centroids.size(); j++) { std::cout << "Cluster " << j << " centroid coordinates: " << centroids[j].x << ", " << centroids[j].y << ", " << centroids[j].z << std::endl; } } return 0; } ``` 该示例加载点云数据,使用区域生长算法将点云分成不同的区域,并对每个区域执行K-means聚类。最后,程序打印每个聚类的质心坐标。 以上是一个简单的K-means算法实现示例,具体实现还需要根据具体应用场景进行调整和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值