amdb

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

exercise1.cc

 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.cc

 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.cc

 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.cc

 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

 

posted on 2015-05-01 15:22  amdb  阅读(298)  评论(0编辑  收藏  举报