2.#
点击查看考点
排序, 去除重复值
点击查看往年相同题目
与 计软智 2020 年第 4 题 极为相似, 也是计算机大类实验手册上某道题的考点. 但注意这题没有提到 set, 所以我们可以直接用 set 实现; 而那题直接说了是要实现 set, 直接用 set 很可能不给分.
警告
以下内容考试中是用英文写在纸上, 也就是代码需要自己抄一遍到电脑里, 所以 要学会正确使用键盘, 加快打字速度!
Please complete the program while keeping interfaces and implementations seperated.
The program implements a class called List, which can store any number of std::string objects, and satisfies the following invariants,
std::string* data_The member that holds all
std::stringobjects.
int size_The member that indicates the amount of
std::stringobjects; if no object exists,size_ == 0.
Element uniqueness
For any two different objects
string1andstring2holded bydata_,string1 == string2(equality) should not be true.Also, when inserting a new object, the insertion should not happen if there's already an equal object.
You should implement the following functions for it,
Constructor
List(std::string* data, int size)Insert all elements inside
[data, data + size)intodata_.
Copy constructor
Copy assignment
auto operator+=(List const& other) -> List&Insert all elements inside
otherinto*this.
friend auto operator+(List const& lhs, List const& rhs) -> ListMerge two
Listas aList.
friend auto operator<<(std::ostream& ostream, List const& list) -> std::ostream&Output the stored
std::stringobjects with the format[element_1, element_2, ..., element_n].
The following has given part of the List's implementation and the main() function.
翻译
请保持接口与实现相分离地完成程序.
该程序实现了一个类 List, 它可以存储任意数量的 std::string 对象, 且满足以下不变式,
std::string* data_保存存储的
std::string对象.
int size_表示存储的
std::string数量; 当不存储有std::string对象时,size_ == 0.
元素唯一性
对于
data_中存储的std::string对象, 不存在两个对象string1和string2, 使得string1 == string2(称为相等) 成立.同时, 当插入新对象时, 若
data_中已存在相等的对象时, 则不进行插入.
为其实现,
构造函数
List(std::string* data, int size)将
[data, data + size)中的元素加入data_中.
拷贝构造函数
拷贝赋值函数
auto operator+=(List const& other) -> List&将
other中的元素加入*this中.
friend auto operator+(List const& lhs, List const& rhs) -> List合并两个
Set为一个新的Set.
friend auto operator<<(std::ostream& ostream, List const& list) -> std::ostream&按
[元素1, 元素2, ..., 元素n]的格式输出存储的std::string对象.
以下已给出 List 的部分实现以及 main() 函数.
1#ifndef MY_LIST_HPP
2#define MY_LIST_HPP
3
4#include <iosfwd>
5#include <string>
6/* 你可以添加任意标准库头文件 */
7
8class MyList {
9 public:
10 // 请补充
11
12 private:
13 std::string* data_;
14 int size_;
15};
16
17#endif
1#include "my_list.hpp"
2
3#include <iostream>
4#include <string>
5
6auto main() -> int {
7 std::string s1[] = {"John", "Bat-man", "Doctor", "John"};
8 std::string s2[] = {"Doctor", "Smith", "Komeji", "Smith"};
9 std::string s3[] = {"Komeji", "Satori", "Momo", "Satori"};
10
11 MyList list1(s1, 4);
12 MyList list2(s2, 4);
13 MyList list3;
14
15 std::cout << list1 << '\n';
16 std::cout << list2 << '\n';
17 std::cout << list3 << '\n';
18
19 list3 = list2;
20 list1 += list2;
21 list1 = list1 + list3;
22
23 std::cout << list1 << '\n';
24 std::cout << list2 << '\n';
25 std::cout << list3 << '\n';
26}
点击查看解答参考
题目代码虽然给出了指针, 暗示需要 new 一个动态数组, 但其实可以在实现时用 std::set 取巧简化. 简化后最复杂的代码反而是笔者无聊加上的输出格式要求.
不使用 std::set 地, 你可以用 std::vector 和 STL 算法 std::sort、std::unique 简单实现: [在线代码 njE6qdfKM].
另可参考我针对别人问的类似问题, 完全自己实现这些的方案: