stl_list複習

#include <iostream>
#include <list>
#include <algorithm>
using namespace std;ios

//底層結構是雙向鏈表函數

struct Node{
int a;
char c;this

};spa

struct Node1{ //重點中的重點
int a;
char c;
Node1(int d,char e)
{
a=d;
c=e;
}
bool operator==(const Node1& i)
{
if(i.a==this->a&&i.c==this->c)
{
return true;
}
return false;
}編譯器

bool operator<(const Node1& f)
{
if(this->a<f.a) //if(this->a>f.a) //能夠改變> , <
return true;
return false;
}
};it

void fun(Node d)
{
cout << d.a << " " << d.c << "\n";
}io

void fun1(Node1 d)
{
cout << d.a << " " << d.c << "\n";
}編譯

void listdefine()
{
list<Node> ls;
list<Node> l(5);
Node no={12,'a'};
list<Node> l1(5,no);
list<Node> l2(l1);
list<Node> l3(l2.begin(),l2.end());容器

list<Node>::iterator ite=l3.begin(); //不能夠加數,只能夠自加,
ite++; //ite+4; 錯誤
list<Node>l4(ite,l3.end());stream

//for_each(l3.begin(),l3.end(),fun);
for_each(l4.begin(),l4.end(),fun);
}

void listsize()
{
Node no={12,'a'};
list<Node> ls(6,no);

cout << ls.size() << endl;

//for_each(ls.begin(),ls.end(),fun);

ls.resize(18); //新擴空間全爲0

cout << ls.size() << endl;

for_each(ls.begin(),ls.end(),fun);

ls.resize(3);

cout << ls.size() << endl;

 

//cout << ls.empty() << endl;

ls.resize(0);

//cout << ls.empty() << endl;
}

void list_put_increase()
{
//Node no={12,'a'};
//list<Node> ls(5,no);

//輸出:循環,迭代器,for_each(不支持下標運算
/*
for(list<Node>::iterator ite=ls.begin();ite!=ls.end();ite++)
{
cout << ite->a << " " << ite->c << endl;
}
*/
//cout << ls.back().a << " " << ls.back().c << endl;
//cout << ls.front().a << " " << ls.front().c << endl;
//list<Node> l1;
//ls.push_front(no);
//for_each(ls.begin(),ls.end(),fun);
list<Node1> li;
li.push_front(Node1(12,'a')); //頭添加
li.push_back(Node1(13,'b')); //尾添加
list<Node1>::iterator ite=li.begin();
ite++;
li.insert(ite,Node1(15,'c'));
ite--;
li.insert(ite,3,Node1(16,'d'));
cout << ite->a << endl;

for_each(li.begin(),li.end(),fun1);
}

void list_delete_modification()//
{
list<Node1> ls;
ls.push_front(Node1(12,'a'));
ls.push_back(Node1(13,'b'));
list<Node1>::iterator ite=ls.begin();
ls.insert(++ite,3,Node1(14,'c'));

for_each(ls.begin(),ls.end(),fun1);

//ls.pop_back(); //尾刪除
//ls.pop_front(); //頭刪除
//ls.erase(ls.begin(),--ls.end()); //選位刪除
//ls.clear(); //清空
//ls.unique(); //去重

//ls.assign(3,Node1(11,'x')); //從新賦值
ls.begin()->a=(--ls.end())->a; //改
//還能夠用迭代器

for_each(ls.begin(),ls.end(),fun1);
}

void list_merge()
{
list<Node1> ls;
ls.push_front(Node1(12,'a'));
ls.push_back(Node1(13,'b'));
ls.insert(++ls.begin(),3,Node1(14,'c'));

list<Node1> ls1;
ls1.push_front(Node1(1,'x'));
ls1.push_front(Node1(25,'y'));
ls1.push_back(Node1(3,'z'));
ls.merge(ls1); //合併

//ls1.swap(ls); //交換


//ls.reverse(); //倒序 //reserve

//ls.sort(); //從小到大

//ls.splice(); //拼接

list<Node1>::iterator ite=find(ls.begin(),ls.end(),Node1(12,'a')); //find須要重載==號 Node1(12,'f')也不影響
cout << ite->a << " " << ite->c << endl;

for_each(ls.begin(),ls.end(),fun1);
}

int main()
{
//listdefine();
//listsize();
//list_put_increase();
//list_delete_modification();
//list_merge();
return 0;
}

/*
與vector的區別:
隨機訪問慢,也支持下標,快速插入刪除

forward_list容器,2011年之後的編譯器可用,更快,無size函數

*/

相關文章
相關標籤/搜索