cs11_adventure c++_lab1

exercise1.ccios

 1 #include <iostream>
 2 #include <vector>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 
 6 using namespace std;
 7 
 8 int myFunction()
 9 {
10     return rand() % 100 + 1;
11 }
12 
13 int main()
14 {
15     vector<int> vi1(100);
16     generate(vi1.begin(), vi1.end(), myFunction);//可用generate函數初始化,與下功能一致
17 /*
18     for(int i = 0; i < 100; i++)
19     {
20         vi1[i] = (rand() % 100 + 1);
21     }
22 */
23     vector<int> vi2(vi1.size());
24     copy(vi1.begin(), vi1.end(), vi2.begin());
25 
26     for(int i = 0; i < 100; i++)
27         cout << i << "        "<< vi1[i] << "    " << vi2[i] << endl;
28 }
View Code

exercise2.ccdom

 1 #include <iostream>
 2 #include <vector>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 string randomString()
10 {
11     int i = rand() % 11 + 5;//隨機字符串長度
12     string str = "";
13     for(int j = 0; j < i; j++)
14     {
15         str += 'a' + rand() % 26;//隨機字符串中內容
16     }
17     str += '\0';//字符串末尾處理
18     return str;
19 }
20 
21 bool myFunction(string s1, string s2)//自定義謂詞1
22 {
23     return s1.size() < s2.size();
24 }
25 
26 struct myClass//自定義謂詞2
27 {
28     bool operator()(string s1, string s2)
29     {
30         return s1.size() < s2.size();
31     }
32 }myObject;
33 
34 /*
35 class myClass//自定義謂詞2
36 {
37 public:
38     bool operator()(string s1, string s2)
39     {
40         return s1.size() < s2.size();
41     }
42 };
43     myClass myObject;
44 */
45 
46 int main()
47 {
48     vector<string> vs(100);
49     vector<string>::iterator vsi;
50 
51     for(vsi = vs.begin(); vsi != vs.end(); vsi++)//填充隨機字符串
52     {
53         *vsi = randomString();
54     }
55 
56     for(int j = 0; j < 100; j++)//輸出原始隨機字符串
57     {
58         cout << j << "      " << vs[j] << endl;
59     }
60     cout << "原始值----------------------------" << endl;
61 
62     sort(vs.begin(), vs.end());//默認排序並輸出
63     for(int j = 0; j < 100; j++)
64     {
65         cout << j << "      " << vs[j] << endl;
66     }
67     cout << "第一次字母序排序------------------------"<< endl;
68 
69     sort(vs.begin(), vs.end(), myFunction);//自定義謂詞1排序並輸出
70     sort(vs.begin(), vs.end(), myObject);//自定義謂詞2排序並輸出
71     for(int j = 0; j < 100; j++)
72     {
73         cout << j << "      " << vs[j] << endl;
74     }
75     cout << "第二次按長度排序------------------------"<< endl;
76 }
View Code

exercise3.ccide

 1 #include <iostream>
 2 #include <vector>
 3 #include <stdlib.h>
 4 #include <algorithm>
 5 #include <string>
 6 
 7 using namespace std;
 8 
 9 int randomNum()
10 {
11     return rand() % 100 + 1;
12 }
13 
14 struct myClass//自定義謂詞
15 {
16     int even;
17     int odd;
18     bool operator()(int num)
19     {
20         if(num % 2)
21         {
22             even++ ;
23             return true;
24         }
25         else
26         {
27             odd++;
28             return false;
29         }
30     }
31 }myObject;
32 
33 int main()
34 {
35     vector<int> vi(100);
36     vector<int>::iterator itor;
37 
38     for(itor = vi.begin(); itor != vi.end(); itor++)//填充隨機字符串
39     {
40         *itor = randomNum();
41     }
42 
43     for(int i = 0; i < 100; i++)//輸出原始隨機字符串
44     {
45         cout << i << "      " << vi[i] << endl;
46     }
47     cout << "原始值----------------------------" << endl;
48 
49     myClass result = for_each(vi.begin(), vi.end(), myClass());//第一種調用方式,帶狀態
50     myClass result = for_each(vi.begin(), vi.end(), myObject);//第二種調用方式,可是,其中odd和even是如何初始化爲零的?
51     cout << result.odd << endl << result.even << endl;
52 }
View Code

exercise4.cc函數

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <iterator>
 5 #include <ext/functional>//爲支持compose1函數額外添加的頭文件
 6 
 7 using namespace std;
 8 
 9 int main()
10 {
11     //初始化vector
12     vector<int> v;
13     v.push_back(1);
14     v.push_back(4);
15     v.push_back(2);
16     v.push_back(8);
17     v.push_back(5);
18     v.push_back(7);
19 
20     //把vector中內容經過指定的流迭代器寫入到指定流中,放一個整數,後跟一個「 」
21     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
22     cout << endl;
23 
24     //remove_if移除序列中謂詞返回true的元素,可是容器長度不變,全部元素還在容器中,實際上是把全部應移除元素至於容器尾部並返回一個分界迭代器
25     //compose1(f,g)函數執行順序是f(g),先執行g,把g的結果做爲f的參數
26     //bind2nd函數是把一個二元謂詞轉化成一元謂詞的函數,綁定第二個參數,使之成爲一個一元謂詞
27     //modulus函數是取模函數,被綁定模2
28     //那麼全部的偶數都被標記,非偶數都被至於前面,返回的是指向8的迭代器
29     vector<int>::iterator new_end =
30             remove_if(v.begin(), v.end(),
31                     __gnu_cxx::compose1(bind2nd(equal_to<int>(), 0),
32                                 bind2nd(modulus<int>(), 2)));
33 
34     copy(v.begin(), v.end(), ostream_iterator<int>(cout, " "));
35     cout << endl;
36 }
View Code

exeercise5.ccspa

 1 #include <iostream>
 2 #include <vector>
 3 #include <algorithm>
 4 #include <iterator>
 5 
 6 using namespace std;
 7 
 8 template <typename BidirectionalIterator>
 9 void my_reverse(BidirectionalIterator first, BidirectionalIterator last)
10 {
11     if(distance(first, last) == 0)//當什麼都沒有的狀況
12     {
13         cout<<"爲零換個屁啊"<<endl;
14         return;
15     }
16     else
17     {
18         while(distance(first, last-1) > 0)//這是考慮除了迭代器重合外的全部狀況,只有在兩迭代器指向不一樣位置的時候纔會對換,指向同一位值就不對換了
19         {
20             cout<<*first<<"---"<<*(last-1)<<endl;//對換的結構
21             swap(*first, *(last-1));//注意,不是first和last對換,那是迭代器互相賦值,不對。也不是*first和*last之間的對換,由於last指向的是最後元素以後的元素,也不對。
22             first++;
23             last--;
24         }
25     }
26 }
27 
28 int myFunction()
29 {
30     return rand() % 100 + 1;
31 }
32 
33 int main()
34 {
35     int size;
36     cout << "input size: " << endl;
37     cin >> size;
38     vector<int> vi(size);
39 
40     generate(vi.begin(), vi.end(), myFunction);
41     copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
42     cout << endl;
43 
44     my_reverse(vi.begin(), vi.end());
45     copy(vi.begin(), vi.end(), ostream_iterator<int>(cout, " "));
46     cout << endl;
47 }
View Code
相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息