C++ 多项选择题:字符串、序列和集合运算
第三章选择题组(二)
题目描述
请完成以下多项选择题,并将答案输出。
第 1 题
关于字符串和序列的字典序,下列比较正确的有:
-
A. (1,2,3) = (1,2,3).
-
B. (1,11,2) = (1,1,1,2).
-
C. 'aaaa' < 'aaaaa'.
-
D. 'ababa' < 'abaab'.
第 2 题
关于集合的运算,下列比较正确的有:
-
A. {1,2,3} ∪ {2,3,4}={1,4}。
-
B. {唐老师,王老师,窦老师} ∩ {唐老师,王老师,窦老师}={唐老师,王老师,窦老师}。
-
C. {1,2,3} - {2,3,4}={1}。
示例代码
// 第1题答案:A
// 第2题答案:C
#include <iostream>
#include <set>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
// 第1题
cout << "第1题答案:";
if (make_tuple(1, 2, 3) == make_tuple(1, 2, 3)) {
cout << "A";
} else if (make_tuple(1, 11, 2) == make_tuple(1, 1, 1, 2)) {
cout << "B";
} else if (string("aaaa") < string("aaaaa")) {
cout << "C";
} else if (string("ababa") < string("abaab")) {
cout << "D";
}
cout << endl;
// 第2题
cout << "第2题答案:";
set<int> set1 = {1, 2, 3};
set<int> set2 = {2, 3, 4};
set<int> set3;
set_difference(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(set3, set3.begin()));
if (set3 == set<int>{1}) {
cout << "C";
} else if (set1.union(set2) == set<int>{1, 2, 3, 4}) {
cout << "A";
} else if (set1.intersection(set2) == set<int>{1, 2, 3}) {
cout << "B";
}
cout << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/knsZ 著作权归作者所有。请勿转载和采集!