Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我的小std::独特示例不起作用_C++_Std - Fatal编程技术网

C++ 我的小std::独特示例不起作用

C++ 我的小std::独特示例不起作用,c++,std,C++,Std,我正在尝试使用cxx-11的std::unique()来查找 数组中的唯一元素: #include <iostream> #include <algorithm> #include <vector> #include <typeinfo> int main(){ const int n=11; double x[n],a3[n],a1[n]; x[0]=-0.717778; x[1]=-0.496843;

我正在尝试使用
cxx-11
std::unique()
来查找
数组中的唯一元素

#include <iostream>
#include <algorithm>
#include <vector>
#include <typeinfo>


int main(){
    const int n=11;
    double x[n],a3[n],a1[n];

    x[0]=-0.717778;
    x[1]=-0.496843;
    x[2]=-0.429063;
    x[3]=-0.3596;
    x[4]=-0.205607;
    x[5]=0.0730536;
    x[6]=0.138018;
    x[7]=0.585526;
    x[8]=2.40104;
    x[9]=3.75268;
    x[10]=4.55704;

    a3[0]=0.790832;
    a3[1]=0.569896;
    a3[2]=0.502116;
    a3[3]=0.432653;
    a3[4]=0.343625;
    a3[5]=0.512472;
    a3[6]=0.56708;
    a3[7]=1.01459;
    a3[8]=2.32799;
    a3[9]=3.67962;
    a3[10]=4.48398;

    std::cout.precision(10);
    std::copy(a3,a3+n,a1);
    for(int i=0;i<n;i++)            a1[i]+=x[i];            
    std::sort(a1,a1+n);
    for(int i=0;i<n;i++)            std::cout << a1[i] << std::endl;
    std::cout << "---" << std::endl;
    int n_1=std::unique(a1,a1+n)-a1;
    std::cout << "length of unique subvector " << n_1 << std::endl;
    std::cout << "---" << std::endl;
    for(int i=0;i<n_1;i++)         std::cout << a1[i] << std::endl;
    std::cout << "---" << std::endl;    
}
唯一数组仍然包含重复项(因此是错误的)

我做错了什么?

那么:

int n_1 = std::unique(a1,a1+n,
            [](float a, float b)
            {
                return std::fabs(a-b) < 10e-9;
            }
          ) - a1;
int n_1=std::unique(a1,a1+n,
[](浮动a、浮动b)
{
返回标准::晶圆厂(a-b)<10e-9;
}
)-a1;
?


让我们尝试更精确一点,
std::cout.precision(20)

由于大多数十进制分数不能用二进制浮点格式精确表示,因此稍有不同的舍入误差会导致稍有不同的结果

一般来说,您不能期望不同浮点计算的结果完全相等,即使应用于数学实数的相应计算可能是相等的

您可以改为测试“几乎相等”,仔细选择适合您的数值域的公差
unique
允许您指定自己的谓词,而不是简单的相等性测试:

std::unique(a1,a1+n,[](double x, double y){return std::abs(x-y) < tolerance;});
std::unique(a1,a1+n,[](双x,双y){返回std::abs(x-y)
另一个浮点值比较问题….带
std::unique的浮点运算
?您所做的是期望双精度表示。。。它不是(特别是在计算之后…)谢谢,但是我得到:'main.cpp:In lambda函数:main.cpp:42:77:error:'tolerance'未被捕获'(我已经在代码中添加了const double tolerance=1e-16;'),我想知道你是否知道更多…@user189035:如果它是一个局部变量,那么lambda需要捕获它。将
[]
替换为
[=]
[公差]
0.073052999999999979064
0.073053000000000034575
0.073053999999999952308
0.13801800000000000179
0.58552559999999997942
0.70509800000000000253
1.6001160000000000938
4.7290299999999998448
7.4322999999999996845
9.0410199999999996123
std::unique(a1,a1+n,[](double x, double y){return std::abs(x-y) < tolerance;});