文章目录
STL简单测试题
1.给定一个vector,如何使用STL中的算法和迭代器来计算其中的最大值和最小值?
第一次代码:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vIntA = { 100,1,20,30,40 };
//(错误)
//从小到大排序
std::sort(vIntA.begin(), vIntA.end());
auto vInt_Min = vIntA.begin();
auto vInt_Max = vIntA.end();
std::cout << "Max:" << *vInt_Max << std::endl;
std::cout << "Min:" << *vInt_Min << std::endl;
return 0;
}
错误编译提示
将一个无效参数传递给了将无效参数视为严重错误的函数。
查阅资料发现,代码中定义的vInt_Max迭代器指向的是vIntA.end(),而不是vIntA中的最大值。因此,输出的最大值是未定义的,可能会导致程序出现错误(vInt_Min没有影响)
正确代码
那么正确代码应是:
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vIntA = { 100,1,20,30,40 };
//(错误)
//从小到大排序
std::sort(vIntA.begin(), vIntA.end());
auto vInt_Min = vIntA.begin();
auto vInt_Max = vIntA.end() - 1;
std::cout << "Max:" << *vInt_Max << std::endl;
std::cout << "Min:" << *vInt_Min << std::endl;
return 0;
}
将vIntA.end()往前移一位,就指向的是最后一个元素也就是最大值。
第二次代码
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vIntA = { 100,1,20,30,40 };
auto vInt_Min = std::max_element(vIntA.begin(), vIntA.end());
auto vInt_Max = std::min_element(vIntA.begin(), vIntA.end());
std::cout << "Max:" << *vInt_Max << std::endl;
std::cout << "Min:" << *vInt_Min << std::endl;
std::cout << std::endl;
return 0;
}
特点
使用了max_element和min_element算法来进行排序,会使程序更为简单
存在问题
上面两次代码,在容器为空时,都会出现问题。对于一个空容器,调用end()
函数将得到一个指向容器尾部的迭代器,但是这个迭代器并不能用于访问容器中的元素,因为它指向的是一个不存在的元素。因此,在处理空容器时,需要特别注意迭代器的使用。
优化代码
加入一个判断是否为空的条件
#include <iostream>
#include <vector>
#include <algorithm>
int main() {
std::vector<int> vIntA = { 100,1,20,30,40 };
if (vIntA.empty()) {
std::cout << "The vector is empty!!" << std::endl;
}
else {
auto vInt_Min = std::max_element(vIntA.begin(), vIntA.end());
auto vInt_Max = std::min_element(vIntA.begin(), vIntA.end());
std::cout << "Max:" << *vInt_Max << std::endl;
std::cout << "Min:" << *vInt_Min << std::endl;
std::cout << std::endl;
}
return 0;
}
2.给定一个vector,如何使用STL中的算法和迭代器来计算其中的平均字符串长度?
第一次代码:
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main() {
std::vector<std::string> vStrA = { "Hello", "World!", "This", "is", "the", "STL", "test" };
int sum = 0;
for (int i = 0; i < vStrA.size(); i++) {
sum += vStrA[i].length();
}
std::cout << std::endl;
double Str_len = (double)sum / vStrA.size();
std::cout << "平均字符串长度:" << Str_len << std::endl;
return 0;
}
优点:
简单,小孩子都会写;
缺点:
处理大量字符串时间复杂度为O(n),时间较长。可以使用STL算法进行优化。
第二次代码
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
int main() {
std::vector<std::string> vStrA = { "Hello", "World!", "This", "is", "the", "STL", "test" };
double avgLength = 0.0;
if (!vStrA.empty()) {
int totalLength = std::accumulate(vStrA.begin(), vStrA.end(), 0, [](int sum, const std::string& str) { return sum + str.length(); });
avgLength = (double) totalLength / vStrA.size();
}
std::cout << "平均字符串长度: " << avgLength << std::endl;
return 0;
}
解释代码
使用了accumulate算法进行求和,accmulate参数如下:
(vStrA.begin(), vStrA.end(), 0, [](int sum, const std::string& str) { return sum + str.length(); })
第一个参数是字符串的起始位置,第二个参数是字符串的终点位置,第三个是init求和初始化,即为求和累加器,第四个是一个Lambda表达式,用于自定义累加操作(若没有第四个参数,即为起始位置到终点位置和累加器的和)
重点解释Lambda表达式:
- '[]'为捕获capture,用于捕获外部作用域的变量,使得Lambda表达式可以访问这些变量。在本例中不需要,即为空。
- “int sum, const std::string& str”,表示传入两个参数,即传入sum和str。
- { return sum + str.length(); },为Lambda表达式的表达式体,即为要通过这个Lambda表达式做的事,此例中指返回sum+str.length()
优点
处理字符串速度较快,对计算机很友好
缺点
晦涩难懂,对人很不友好