10.QT中使用STL容器
·
1.创建类

2.编写代码
#ifndef STLTEST_H
#define STLTEST_H
// 包含STL核心头文件
#include <vector>
#include <map>
#include <unordered_map>
#include <list>
#include <stack>
#include <set>
#include <algorithm>
#include <numeric>
// Qt调试输出头文件
#include <QDebug>
#include <QString>
// 独立的STL测试类
class STLTest
{
public:
// 构造函数
STLTest() = default;
// 析构函数
~STLTest() = default;
// 测试Vector相关操作
void testVector();
// 测试Map/UnorderedMap相关操作
void testMap();
// 测试List相关操作
void testList();
// 测试Stack/Set相关操作
void testStackAndSet();
// 测试STL算法
void testAlgorithm();
// 一键执行所有测试
void runAllTests();
private:
// 私有辅助函数:打印Vector
template <typename T>
void printVector(const std::vector<T>& vec, const QString& title);
// 私有辅助函数:打印Map(模板函数)
template <typename K, typename V>
void printMap(const std::map<K, V>& map, const QString& title);
};
#endif // STLTESTER_H
#include "stltest.h"
// 实现Vector打印辅助函数
template <typename T>
void STLTest::printVector(const std::vector<T>& vec, const QString& title)
{
qDebug() << "\n" << title;
QString content;
for (const auto& elem : vec) {
content += QString::number(elem) + " ";
}
qDebug() << "Content: " << content;
}
// 实现Map打印辅助函数
template <typename K, typename V>
void STLTest::printMap(const std::map<K, V>& map, const QString& title)
{
qDebug() << "\n" << title;
for (const auto& pair : map) {
qDebug() << pair.first << " → " << pair.second;
}
}
// 测试Vector
void STLTest::testVector()
{
qDebug() << "===== Start Testing STL Vector =====";
std::vector<int> vec = { 23, 7, 45, 19, 8, 3 };
// 初始状态
printVector(vec, "[Initial Vector]");
// 排序
std::sort(vec.begin(), vec.end());
printVector(vec, "[After Ascending Sort]");
// 反转
std::reverse(vec.begin(), vec.end());
printVector(vec, "[After Reversal (Descending)]");
// 查找元素
auto it = std::find(vec.begin(), vec.end(), 19);
if (it != vec.end()) {
qDebug() << "[Search Result] Found 19 at index: " << (it - vec.begin());
}
else {
qDebug() << "[Search Result] 19 not found";
}
// 累加求和
int sum = std::accumulate(vec.begin(), vec.end(), 0);
qDebug() << "[Accumulation Result] Sum of all elements: " << sum;
// 插入/删除
vec.insert(vec.begin() + 2, 99); // Insert 99 at index 2
printVector(vec, "[After Inserting 99]");
vec.erase(vec.begin() + 2); // Delete element at index 2
printVector(vec, "[After Deleting 99]");
}
// 测试Map/UnorderedMap
void STLTest::testMap()
{
qDebug() << "\n===== Start Testing STL Map/UnorderedMap =====";
// 测试std::map(有序、key唯一)
std::map<QString, QString> cityMap;
cityMap["CN"] = "China";
cityMap["US"] = "United States";
cityMap["JP"] = "Japan";
printMap(cityMap, "[std::map (Sorted by Key)]");
// 测试std::unordered_map(无序、效率更高)
qDebug() << "\n[std::unordered_map (Unordered)]";
std::unordered_map<QString, int> scoreMap;
scoreMap["XiaoMing"] = 95;
scoreMap["XiaoHong"] = 98;
scoreMap["XiaoGang"] = 89;
for (const auto& pair : scoreMap) {
qDebug() << pair.first << " → " << pair.second;
}
// 查找map中的元素
auto mapIt = cityMap.find("CN");
if (mapIt != cityMap.end()) {
qDebug() << "\n[Map Search] CN → " << mapIt->second;
}
}
// 测试List
void STLTest::testList()
{
qDebug() << "\n===== Start Testing STL List =====";
std::list<double> lst = { 3.14, 1.618, 2.718, 0.618, 1.414 };
qDebug() << "[Initial List]";
for (const auto& num : lst) {
qDebug() << num << " ";
}
// List自带sort(STL的std::sort不支持list)
lst.sort();
qDebug() << "\n[After Sorting List]";
for (const auto& num : lst) {
qDebug() << num << " ";
}
// 插入/删除
lst.push_front(0.0); // Insert at front
lst.push_back(5.0); // Insert at back
lst.pop_front(); // Delete from front
qDebug() << "\n[After Insert/Delete]";
for (const auto& num : lst) {
qDebug() << num << " ";
}
}
// 测试Stack/Set
void STLTest::testStackAndSet()
{
qDebug() << "\n===== Start Testing STL Stack/Set =====";
// 测试std::stack(后进先出)
qDebug() << "[std::stack Test]";
std::stack<int> st;
st.push(10);
st.push(20);
st.push(30);
qDebug() << "Top element: " << st.top();
st.pop();
qDebug() << "Top element after Pop: " << st.top();
qDebug() << "Stack size: " << st.size();
// 测试std::set(自动去重+排序)
qDebug() << "\n[std::set Test (Deduplication + Sorted)]";
std::set<int> s = { 5, 2, 8, 2, 9, 5, 1 };
for (const auto& num : s) {
qDebug() << num << " ";
}
}
// 测试STL算法
void STLTest::testAlgorithm()
{
qDebug() << "\n===== Start Testing STL Algorithms =====";
std::vector<int> nums = { 10, 20, 30, 40, 50 };
// 遍历+修改(for_each + lambda)
qDebug() << "[for_each] Multiply each element by 2: ";
std::for_each(nums.begin(), nums.end(), [](int& n) {
n *= 2;
qDebug() << n << " ";
});
// 统计满足条件的元素(count_if)
int cnt = std::count_if(nums.begin(), nums.end(), [](int n) {
return n > 50; // Count elements greater than 50
});
qDebug() << "\n[count_if] Number of elements greater than 50: " << cnt;
// 二分查找(需先排序)
std::sort(nums.begin(), nums.end());
bool found = std::binary_search(nums.begin(), nums.end(), 60);
qDebug() << "[binary_search] Is 60 found: " << (found ? "Yes" : "No");
}
// 一键执行所有测试
void STLTest::runAllTests()
{
qDebug() << "==================== STL Test Started ====================\n";
testVector();
testMap();
testList();
testStackAndSet();
testAlgorithm();
qDebug() << "\n==================== STL Test Finished ====================";
}
main函数中做测试即可:
#include <QCoreApplication>
#include "stltest.h"
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
// 创建STL测试类对象
STLTest stlTest;
// 方式1:执行所有测试
stlTest.runAllTests();
return 0;
}
3.配置控制台输出

4.效果

更多推荐
所有评论(0)