第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

排序比較。

排序比較。

Go
猛跑小豬 2023-03-21 15:38:16
我正在將C++代碼轉(zhuǎn)換為Go,但我很難理解這個比較函數(shù):#include <stdio.h>#include <stdlib.h>#include <math.h>#include <iostream>using namespace std;typedef struct SensorIndex{   double value;    int    index;} SensorIndex;int comp(const void *a, const void* b){   SensorIndex* x = (SensorIndex*)a;    SensorIndex* y = (SensorIndex*)b;    return abs(y->value) - abs(x->value);}int main(int argc , char *argv[]){    SensorIndex *s_tmp;    s_tmp = (SensorIndex *)malloc(sizeof(SensorIndex)*200);    for( int i=0; i < 200; ++i ) {        s_tmp[i].value = q[i];        s_tmp[i].index = i;    }    qsort(s_tmp, 200, sizeof(SensorIndex), comp);    for( int i=0; i<200; i++)    {           cout << s_tmp[i].index << " " << s_tmp[i].value << endl;    }}我預(yù)計“comp”函數(shù)將允許從最高(絕對)值到次要值排序,但在我的環(huán)境(gcc 32 位)中,結(jié)果是:1 8.418510 8.483592 -2.535853 1.6994911 -1.849085 -3.193416 3.292157 2.6820110 1.6466114 2.6378512 0.64306613 1.534724 0.003581299 -0.1405328 -0.44354915 -0.75441716 0.43107717 -0.12325618 -0.12325619 -0.12325620 -0.123256...此外,對我來說似乎很奇怪的一件事是,通過使用在線服務(wù)執(zhí)行相同的代碼,我得到不同的值(cpp.sh,C++98):0 8.483591 8.418515 -3.193416 3.292152 -2.535857 2.6820114 2.637853 1.6994910 1.6466111 -1.8490813 1.534724 0.003581298 -0.4435499 -0.14053212 0.64306615 -0.75441716 0.43107717 -0.12325618 -0.12325619 -0.12325620 -0.123256...有什么幫助嗎?
查看完整描述

2 回答

?
慕妹3242003

TA貢獻(xiàn)1824條經(jīng)驗(yàn) 獲得超6個贊

此行為是由于使用abs,一個與 一起使用的函數(shù)int,并向其傳遞double參數(shù)引起的。sdouble被隱式轉(zhuǎn)換為int,在比較它們之前截斷小數(shù)部分。從本質(zhì)上講,這意味著您采用原始數(shù)字,去除符號,然后去除小數(shù)點(diǎn)右側(cè)的所有內(nèi)容并比較這些值。所以8.123-8.9都轉(zhuǎn)換為8, 比較相等。由于減法的輸入是相反的,因此排序是按幅度降序排列的。

您的cpp.sh輸出反映了這一點(diǎn);所有大小在 8 和 9 之間的值首先出現(xiàn),然后是 3-4s,然后是 2-3s、1-2s 和小于 1 的值。

如果您想修復(fù)此問題以實(shí)際按一般降序排序,則需要一個正確使用-friendly函數(shù)的doublefabs比較函數(shù),例如

int comp(const void *a, const void* b)

{   SensorIndex* x = (SensorIndex*)a;

    SensorIndex* y = (SensorIndex*)b;

    double diff = fabs(y->value) - fabs(x->value);

    if (diff < 0.0) return -1;

    return diff > 0;

}

更新:進(jìn)一步閱讀,看起來std::absfrom<cmath>已經(jīng)與doubles 一起工作了很長時間,但std::absfor s 僅在 C++17 中double添加到<cstdlib>(整數(shù)函數(shù)所在的位置)。而且實(shí)施者總是把這些東西弄錯,所以不同的編譯器會隨機(jī)表現(xiàn)不同。無論如何,這里給出的兩個答案都是正確的;如果您沒有包含并且您使用的是 C++17 之前的編譯器,則您應(yīng)該只能訪問基于整數(shù)的版本(或from ),這將在比較之前截斷每個值。即使您使用的是正確的,將減法的結(jié)果返回abs<cmath>std::abs::absmath.hstd::absdoubleint會丟棄 difference 的小數(shù)部分,使幅度差異小于的任何值1.0看起來相等。更糟糕的是,根據(jù)執(zhí)行的特定比較及其排序(因?yàn)椴⒎撬兄刀枷嗷ケ容^),這種效果的后果可能會連鎖,因?yàn)楸容^排序更改可能會使看起來等于,而反過來又會1.0看起來等于1.6,2.5即使如果將它們相互比較,1.0則被正確識別為小于;2.5理論上,只要每個數(shù)字與其他數(shù)字的差值在 1.0 以內(nèi),比較的結(jié)果就好像它們彼此相等(病態(tài)情況是的,但肯定會發(fā)生較小的此類錯誤)。

關(guān)鍵是,弄清楚這段代碼的真正意圖的唯一方法是弄清楚它最初編譯的確切編譯器版本和 C++ 標(biāo)準(zhǔn),并在那里進(jìn)行測試。


查看完整回答
反對 回復(fù) 2023-03-21
?
尚方寶劍之說

TA貢獻(xiàn)1788條經(jīng)驗(yàn) 獲得超4個贊

您的比較功能中存在錯誤。你返回 anint這意味著你失去了絕對差異小于 的元素值之間的區(qū)別!1


int comp(const void* a, const void* b)

{

    SensorIndex* x = (SensorIndex*)a;

    SensorIndex* y = (SensorIndex*)b;


    // what about differences between 0.0 and 1.0?

    return abs(y->value) - abs(x->value); 

}

您可以這樣修復(fù)它:


int comp(const void* a, const void* b)

{   SensorIndex* x = (SensorIndex*)a;

    SensorIndex* y = (SensorIndex*)b;


    if(std::abs(y->value) < std::abs(x->value))

        return -1;


    return 1;

}

一種更現(xiàn)代(也更安全)的方法是使用std::vectorand std::sort:


// use a vector for dynamic arrays

std::vector<SensorIndex> s_tmp;


for(int i = 0; i < 200; ++i) {

    s_tmp.push_back({q[i], i});

}


// use std::sort

std::sort(std::begin(s_tmp), std::end(s_tmp), [](SensorIndex const& a, SensorIndex const& b){


    return std::abs(b.value) < std::abs(a.value);

});


查看完整回答
反對 回復(fù) 2023-03-21
  • 2 回答
  • 0 關(guān)注
  • 135 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號