实验2 数组、指针与C++标准库
四、实验结论
1.实验任务1-4
归纳整理:
-
普通数组、array、vector的相关性,以及,区别
-
内存分配方式:数组和array都是静态内存分配,位于栈中。vector使用动态内存分配方式,位于堆中。
-
效率上:数组=array>vector
-
复制:vector和array都可以把一个对象直接复制给另一个对象,数组只能逐元素复制。
-
-
迭代器与指针相关性,以及,区别
相关性:
-
指针和iterator都支持与整数进行+,-运算,而且其含义都是从当前位置向前或者向后移动n个位置。
-
指针和iterator都支持减法运算,指针-指针得到的是两个指针之间的距离,迭代器-迭代器得到的是两个
迭代器之间的距离。 -
通过指针或者iterator都能够修改其指向的元素。
区别:
-
cout操作符可以直接输出指针的值,但是对迭代器进行在操作的时候会报错。通过看报错信息和头文件知道,迭代器返回的是对象引用而不是对象的值,所以cout只能输出迭代器使用*取值后的值而不能直接输出其自身。
-
指针能指向函数而迭代器不行,迭代器只能指向容器
这就说明了迭代器和指针其实是完全不一样的概念来的。指针是一种特殊的变量,它专门用来存放另一变量的地址,而迭代器只是参考了指针的特性进行设计的一种STL接口。
-
-
C风格字符串与string区别
最大的区别就是C风格的字符串是静态的,不可以动态变化,使用极为麻烦。而C++的std::string类型动态管理,非常方便。
C风格字符串和char数组是不一样的,看下面两种定义:
char carr1 = {'a', 'b', 'c'};
char carr2 = {'a', 'b', 'c', '\0'};
看上面,carr2可以说成是C风格字符串,carr1就不是C风格字符串,C风格字符串必须要以'\0'结尾的。
string类是标准库的类,并不是内置类型,标准库就像是我们自己定义的类差不多的,string类型对象没有标配'\0'结尾的。如果需要用string类给C风格字符串赋值的话,后面是需要添加'\0'的。
2.实验任务5
-
Info.hpp文件源码
#ifndef info_hpp #define info_hpp #include <iostream> using namespace std; class info { public: info(string x, string y, string z, int w) : nickname{x}, contact{y}, city{z}, n{w} {}; ~info() = default; void print() const; private: string nickname, contact, city; int n; }; void info::print() const { cout << "称呼: " << nickname << "\n"; cout << "联系方式: " << contact << "\n"; cout << "所在城市: " << city << "\n"; cout << "预定人数: " << n << "\n"; return; } #endif -
task5.cpp源码
#include "info.hpp" #include <iostream> #include <vector> using namespace std; const int capacity = 100; int main() { vector<info> audience_info_list; string x, y, z; int w, sum = 0; char op; cout << "录入信息:\n\n"; cout << "称呼/昵称," << "联系方式(邮箱/手机号)," << "所在城市," << "预定参加人数" << endl; while (cin >> x >> y >> z >> w) { if (sum + w > capacity) { cout << "对不起,只剩" << capacity - sum << "个位置。" << endl; cout << "1. 输入u,更新(update)预定信息" << endl; cout << "2. 输入q,退出预定" << endl; cout << "你的选择:"; cin >> op; if (op == 'q') break; else continue; } info a(x, y, z, w); sum += w; audience_info_list.push_back(a); if (sum == capacity) break; } cout << endl; cout << "截至目前,一共有" << sum << "位听众预定参加。预定听众信息如下:" << endl; for (auto i : audience_info_list) { i.print(); } return 0; } -
运行测试结果截图
![]()
![]()
3.实验任务6
-
TextCoder.hpp文件源码
#ifndef textcoder_hpp #define textcoder_hpp #include <iostream> using namespace std; class TextCoder { public: TextCoder(string x) : text{x} {}; ~TextCoder() = default; string encoder(); string decoder(); private: string text; }; string TextCoder::encoder() { for (int i = 0; i < text.length(); i++) { if (text[i] >= 'a' && text[i] <= 'z') { text[i] = 'a' + (text[i] - 'a' + 5) % 26; } if (text[i] >= 'A' && text[i] <= 'Z') { text[i] = 'A' + (text[i] - 'A' + 5) % 26; } } return text; } string TextCoder::decoder() { for (int i = 0; i < text.length(); i++) { if (text[i] >= 'a' && text[i] <= 'z') { text[i] = 'a' + (text[i] - 'a' + 21) % 26; } if (text[i] >= 'A' && text[i] <= 'Z') { text[i] = 'A' + (text[i] - 'A' + 21) % 26; } } return text; } #endif -
task6.cpp源码
#include "textcoder.hpp" #include <iostream> #include <string> int main() { using namespace std; string text, encoded_text, decoded_text; cout << "输入英文文本: "; while (getline(cin, text)) { encoded_text = TextCoder(text).encoder(); // 这里使用的是临时无名对象 cout << "加密后英文文本:\t" << encoded_text << endl; decoded_text = TextCoder(encoded_text).decoder(); // 这里使用的是临时无名对象 cout << "解密后英文文本:\t" << decoded_text << endl; cout << "\n输入英文文本: "; } } -
运行测试结果截图
![]()



浙公网安备 33010602011771号