对比Vector与List操作时间,以及用日志打印操作时间

此博客通过代码实例比较了C++ STL中的Vector和List在执行erase操作时的时间效率。内容指出,List的erase操作时间复杂度为O(n),而Vector的操作时间复杂度为平方级别。实验旨在展示不同数据结构在特定操作下的性能差异。
#include <list>
#include <string>
#include <time.h>
#include <sys/timeb.h>
using namespace std;

char * log_Time(void)
{
	struct tm *ptm;
	struct timeb stTimeb;
	static char szTime[19];

	ftime(&stTimeb);
	ptm = localtime(&stTimeb.time);
	sprintf(szTime, "%02d-%02d %02d:%02d:%02d.%03d",
		ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, ptm->tm_sec, stTimeb.millitm);
	szTime[18] = 0;
	return szTime;
}

template<typename Container>
void removeEveryOtherItem(Container & lst)
{
	typename Container::iterator itr = lst.begin();
	while (itr != lst.end())
	{
		itr = lst.erase(itr);
		if (itr != lst.end())
			++itr;
	}
}



int main()
{
	int vecSize = 800000;
	vector<int> data_vector;
	list<int>	data_list;
	
	cout << log_Time() << endl;
	cout << "插入Vector: " <<log_Time() << endl;
	for (int i = 0; i < vecSize; i++)
	{
		data_vector.push_back(i);
	}

	cout << "插入到List: " <<log_Time() << endl;
	for (int i = 0; i < vecSize; i++)
	{
		data_list.push_back(i);
	}

	cout <<"开始删除vector:" <<log_Time() << endl;
	removeEveryOtherItem<vector<int>>(data_vector);

	cout << "开始删除list:" << log_Time() << endl;
	removeEveryOtherItem<list<int>>(data_list);

	cout << "删除结束" << log_Time() << endl;

	return 0;
}


本代码示例对表(vector和list)进行erase操作,分别是通过操作Vector和List来显示其操作性能。例如表中有1,2,3,4,5,间隔操作删除后,剩下的表是2,4。对于list操作的时间消耗是O(n),对于Vector消耗的时间为平方级别。



<think>好的,我需要解决用户的问题:如何在Qt中遍历std::list并获取其中的值。用户还希望看到示例代码。首先,我得回忆一下Qt的容器和标准库容器的区别。用户提到的是std::list,所以应该使用C++标准库的方法,而不是Qt特有的容器如QList或QLinkedList。不过用户可能是在Qt项目中使用std::list,需要结合Qt的其他功能。根据引用中的示例,比如引用3中的代码,展示了如何使用std::list的迭代器和for_each算法来遍历元素。用户可能希望类似的代码,但可能在Qt环境中使用。另外,引用4提到了使用迭代器遍历vector并取值的例子,这也可以参考。首先,遍历std::list通常有两种方法:使用迭代器或基于范围的for循环(C++11及以上)。在Qt项目中,这两种方法都是可行的。需要确保代码兼容用户的C++版本。然后,获取值的方式,比如通过解引用迭代器或者直接访问元素。需要注意的是std::list是双向链表,不支持随机访问,所以不能像vector那样用下标访问,必须通过迭代器逐个访问。 接下来,可能需要考虑在遍历过程中如何将std::list中的值Qt的组件结合,比如显示到UI上,或者进行数据处理。但用户的问题主要是遍历和取值,所以示例代码应聚焦于此。 检查引用中的代码,引用3用了for_each和函数指针,但现代C++更常用lambda表达式。不过用户可能希望看到多种方法,包括传统的迭代器、基于范围的循环,以及可能的Qt特定方法,但std::list是标准库容器,Qt没有直接的方法,所以还是用标准库的方式。 需要确保示例代码正确,比如包含必要的头文件,使用正确的命名空间,以及正确的语法。比如,std::list的迭代器操作,begin()和end()的使用,解引用迭代器来获取值。 此外,用户可能想知道如何将遍历的值用于Qt的功能,比如输出到调试窗口,使用qDebug(),这样示例代码会更贴近Qt环境。例如,在遍历时用qDebug()打印每个元素的值。最后,生成相关问题需要考虑用户可能进一步的问题,比如性能优化,其他Qt容器的对比,或者如何处理不同类型的数据。</think>在Qt中遍历`std::list`并获取值主要依赖C++标准库的方法,因为`std::list`属于标准容器而非Qt特有容器。以下是几种实现方式及示例代码: --- ### **方法1:使用迭代器遍历** ```cpp #include <list> #include <QDebug> void traverseWithIterator() { std::list<int> mylist = {1, 2, 3, 4, 5}; // 使用迭代器遍历 for (std::list<int>::iterator it = mylist.begin(); it != mylist.end(); ++it) { int value = *it; // 通过解引用获取值 qDebug() << "Value:" << value; } } ``` --- ### **方法2:使用基于范围的for循环(C++11及以上)** ```cpp #include <list> #include <QDebug> void traverseWithRangeFor() { std::list<int> mylist = {1, 2, 3, 4, 5}; // 基于范围的for循环 for (const auto& value : mylist) { qDebug() << "Value:" << value; } } ``` --- ### **方法3:使用`std::for_each`算法** ```cpp #include <list> #include <algorithm> #include <QDebug> void printValue(int value) { qDebug() << "Value:" << value; } void traverseWithForEach() { std::list<int> mylist = {1, 2, 3, 4, 5}; // 使用std::for_each std::for_each(mylist.begin(), mylist.end(), printValue); } ``` --- ### **方法4:结合Qt的`qDebug()`输出** 若需将值用于Qt功能(如界面显示或日志输出): ```cpp #include <list> #include <QDebug> void traverseAndUseInQt() { std::list<QString> mylist = {"Apple", "Banana", "Cherry"}; for (const auto& str : mylist) { qDebug() << "String:" << str; // 可进一步将str传递给Qt控件(如QLabel) } } ``` --- ### **关键注意事项** 1. **性能**:`std::list`是双向链表,遍历时间复杂度为$O(n)$,但随机访问效率低,需避免频繁查找[^3]。 2. **兼容性**:若需Qt容器互操作(如转为`QList`),可借助迭代器或范围构造: ```cpp std::list<int> stdList = {1, 2, 3}; QList<int> qtList(stdList.begin(), stdList.end()); ``` 3. **线程安全**:遍历时若涉及多线程修改,需加锁(如`QMutex`)。 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值