C 20新特性 數學計算庫

2021-10-19 04:15:55 字數 2469 閱讀 4895

數學常數

增加了,其中定義了常用的數學常數,如e,log2e,sqrt2,sqrt3等。

bit操作

新增了一些列bit的操作,如按位判斷有幾個0,幾個1等,具體內容參加庫函式說明,用法示意如下(std::bit_cast在gcc10.2中未被支援):

void test_bit() 

endian判斷

std::endian 在中定義,僅是乙個簡單的判斷,用法示意如下:

void test_endian() 

else if constexpr (std::endian::native == std::endian::little)

else

}

std::midpoint

定義在中,std::midpoint(a,b)的意思就是a+(b-a)/2;

std::lerp

定義在中,std::lerp(a,b,t)的意思就是a+(b-a)*t;

std::accumulate

定義在中,預設形式std:: accumulate (a.begin(), a.end(), 0)表示集合a元素的累加,

比較複雜的形式std:: accumulate (a.begin(), a.end(),init, op)表示a中的元素運用op進行積累運算,即:  init[i]=op1(init[i-1],a[i])),其中i從1到n, init[0]=init, a[0]=a.begin(), a[n-1]=a.end(),最終init[n-1]就是返回值(標量)。

std::partial_sum

定義在中,與std::accumulate類似,但將每次計算的中間結果儲存到集合中,因此它的輸出是乙個集合而不是最終的乙個標量值。

std::inner_product

定義在中,預設形式std::inner_product(a.begin(), a.end(), b.begin(), 0)表示集合a和b的內積,比較複雜的形式std::inner_product(a.begin(), a.end(), b.begin(), init, op1, op2)表示a中的元素與b中的元素經過op2運算後再運用op1進行積累運算,即:  init[i]=op1(init[i-1],op2(a[i],b[i])),其中i從1到n, init[0]=init, a[0]=a.begin(), b[0]=b.begin(),a[n-1]=a.end(),最終init[n-1]就是返回值,大概最終結果是乙個標量,所以也把它叫「內積」吧。

std::adjacent_difference

定義在中,表示將op(當前元素,後乙個元素)的結果儲存到集合中,op為空時就是相減,因此叫adjacent_difference,輸出是乙個集合。

std::transform

定義在中,表示集合元素的變換,即將乙個集合中的元素經過一元運算或兩個集合中元素經過二元運算後,結果再儲存到集合中,上面涉及的集合可以是乙個集合,兩個集合或三個集合。

void test_numeric()

;int r3= std::accumulate(a.begin(), a.end(), 0); //10

auto dash_fold = (std::string a, int b) ;

std::string r4 = std::accumulate(std::next(v.rbegin()), v.rend(),

std::to_string(v.back()), // start with last element

dash_fold); //4-3-2-1-0

std::vectorv(a.size());

auto op=(const int a, const int b)->int;

std::partial_sum(a.begin(), a.end(), v.begin(), op); //

std::adjacent_difference(a.begin(), a.end(), v.begin()); //

std::vectorb;

int r5 = std::inner_product(a.begin(), a.end(), b.begin(), 0); //33

int r6 = std::inner_product(a.begin(), a.end(), b.begin(),0, std::plus<>(), std::equal_to<>()); //3

}

C 2 0的新特性

c 2.0的新特性 1 區域性型別 就是把乙個類 介面,結構 分成幾部分,系統編譯時自動的組合 利用關鍵字partial修飾 partial class a partial class a 只要類的一部分繼承乙個父類,那整個類都繼承。沒一部分都可以繼承乙個介面,則整個類繼承所有介面 類的一部分一但被...

C 2 0 有哪些新特性?

泛型 在我看來,泛型就是通過將資料型別引數化從而實現了 的更為靈活的復用,泛型的出現使得c 能夠使用同一段 來操作多種資料型別。泛型無疑是c 2.0最重大的改進,它的出現賦予了c 更強的型別安全,更好的復用,更高的效率和更清晰的約束。匿名方法 匿名方法允許我們將 直接與委託例項相關聯,使委託例項化工...

從C 2 0新特性到C 3 5新特性

一 c 2.0 新特性 1 泛型 listobj list new list obj list.add new myobject 2 部分類 partial namespace set 或在另乙個檔案中 public partial class class1 3 靜態類 public static ...