HW10 C 11及高階特性作業

2022-08-01 06:21:11 字數 4730 閱讀 4066

1.高階函式f(x)

lisp語言中有高階函式的概念,即函式可以作為函式的引數,也可以作為函式的返回值。例如:

(define (f n)   (lambda (x) (+ x n)))

定義了乙個函式f,該函式的返回值是另乙個函式,假定稱為g即 (lambda (x) (+ x n) 此函式功能是引數為x,返回值為x + n。 於是 ((f 7) 9)   如下執行

(f 7)以引數7呼叫f, f的返回值是g,n的值為7

((f 7) 9)等價於 (g 9),即以引數9呼叫g。 因 n = 7, x = 9 ,因此(g 9)返回值為16

編寫乙個c++的通用函式模板f,使之能完成類似於lisp函式f的功能。 對於下面的程式,輸出結果是

16world hello!

world!

#include using namespace std;
// 在此處補充你的**
int main()

輸入無輸出

16world hello!

world!

樣例輸入

樣例輸出

16

world hello!

world!

提示

c++函式模板例項化時,也可以通過在<>中指定型別引數所對應的具體型別來實現。

1 #include 2

using

namespace

std;3//

在此處補充你的**

45 template

6class

f10 t2 operator

()(t2 b)

13};

1415

intmain()

16

2.高階函式combine

描述lisp語言中有高階函式的概念,即函式可以作為函式的引數,也可以作為函式的返回值。例如:

(define (square x)  (* xx))   定義了乙個求x的平方的函式

(define (inc x)  (+ x1))     定義了乙個求x+1的函式

(define (combine f g)      (lambda (x) (f (+ (fx) (g x)))))

(combine f g) 返回函式k , k(x) = f( f(x)+g(x))

因此 ((combine square inc) 3) 的返回值就是169

此處f(x) = x*x      g(x) = x + 1

k(x) = (x*x+(x+1)) ^2

((combine square inc) 3) 即是 k(3)

因此返回值為169 用c++實現類似的combine函式模板,使得下面的程式輸出結果為

16910.75

#include using namespace std;
// 在此處補充你的**
int main()

; auto inc = (double a) ;

cout << combine(square,inc)(3) << endl;

cout << combine(inc,square)(2.5) << endl;

return 0;

}

輸入無輸出

16910.75

樣例輸入

樣例輸出

169

10.75

提示

c++函式模板例項化時,也可以通過在<>中指定型別引數所對應的具體型別來實現。

1 #include 2

using

namespace

std;3//

在此處補充你的**

4 template

5class

combine

9 t3 operator

()(t3 c)

12};

1314

intmain()15;

17 auto inc = (double a) ;

18 cout << combineint>(square,inc)(3) <

19 cout << combinedouble>(inc,square)(2.5) <

2021

return0;

22 }

這個要注意的就是square和inc都是函式指標不是變數orz

3.自己實現bitset

描述程式填空,實現乙個類似stl bitset的 mybitset, 輸出指定結果

#include #include using namespace std;

template struct mybitset

; void set(int i,int v)

// 在此處補充你的**
void print() 

};int main()

bs.print();

cin >> i >> j >> k >> v;

bs[k] = v;

bs[i] = bs[j] = bs[k];

bs.print();

cin >> i >> j >> k >> v;

bs[k] = v;

(bs[i] = bs[j]) = bs[k];

bs.print();

} return 0;

}

輸入

多組資料

每組資料:

第一行是整數 n , 1 <= n < 20;

第二行是n個整數 k1,k2... kn,均在範圍 [0,19]內。

第三行是 四個整數 i1,j1,k1,v1 。 0 <= i1,j1,k1 <= 19, v1值為0或1

第三行是 四個整數 i2,j2,k2,v2 。 0 <= i2,j2,k2 <= 19, v2值為0或1

輸出對每組資料,共輸出3行,每行20位,每位為1或者0。最左邊稱為第0位

第一行: 第 k1,k2 ... kn位為1,其餘位為0。

第二行: 將第一行中的第 i1,j1,k1位變為 v1,其餘位不變

第三行: 將第二行中的第i2位和k2位變為v2,其餘位不變

樣例輸入

4

0 1 2 8

7 19 0 1

7 2 8 011

1 1 1 0

1 1 1 1

樣例輸出

11100000100000000000

11100001100000000001

11100000000000000001

01000000000000000000

00000000000000000000

01000000000000000000

提示

推薦使用內部類,內部類中使用引用成員。引用成員要在建構函式中初始化。

1 #include 2 #include 3

using

namespace

std;

4 template //

函式模板還可以這麼用

5struct

mybitset 6;

9void set(int i,int

v) 17

//在此處補充你的**

18int bitt[20

];19

bool flag = false; //

用來記錄字串是否已經被轉化成陣列過

20void

transform()31}

32 flag = true

; 33}34

int & operator(int

x) 38

39void

print()

4445

};46

47int

main()

4858

bs.print();

59 cin >> i >> j >> k >>v;

60 bs[k] = v; //

過載中括號

61 bs[i] = bs[j] =bs[k];

62bs.print();

63 cin >> i >> j >> k >>v;

64 bs[k] =v;

65 (bs[i] = bs[j]) =bs[k];

66bs.print();67}

68return0;

69 }

嗷我又參考網上題解了,然後我發現像他一樣寫成那種內部類沒有啥意義,就改成了現在這樣。要注意的是標黃那一行,要先強轉成unsigned char再傳給int!要不然就會出現符號位的問題。這是我請教了學弟才明白的結果orz但其實這種方法一看就不是正解,因為相當於用了int陣列來存,本來的char陣列就廢了。

但其實這道題正經的思路是:

附上學弟的話:如果要在原來的類裡直接過載的話需要返回乙個那個位的引用,mybitset裡只有兩個char型,不太方便,所以可以再寫乙個類來代替每一位。思路就是用乙個內部類來代替每一位達到用=修改的目的。

c 11特性之std thread 高階

部落格 c 11特性之std thread 初識 std thread 講了std thread join和std thread detach的用法。今天就再來點深入的。先看看這個 int main 一旦 detached,就不能再join 這個時候總要有新東西出場了 joinable includ...

C 補充及C 11特性

三 c 11新特性變參模板 完美 和emplace explicit關鍵字,作用時表面該建構函式是顯式的不能進行隱式的轉換。include include using namespace std class human human int age,string name private int ag...

c 11特性之std thread 高階二

繼續c 11的std thread之旅!下面討論如何給執行緒傳遞引數 這個例子是傳遞乙個string include include include void thread function std string s int main 如果執行,我們可以從輸出結果看出傳遞成功了。良好程式設計習慣的人...