C++中list简单用法

List介绍:

    List是stl实现的双向链表,与向量(vectors)相比, 它允许快速的插入和删除,但是随机访问却比较慢。


示例代码1:

#include <list> 
#include <stdio.h>
using namespace std; 

int main(void)
{
	list<int> myList;
	myList.push_back(1);
	myList.push_back(2);
	myList.push_back(3);
	
	list<int>::iterator iter;
	for (iter = myList.begin(); iter != myList.end(); iter++)
	{
		printf("%d\n", *iter);
	}

	return 0;
}

示例代码2:

using namespace std; 

typedef struct logFilter
{
	char keyword[64];
}logFilter_t;

void extractLogKeyword(char *szUploadFilter, list<logFilter_t> *pUploadKeywordList, list<logFilter_t> *pNotUploadKeywordList)
{
	int readLen;
	logFilter_t tmpStBuf;
	char *point = NULL;
	char *pszUploadFilter = szUploadFilter;
	//printf("%s\n\n", pszUploadFilter);
	
	while(pszUploadFilter[0] == '^')
	{
		memset(tmpStBuf.keyword, 0, sizeof(tmpStBuf.keyword));
		if(pszUploadFilter[1] == '~')
		{
			pszUploadFilter += 2;
			point = strstr(pszUploadFilter, "^");
			if(point != NULL)
			{
				readLen = point - pszUploadFilter;
				memcpy(tmpStBuf.keyword, pszUploadFilter, readLen);
				pszUploadFilter = point;
				if(readLen > 0)
					pNotUploadKeywordList->push_back(tmpStBuf);
			}
			else
			{
				memcpy(tmpStBuf.keyword, pszUploadFilter, strlen(pszUploadFilter));
				pNotUploadKeywordList->push_back(tmpStBuf);
				break;
			}
		}
		else
		{
			pszUploadFilter += 1;
			point = strstr(pszUploadFilter, "^");
			if(point != NULL)
			{
				readLen = point - pszUploadFilter;
				memcpy(tmpStBuf.keyword, pszUploadFilter, readLen);
				pszUploadFilter = point;
				if(readLen > 0)
					pUploadKeywordList->push_back(tmpStBuf);

			}
			else
			{	
				memcpy(tmpStBuf.keyword, pszUploadFilter, strlen(pszUploadFilter));
				if(strlen(pszUploadFilter) > 0)
					pUploadKeywordList->push_back(tmpStBuf);
				break;
			}
		}
	}
}


int main(void)
{

	list<logFilter_t> pUploadKeywordList;
	list<logFilter_t> pNotUploadKeywordList;
	char buf[256] = "^~normalLog^~camStatus";
	if(buf[0] != '^' || strlen(buf) == 1)
		printf("invalid\n");
	extractLogKeyword(buf, &pUploadKeywordList, &pNotUploadKeywordList);
	
	list<logFilter_t>::iterator iter;
	for (iter = pUploadKeywordList.begin(); iter != pUploadKeywordList.end(); iter++)
	{
		printf("upload : %s\n", iter->keyword);
	}
	for (iter = pNotUploadKeywordList.begin(); iter != pNotUploadKeywordList.end(); iter++)
	{
		printf("notUpload : %s\n", iter->keyword);
	}
	return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
引用:在C++list表示双向链表,它是一种线性容器,可以在任意位置插入和删除元素。可以利用正向迭代器遍历list容器进行操作。例如,使用list<int>迭代器it来遍历容器lt,可以使用lt.begin()获取list的第一个元素的迭代器,使用lt.end()获取list的末尾元素的下一个位置的迭代器。然后使用while循环和迭代器自增来遍历并输出list的元素。 引用展示了C++使用list的一些常见操作。比如,使用push_back()方法在list的末尾插入元素,使用merge()方法将两个list合并并按照指定的排序方式进行排序。另外,可以使用begin()和end()方法获取list的起始和结束迭代器,并使用for循环和迭代器自增来遍历并输出list的元素。 引用展示了C++list进行删除操作的用法。可以使用erase()方法来删除list的元素,其可以指定要删除的元素的位置或者范围。删除元素后,list的迭代器不会失效,仍然可以使用它们来继续遍历和操作list。 因此,C++list可以通过迭代器来遍历并操作其的元素,可以进行插入、删除和合并等操作。同时,使用迭代器遍历和操作list时需要注意迭代器的有效性,确保在删除元素后仍然能够正确使用迭代器。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [【C++list使用](https://blog.csdn.net/chuxinchangcun/article/details/128571927)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [C++list用法详解](https://blog.csdn.net/fengruoying93/article/details/108222992)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值