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 }
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 += '