list是双向链表
头文件
#include <list>
定义
std::list<int> nameList; // empty list
std::list<int> nameList(20); // name list with 20 empty elements
std::list<int> List(9, 13); // list 9 int elements with value 13
std::list<int> mList(List.begin(),List.end()); // iterating through List
std::list<int> m2List(nameList); // a copy of List
成员函数
begin // Returns an iterator pointing to the first element in the list container
end // Returns an iterator pointing to the last element in the list container
size // Returns the number of elements in the list container.
empty // Returns whether the list container is empty, empty return true otherwise false
front // Returns a reference to the first element in the list container.
back // Returns a reference to the last element in the list container.
assign // Assigns new contents to the list container, replacing its current contents, and modifying its size accordingly
push_front// Inserts a new element at the beginning of the list, right before its current first element
pop_front // Removes the first element in the list container, effectively reducing its size by one
push_back // Adds a new element at the end of the list container, after its current last element.
pop_back // Removes the last element in the list container, effectively reducing the container size by one.
insert // The container is extended by inserting new elements before the element at the specified position.
erase // Removes from the list container either a single element (position) or a range of elements ([first,last))
clear // Removes all elements from the list container (which are destroyed), and leaving remove // the container with a size of 0.
Removes from the container all the elements that compare equal to val.
sort // Sorts the elements in the list, altering their position within the container.
reverse // Reverses the order of the elements in the list container.
insert 实例
// inserting into a list
#include <iostream>
#include <list>
#include <vector>
int main ()
{
std::list<int> mylist;
std::list<int>::iterator it;
// set some initial values:
for (int i=1; i<=5; ++i) mylist.push_back(i); // 1 2 3 4 5
it = mylist.begin();
++it; // it points now to number 2 ^
mylist.insert (it,10); // 1 10 2 3 4 5
// "it" still points to number 2 ^
mylist.insert (it,2,20); // 1 10 20 20 2 3 4 5
--it; // it points now to the second 20 ^
std::vector<int> myvector (2,30);
mylist.insert (it,myvector.begin(),myvector.end());
// 1 10 20 30 30 20 2 3 4 5
// ^
std::cout << "mylist contains:";
for (it=mylist.begin(); it!=mylist.end(); ++it)
std::cout << ' ' << *it;
std::cout << '\n';
return 0;
}
sort 应用
#include <list>
#include<iostream>
using namespace std;
int main()
{
int marray[] = {3, 6, 8, 1, 2, 7, 4, 9, 5};
list<int> mList(marray, marray+9);
list<int>::iterator iter;
cout << "mList : ";
for (iter = mList.begin(); iter != mList.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
mList.sort();
cout << "mList : ";
for (iter = mList.begin(); iter != mList.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
return 0;
}
// output
mList : 3 6 8 1 2 7 4 9 5
mList : 1 2 3 4 5 6 7 8 9
typedef struct WarningList_S
{
int id;
int state;
string name;
bool operator < (const WarningList_S & s) const
{
return id < s.id;
}
} WarningList;
class WarningData
{
public:
int id;
int state;
string name;
bool operator == (const WarningData & s) const
{
return id == s.id;
}
};
sort 结构体重载
#include <list>
#include<iostream>
#include <string.h>
using namespace std;
typedef struct WarningList
{
int id;
int state;
string name;
bool operator < (const WarningList & s) const
{
return this->id < s.id;
}
} WarningList_T;
int main()
{
list<WarningList> activeList;
WarningList mactive[4];
mactive[0].id = 9;
mactive[0].state = true;
mactive[0].name = "nine";
mactive[1].id = 6;
mactive[1].state = true;
mactive[1].name = "six";
mactive[2].id = 8;
mactive[2].state = false;
mactive[2].name = "eight";
mactive[3].id = 7;
mactive[3].state = true;
mactive[3].name = "seven";
list<WarningList>::iterator it;
it = activeList.begin();
for (int i=0; i<4; ++i)
{
activeList.insert(it, mactive[i]);
++it;
}
list<WarningList>::iterator iter;
for (iter = activeList.begin(); iter != activeList.end(); ++iter)
{
cout << (*iter).id << ":" << (*iter).name<< " ";
}
cout << endl;
activeList.sort();
for (iter = activeList.begin(); iter != activeList.end(); ++iter)
{
cout << (*iter).id << ":" << (*iter).name<< " ";
}
return 0;
}
//output
7:seven 6:six 9:nine 8:eight
6:six 7:seven 8:eight 9:nine
实例
#include <list>
#include<iostream>
using namespace std;
int main()
{
list<int> mList;
// empty()
if (mList.empty())
{
cout << "mList is empty" << endl;
}
// push_back()
for (int i = 1; i < 10; ++i)
{
mList.push_back(i);
}
list<int>::iterator iter;
// begin() end()
cout << "mList : ";
for (iter = mList.begin(); iter != mList.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
// size()
cout << "mList size is : " << mList.size() << endl;
// front() back()
cout << "mList front is : " << mList.front() << endl;
cout << "mList back is : " << mList.back() << endl;
mList.front() = 0;
cout << "mList latest front is : " << mList.front() << endl;
// pop_front push_front pop_back push_back
mList.pop_front();
cout << "pop_front ";
for (iter = mList.begin(); iter != mList.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
mList.push_front(1);
mList.push_front(0);
cout << "push_front ";
for (iter = mList.begin(); iter != mList.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
// insert
std::list<int>::iterator it;
it = mList.begin();
++it;
mList.insert(it,9);
cout << "insert ";
for (iter = mList.begin(); iter != mList.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
// erase
std::list<int>::iterator it1;
it1 = mList.begin();
++it1;
mList.erase(it1);
cout << "erase : ";
for (iter = mList.begin(); iter != mList.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
// remove
mList.remove(7);
cout << "mList.remove(7) : ";
for (iter = mList.begin(); iter != mList.end(); ++iter)
{
cout << *iter << " ";
}
cout << endl;
// clear
mList.clear();
cout << "mList size is : " << mList.size() << endl;
return 0;
}
// output
mList is empty
mList : 1 2 3 4 5 6 7 8 9
mList size is : 9
mList front is : 1
mList back is : 9
mList latest front is : 0
pop_front 2 3 4 5 6 7 8 9
push_front 0 1 2 3 4 5 6 7 8 9
insert 0 9 1 2 3 4 5 6 7 8 9
erase : 0 1 2 3 4 5 6 7 8 9
mList.remove(7) : 0 1 2 3 4 5 6 8 9
mList size is : 0