C LIST容器用法操作实例
cout << *iter << endl;
//取出最后一个元素 cout << "------------------------------------------------" << endl << "操作:取出最后一个元素" << endl; cout << lInt.back() << endl;
return 0; }
//取出最前一个元素 cout << "------------------------------------------------" << endl << "操作:取出最前一个元素" << endl; cout << lInt.front() << endl;
//从后向前显示 list 中的元素(注意:迭代器,不可以同名,虽然在不同的作用域下) cout << "------------------------------------------------" << endl << "操作:从后向向前显示 list 中的元素 " << endl; for (LISTINT::reverse_iterator iter2 = lInt.rbegin(); iter2 != lInt.rend(); iter2++)
#include "stdafx.h"
#include <list> #include <iostream> using namespace std;
typedef list<int> LISTINT;
int _tmain(ilist 容器 LISTINT lInt;
//所有操作均已经结束 cout << "------------------------------------------------" << endl << " 操 作 : 所 有 操 作 均 已 经 结 束
" << endl; cout << "------------------------------------------------" << endl;
List 容器用法操作实例
// Test_20110513_1756.cpp : Defines the entry point for the console application. //
////////////////////////////////////////////////////////////////////////// //* list 容器测试 //////////////////////////////////////////////////////////////////////////
cout << *iter2 << endl;
//删除一个元素 cout << "------------------------------------------------" << endl << "操作:删除最后一个元素" << endl; if (lInt.size() > 0) {
LISTINT::iterator delIter = lInt.end(); delIter--; cout << *delIter << endl; lInt.erase(delIter); if (lInt.size() > 0) {
delIter = lInt.end(); delIter--; cout << *delIter << endl; } }
//清空元素(并输入所有元素内容----正常输出为空。因为被清空了) cout << "------------------------------------------------" << endl << "操作:清空所有元素(并输入所有元素 内容----正常输出为空。因为被清空了)" << endl; lInt.clear(); for (LISTINT::iterator iter3 = lInt.begin(); iter3 != lInt.end(); iter3++) cout << *iter3 << endl;
//在 list 容器的末尾,添加元素 cout << "------------------------------------------------" << endl << "操作:从后面添加元素" << endl; for (LISTINT::value_type iValCount = 1; iValCount != 11; iValCount++)
lInt.push_back(iValCount); for (LISTINT::iterator iter = lInt.begin(); iter != lInt.end(); iter++)
cout << *iter << endl;
//在 list 容器的头部,添加元素 cout << "------------------------------------------------" << endl << "操作:从前面添加元素" << endl; for (LISTINT::value_type iValCount = 11; iValCount != 21; iValCount++)
c语言中list的用法
C语言中list的用法1.简介在C语言中,l is t是一种常用的数据结构,用于存储和管理多个元素。
它类似于数组,但具有更强大的灵活性和功能。
本文将介绍C语言中l i st的使用方法,包括创建、添加、删除和遍历等操作。
2.创建lis t要使用l is t,首先需要定义一个结构体来表示l is t的节点,节点中包含数据元素和指向下一个节点的指针。
然后,使用指向该结构体的指针来表示整个l is t。
以下是创建l is t的基本代码:t y pe de fs tr uc tN ode{i n td at a;s t ru ct No de*n ex t;}N od e;t y pe de fs tr uc t{N o de*h ea d;}L is t;3.添加元素要向li st中添加元素,可以使用以下代码:v o id ad dE le me nt(Li s t*li st,i nt ne wDa t a){N o de*n ew No de=(Nod e*)ma ll oc(s iz eof(No de));n e wN od e->d at a=new D at a;n e wN od e->n ex t=NUL L;i f(l is t->h ea d==NU L L){l i st->he ad=n ew Nod e;}e ls e{N o de*c ur re nt No de=l is t->h ea d;w h il e(cu rr en tN ode->n ex t!=N UL L){c u rr en tN od e=cu rre n tN od e->n ex t;}c u rr en tN od e->n ext=ne wN od e;}}4.删除元素要从li st中删除元素,可以使用以下代码:v o id re mo ve El em ent(Li st*l is t,in tta r ge t){ N o de*c ur re nt No de=l is t->h ea d;N o de*p re vN od e=NUL L;w h il e(cu rr en tN ode!=N UL L){i f(c ur re nt No de->d a ta==ta rg et){i f(p re vN od e==N ULL){l i st->he ad=c ur ren t No de->ne xt;}e ls e{p r ev No de->ne xt=cu r re nt No de->ne xt;}f r ee(c ur re nt No de);b r ea k;}p r ev No de=c ur re ntN o de;c u rr en tN od e=cu rre n tN od e->n ex t;}}5.遍历lis t要遍历l is t中的所有元素,可以使用以下代码:v o id tr av er se Li st(L is t*li st){N o de*c ur re nt No de=l is t->h ea d;w h il e(cu rr en tN ode!=N UL L){p r in tf("%d",cu rre n tN od e->d at a);c u rr en tN od e=cu rre n tN od e->n ex t;}}6.示例下面是一个使用l ist的示例:#i nc lu de<s td io.h>#i nc lu de<s td li b.h>t y pe de fs tr uc tN ode{i n td at a;s t ru ct No de*n ex t;}N od e;t y pe de fs tr uc t{N o de*h ea d;}L is t;v o id ad dE le me nt(Li s t*li st,i nt ne wDa t a){N o de*n ew No de=(Nod e*)ma ll oc(s iz eof(No de)); n e wN od e->d at a=new D at a;n e wN od e->n ex t=NUL L;i f(l is t->h ea d==NU L L){l i st->he ad=n ew Nod e;}e ls e{N o de*c ur re nt No de=l is t->h ea d;w h il e(cu rr en tN ode->n ex t!=N UL L){c u rr en tN od e=cu rre n tN od e->n ex t;}c u rr en tN od e->n ext=ne wN od e;}}v o id re mo ve El em ent(Li st*l is t,in tta r ge t){N o de*c ur re nt No de=l is t->h ea d;N o de*p re vN od e=NUL L;w h il e(cu rr en tN ode!=N UL L){i f(c ur re nt No de->d a ta==ta rg et){i f(p re vN od e==N ULL){l i st->he ad=c ur ren t No de->ne xt;}e ls e{p r ev No de->ne xt=cu r re nt No de->ne xt; }f r ee(c ur re nt No de);b r ea k;}p r ev No de=c ur re ntN o de;c u rr en tN od e=cu rre n tN od e->n ex t;}}v o id tr av er se Li st(L is t*li st){N o de*c ur re nt No de=l is t->h ea d;w h il e(cu rr en tN ode!=N UL L){p r in tf("%d",cu rre n tN od e->d at a);c u rr en tN od e=cu rre n tN od e->n ex t;}}i n tm ai n(){L i st my Li st;m y Li st.h ea d=NU LL;a d dE le me nt(&my Lis t,5);a d dE le me nt(&my Lis t,10);a d dE le me nt(&my Lis t,15);r e mo ve El em en t(&my L is t,10);t r av er se Li st(&myL i st);r e tu rn0;}7.总结使用li st可以轻松地管理多个元素,实现灵活的数据存储和操作。
c语言list用法
c语言list用法在C语言中,没有内置的List数据类型,但是可以使用数组来实现类似的功能。
下面是一种用数组实现List的常见方式:1. 定义一个结构体来表示List和相关的元素:ctypedef struct{int capacity; List最大容量int size; List当前元素个数int *data; List元素数组} List;2. 初始化List:cList* initList(int capacity){List *list = (List*)malloc(sizeof(List)); 分配内存list->capacity = capacity; 设置容量list->size = 0; 初始元素个数为0list->data = (int*)malloc(sizeof(int) * capacity); 分配数组内存return list;}3. 添加元素到List的末尾:cvoid add(List *list, int element){if(list->size == list->capacity){List已满,需要扩容list->capacity *= 2;list->data = (int*)realloc(list->data, sizeof(int) * list->capacity);}list->data[list->size++] = element;}4. 获取List指定位置的元素:cint get(List *list, int index){if(index < 0 index >= list->size){printf("Index out of range.\n");return -1; 返回一个错误值}return list->data[index];5. 删除List末尾的元素:cvoid removeLast(List *list){if(list->size > 0){list->size;}}6. 销毁List:cvoid destroyList(List *list){free(list->data); 释放数组内存free(list); 释放List内存}使用示例:cint main(){List *list = initList(5); 初始化容量为5的Listadd(list, 10);add(list, 20);add(list, 30);printf("List[0]: %d\n", get(list, 0)); 输出:List[0]: 10removeLast(list);printf("List[2]: %d\n", get(list, 2)); 输出:Index out of range. List[2]: -1destroyList(list); 销毁Listreturn 0;}这只是一种使用数组实现List的方式,根据实际需求可以进行相应的修改和扩展。
C++List容器用法操作实例
List容器用法操作实例// T est_20110513_1756.cpp : Defines the entry point for the console application.//////////////////////////////////////////////////////////////////////////////* list容器测试//////////////////////////////////////////////////////////////////////////#include "stdafx.h"#include <list>#include <iostream>using namespace std;typedef list<int> LISTINT;int _tmain(int argc, _TCHAR* argv[]){//创建一个list容器LISTINT lInt;//在list容器的末尾,添加元素cout << "------------------------------------------------" << endl << "操作:从后面添加元素" << endl;for (LISTINT::value_t ype iV alCount = 1; iV alCount != 11; iValCount++)lInt.push_back(iValCount);for (LISTINT::iterat or it er = lInt.begin(); it er != lInt.end(); it er++)cout << *it er << endl;//在list容器的头部,添加元素cout << "------------------------------------------------" << endl << "操作:从前面添加元素" << endl;for (LISTINT::value_t ype iV alCount = 11; iV alCount != 21; iV alCount++)lInt.push_front(iV alCount);for (LISTINT::iterat or it er = lInt.begin(); it er != lInt.end(); it er++)cout << *it er << endl;//取出最后一个元素cout << "------------------------------------------------" << endl << "操作:取出最后一个元素" << endl; cout << lInt.back() << endl;//取出最前一个元素cout << "------------------------------------------------" << endl << "操作:取出最前一个元素" << endl; cout << lInt.front() << endl;//从后向前显示list中的元素(注意:迭代器,不可以同名,虽然在不同的作用域下)cout << "------------------------------------------------" << endl << "操作:从后向向前显示list中的元素" << endl;for (LISTINT::reverse_iterator iter2 = lInt.rbegin(); it er2 != lInt.rend(); iter2++)cout << *it er2 << endl;//删除一个元素cout << "------------------------------------------------" << endl << "操作:删除最后一个元素" << endl;if (lInt.size() > 0){LISTINT::iterator delIt er = lInt.end();delIter--;cout << *delIter << endl;lInt.erase(delIt er);if (lInt.size() > 0){delIter = lInt.end();delIter--;cout << *delIter << endl;}}//清空元素(并输入所有元素内容----正常输出为空。
C++中list类使用教程
固本培元的专栏你的问题,我也同样关注,可以共同探讨,联系邮箱:leo_luopy@ QQ:8944105C++中list类使用教程分类: C/C++高级用法 2013-03-11 10:31 34人阅读 评论(0) 收藏举报双向循环链表listlist是双向循环链表,,每一个元素都知道前面一个元素和后面一个元素。
在STL中,list和vector一样,是两个常被使用的容器。
和vector不一样的是,list不支持对元素的任意存取。
list中提供的成员函数与vector类似,不过list提供对表首元素的操作push_front、pop_front,这是vector不具备的。
和vector另一点不同的是,list的迭代器不会存在失效的情况,他不像vector会保留备份空间,在超过容量额度时重新全部分配内存,导致迭代器失效;list没有备份空间的概念,出入一个元素就申请一个元素的空间,所以它的迭代器不会失效。
还是举《C++之vector》中的例子:int data[6]={3,5,7,9,2,4};list<int> lidata(data, data+6);lidata.push_back(6);...list初始化时,申请的空间大小为6,存放下了data中的6个元素,当向lidata插入第7个元素“6”时,list申请新的节点单元,插入到list链表中,数据存放结构如图1所示:图1 list的存储结构list每次增加一个元素,不存在重新申请内存的情况,它的成本是恒定的。
而vector每当增加关键元素的时候,都需要重新申请新的更大的内存空间,会调用元素的自身的复制构造函数,存在构造成本。
在销毁旧内存的时候,会调用析构函数,存在析构成本。
所以在存储复杂类型和大量元素的情况下,list比vector更有优势!List是一个双向链表,双链表既可以向前又向后链接他的元素。
List将元素按顺序储存在链表中. 与向量(vector)相比, 它允许快速的插入和删除,但是随机访问却比较慢。
CList控件使用方法1
列表控件可以看作是功能增强的ListBox,它提供了四种风格,而且可以同时显示一列的多中属性值。
MFC中使用CListCtrl类来封装列表控件的各种操作。
通过调用BOOL Create( DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID );创建一个窗口,dwStyle中可以使用以下一些列表控件的专用风格:∙LVS_ICON LVS_SMALLICON LVS_LIST LVS_REPORT这四种风格决定控件的外观,同时只可以选择其中一种,分别对应:大图标显示,小图标显示,列表显示,详细报表显示∙LVS_EDITLABELS结点的显示字符可以被编辑,对于报表风格来讲可编辑的只为第一列。
∙LVS_SHOWSELALWAYS在失去焦点时也显示当前选中的结点∙LVS_SINGLESEL同时只能选中列表中一项首先你需要设置列表控件所使用的ImageList,如果你使用大图标显示风格,你就需要以如下形式调用:CImageList* SetImageList( CImageList* pImageList, LVSIL_NORMAL); 如果使用其它三种风格显示而不想显示图标你可以不进行任何设置,否则需要以如下形式调用:CImageList* SetImageList( CImageList* pImageList, LVSIL_SMALL);通过调用int InsertItem( int nItem, LPCTSTR lpszItem );可以在列表控件中nItem指明位置插入一项,lpszItem为显示字符。
除LVS_REPORT风格外其他三种风格都只需要直接调用InsertItem就可以了,但如果使用报表风格就必须先设置列表控件中的列信息。
通过调用int InsertColumn( int nCol, LPCTSTR lpszColumnHeading, int nFormat , int nWidth, int nSubItem);可以插入列。
c中list的用法
c中list的用法
C语言中的list是一种动态数组(dynamic array)的实现方式,也称作可变长
数组(variable-length array)。
它允许在程序运行过程中动态地分配内存,以存储
一组数据,并支持在任意位置插入、删除、查找和修改元素。
使用list,可以轻松
地实现各种数据结构和算法,如栈、队列、堆、哈希表等。
使用list时,需要先包含头文件<stdlib.h>,然后使用malloc函数分配一定大小的内存空间,然后使用realloc函数在需要的时候重新调整内存大小,使用free函
数释放不再需要的内存空间。
在对list进行插入和删除操作时,需要注意指针和数
组下标的正确使用,以防止内存泄漏和越界访问等错误。
list的使用方法比较灵活,可以根据需要自定义结构体类型,然后对结构体指
针进行动态内存分配。
此外,可以使用预定义的数据类型,如int、char、float等,也可以使用void类型指针,以实现更高级的数据结构和算法。
总之,C语言中的list是一种强大的数据结构,它允许动态地管理一组数据,
并提供了丰富的操作接口,可以用于实现各种数据结构和算法。
对于需要高效处理大量数据的程序,使用list可以大大提高程序的性能和可维护性。
c++list字面量初始化方法
c++list字面量初始化方法C++的STL库包含了许多容器,其中一个非常常用的容器是list。
list是一个模板类,定义在头文件<list>中。
它是一个双向链表,每个节点都存储了一个值。
list储存数值的方式和数组不同,它不是连续储存,而是每个节点都有指向前一个节点和后一个节点的指针。
这个特性使得list的插入和删除操作非常高效,但随机访问元素的代价很高。
C++ 11新增了支持容器字面量初始化的功能,能够更加简洁地初始化容器。
下面是C++11中使用容器字面量初始化list的方法:```c++#include <iostream>#include <list>using namespace std;//初始化一个listlist<int> numList = {1, 3, 5, 7, 9};for (auto num : numList) {cout << num << " "; //使用迭代器输出list中的元素}cout << endl;return 0;}```上例中,通过在赋值符号右侧使用花括号来初始化numList。
这种容器字面量初始化方式并不仅限于这种情况。
在下面的例子中,我们进一步介绍了C++中list容器字面量初始化的方法。
## 一、使用空花括号初始化list你可以使用一个空花括号初始化一个list。
这会先创建一个空的list,然后你可以使用push_back等函数向其中添加元素。
如下所示:#include <iostream>#include <list>using namespace std;list<int> numList = {};numList.push_back(1);numList.push_back(3);numList.push_back(5);numList.push_back(7);numList.push_back(9);for (auto num : numList) {cout << num << " "; //使用迭代器输出list中的元素}cout << endl;return 0;}```执行结果:```1 3 5 7 9```## 二、使用花括号分隔符初始化list在C++11标准中,可以使用花括号分隔符初始化一个list。
std list自定义结构体使用方法
std list自定义结构体使用方法在C++中,你可以使用`std::list`容器来存储自定义结构体。
以下是一个示例,展示如何定义一个结构体,并使用`std::list`来存储这个结构体的实例。
首先,定义一个结构体:```cppstruct Person {std::string name;int age;};```然后,你可以创建一个`std::list`来存储`Person`类型的对象:```cppinclude <list>include <iostream>int main() {std::list<Person> people;// 添加元素到列表中_back({"Alice", 25});_back({"Bob", 30});_back({"Charlie", 35});// 遍历列表并打印每个元素for (const auto& person : people) {std::cout << "Name: " << << ", Age: " << << std::endl;}return 0;}```在上面的示例中,我们创建了一个名为`people`的`std::list`,其中包含`Person`类型的对象。
然后,我们使用`push_back()`方法将几个`Person`对象添加到列表中。
最后,我们使用范围for循环遍历列表,并打印出每个元素的`name`和`age`字段。
c语言 list 实例
c语言 list 实例
以下是一个使用C++语言实现的list容器实例:
```cpp
#include <iostream>
#include <list>
using namespace std;
int main() {
list<int> mylist = {1,1,2};
list<int>::iterator it = mylist.begin(); // 迭代器指向第2 个位置
advance(it, 1); // 将元素 3 插入迭代器指向的位置
mylist.insert(it, 3);
cout << "The list contains: ";
for (it = mylist.begin(); it != mylist.end(); ++it) {
cout << *it << " ";
}
cout << endl;
return 0;
}
```
在这个例子中,我们首先创建了一个名为`mylist`的`list`容器,并初始化了其中的三个元素。
然后,我们使用迭代器`it`来访问容器中的元素,并使用`advance`函数将迭代器向前移动一个位置,以便在该位置插入新的元素。
最后,我们使用`insert`函数在指定位置插入元素`3`,并输出容器中的所有元素。
你可以根据自己的需求修改和扩展这个示例。
如果你还有其他疑问,请随时向我提问。
C学习:list容器详解(一)
C学习:list容器详解(一)list容器详解首先说说STLSTL就是Standard Template Library,标准模板库。
这可能是一个历史上最令人兴奋的工具的最无聊的术语。
从根本上说,STL是一些“容器”的集合,这些“容器”有list,vector,set,map等,STL也是算法和其他一些组件的集合。
这里的“容器”和算法的集合指的是世界上很多聪明人很多年的杰作。
STL的目的是标准化组件,这样就不用重新开发,可以使用现成的组件。
STL现在是C++的一部分,因此不用额外安装什么。
它被内建在你的编译器之内。
因为STL的list是一个简单的容器,所以我打算从它开始介绍STL如何使用。
如果你懂得了这个概念,其他的就都没有问题了。
另外,list容器是相当简单的,我们会看到这一点。
STL容器可以保存对象,内建对象和类对象。
它们会安全的保存对象,并定义我们能够操作的这个对象的接口。
放在蛋架上的鸡蛋不会滚到桌上。
它们很安全。
因此,在STL容器中的对象也很安全。
STL算法是标准算法,我们可以把它们应用在那些容器中的对象上。
这些算法都有很著名的执行特性。
它们可以给对象排序,删除它们,给它们记数,比较,找出特殊的对象,把它们合并到另一个容器中,以及执行其他有用的操作。
STL iterator就象是容器中指向对象的指针。
STL的算法使用iterator在容器上进行操作。
Iterator设置算法的边界,容器的长度,和其他一些事情。
举个例子,有些iterator仅让算法读元素,有一些让算法写元素,有一些则两者都行。
Iterator也决定在容器中处理的方向。
你可以通过调用容器的成员函数begin()来得到一个指向一个容器起始位置的iterator。
你可以调用一个容器的end() 函数来得到过去的最后一个值(就是处理停在那的那个值)。
这就是STL所有的东西,容器、算法、和允许算法工作在容器中的元素上的iterator。
