gpt4 book ai didi

c++ - 从 vector> 中排序和删除重复项

转载 作者:塔克拉玛干 更新时间:2023-11-03 06:45:56 26 4
gpt4 key购买 nike

您好,我知道有几个与此类似的问题,但我无法解决我的问题。我需要在笛卡尔坐标中的球体上生成一组独特的点,即从球面转换为笛卡尔坐标。当我这样做时,我将这些点存储在 vector 的 vector 中。但是创建了一些重复项并删除它们我尝试使用排序删除和独特功能。问题是 sort 看不到对我的整个 vector 进行排序,我不明白为什么?它适用于我只是将数字推回的 vector vector ,而不适用于我的笛卡尔函数生成的 vector vector 。我知道这很简单,我已经被困了 3 天了,我确定它正盯着我的脸!!!代码和输出如下

#include <iostream>
#include <math.h>
#include <algorithm>
#include <vector>
#include <stdio.h>

int main(int argc, const char * argv[]){
std::vector<double> locations; //center of the bubble
locations.push_back(1.0);
locations.push_back(1.0);
locations.push_back(1.0);
std::vector<std::vector<double> > points; //set of points to be created around the bubble
double PI=atan(1)*4;

for(int dr=1; dr<2; dr++){
for (int phi=0; phi<180; phi+=90){
for (int theta=0; theta<360; theta+=90){

std::vector<double> row;
double x=locations[0]+(dr*sin(theta*(PI/180))*cos(phi*(PI/180)));
double y=locations[1]+(dr*cos(theta*(PI/180)));
double z=locations[2]+(dr*sin(theta*(PI/180))*sin(phi*(PI/180)));
row.push_back(x);
row.push_back(y);
row.push_back(z);

points.push_back(row);


}
}
}



std::sort(points.begin(), points.end()); //sort points
std::cout<<"sorted points \n";
for (int i =0; i<points.size(); i++){
std::cout<<points[i][0]<<" "<<points[i][1]<<" "<<points[i][2]<<"\n";
}

points.erase(std::unique(points.begin(), points.end()), points.end()); //erase duplicates

std::cout<<"duplicates removed \n";
for (int i =0; i<points.size(); i++){
std::cout<< points[i][0]<<" "<<points[i][1]<<" "<<points[i][2]<<"\n";
}

输出

sorted points 
0 1 1
1 1 0
1 0 1 THIS HASN'T BEEN SORTED CORRECTLY
1 1 2
1 2 1
1 2 1
1 0 1 THIS HASN'T BEEN SORTED CORRECTLY
2 1 1
duplicates removed
0 1 1
1 1 0
1 0 1
1 1 2
1 2 1
1 0 1
2 1 1

最佳答案

如果你改变行:

std::cout << points[i][0] << " " << points[i][1] << " " << points[i][2] << "\n";

与(您还需要包括 <limits><iomanip> ):

std::cout << std::fixed << std::setprecision(std::numeric_limits<double>::digits10+2) << points[i][0] << " " << points[i][1] << " " << points[i][2] << "\n";

你会看到输出是:

sorted points
0.00000000000000000 0.99999999999999978 1.00000000000000000
0.99999999999999989 0.99999999999999978 0.00000000000000000
1.00000000000000000 0.00000000000000000 1.00000000000000022
1.00000000000000000 1.00000000000000000 2.00000000000000000
1.00000000000000000 2.00000000000000000 1.00000000000000000
1.00000000000000000 2.00000000000000000 1.00000000000000000
1.00000000000000022 0.00000000000000000 1.00000000000000000
2.00000000000000000 1.00000000000000000 1.00000000000000000
duplicates removed
0.00000000000000000 0.99999999999999978 1.00000000000000000
0.99999999999999989 0.99999999999999978 0.00000000000000000
1.00000000000000000 0.00000000000000000 1.00000000000000022
1.00000000000000000 1.00000000000000000 2.00000000000000000
1.00000000000000000 2.00000000000000000 1.00000000000000000
1.00000000000000022 0.00000000000000000 1.00000000000000000
2.00000000000000000 1.00000000000000000 1.00000000000000000

因此考虑近似值 vector 已正确排序。

PS 你可以在 std::sort() 中使用自定义比较器在 std::unique 中调用和自定义二进制谓词打电话。

关于c++ - 从 vector<vectors< double>> 中排序和删除重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21913177/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com