实验1 类和对象

发布时间 2023-10-13 10:37:34作者: cc00

实验任务1

task1.cpp源码

  1 // 标准库string, vector, array基础用法
  2 
  3 #include <iostream>
  4 #include <string>
  5 #include <vector>
  6 #include <array>
  7 
  8 // 函数模板
  9 // 对满足特定条件的序列类型T对象,使用范围for输出
 10 template<typename T>
 11 void output1(const T &obj) {
 12     for(auto i: obj)
 13         std::cout << i << ", ";
 14     std::cout << "\b\b \n";
 15 }
 16 
 17 // 函数模板
 18 // 对满足特定条件的序列类型T对象,使用迭代器输出
 19 template<typename T>
 20 void output2(const T &obj) {
 21     for(auto p = obj.begin(); p != obj.end(); ++p)
 22         std::cout << *p << ", ";
 23     std::cout << "\b\b \n";
 24 }
 25 
 26 // array模板类基础用法
 27 void test_array() {
 28     using namespace std;
 29 
 30     array<int, 5> x1;   // 创建一个array对象,包含5个int元素,未初始化
 31     cout << "x1.size() = " << x1.size() << endl;  // 输出元素个数
 32     x1.fill(42);        // 把x1的所有元素都用42填充
 33     x1.at(0) = 999;     // 把下标为0的元素值修改为999
 34     x1[4] = -999;       // 把下表为4的元素值修改为-999
 35     cout << "x1: ";
 36     output1(x1);        // 调用模板函数output1输出x1
 37     cout << "x1: ";
 38     output2(x1);        // 调用模板函数output1输出x1
 39 
 40     array<int, 5> x2{x1};   
 41     cout << boolalpha << (x1 == x2) << endl;
 42     x2.fill(22);
 43     cout << "x2: ";
 44     output1(x2);
 45 
 46     swap(x1, x2);   // 交换array对象x1, x2
 47     cout << "x1: ";
 48     output1(x1);
 49     cout << "x2: ";
 50     output1(x2);
 51 }
 52 
 53 // vector模板类基础用法
 54 void test_vector() {
 55     using namespace std;
 56 
 57     vector<int> v1;
 58     cout << v1.size() << endl;      // 输出目前元素个数
 59     cout << v1.max_size() << endl;  // 输出元素个数之最大可能个数
 60     v1.push_back(55);               // 在v1末尾插入元素
 61     cout << "v1: ";
 62     output1(v1);
 63 
 64     vector<int> v2 {1, 0, 5, 2};
 65     v2.pop_back();                  // 从v2末尾弹出一个元素
 66     v2.erase(v2.begin());         // 删除v2.begin()位置的数据项
 67     v2.insert(v2.begin(), 999);     // 在v1.begin()之前的位置插入
 68     v2.insert(v2.end(), -999);      // 在v1.end()之前的位置插入
 69     cout << v2.size() << endl;
 70     cout << "v2: ";
 71     output2(v2);
 72 
 73     vector<int> v3(5, 42);         //创建vector对象,包含5个元素,每个元素值都是42
 74     cout << "v3: ";
 75     output1(v3);
 76 
 77     vector<int> v4(v3.begin(), v3.end()-2); // 创建vector对象,以v3对象的[v3.begin(), v3.end()-2)区间作为元素值
 78     cout << "v4: ";
 79     output1(v4);
 80 }
 81 
 82 // string类基础用法
 83 void test_string() {
 84     using namespace std;
 85 
 86     string s1{"oop"};
 87     cout << s1.size() << endl;
 88     for(auto &i: s1)
 89         i -= 32;
 90     s1 += "2023";
 91     s1.append(", hello");
 92     cout << s1 << endl;
 93 }
 94 
 95 int main() {
 96     using namespace std;
 97 
 98     cout << "===========测试1: array模板类基础用法===========" << endl;
 99     test_array();
100 
101     cout << "\n===========测试2: vector模板类基础用法===========" << endl;
102     test_vector();
103 
104     cout << "\n===========测试3: string类基础用法===========" << endl;
105     test_string();
106 }
View Code

 

运行测试结果:

 

实验任务2

实验任务3