stack容器
stack容器——基本概念
就是棧
棧不允許有遍歷行為
是先進(jìn)后出的數(shù)據(jù)結(jié)構(gòu)
stack容器——常用接口
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stack>
using namespace std;
//棧stack容器
void test01()
{
//特點(diǎn):符合先進(jìn)后出數(shù)據(jù)結(jié)構(gòu)
stack<int>s;
//入棧
s.push(10);
s.push(20);
s.push(30);
s.push(40);
cout << "棧的大小:" << s.size() << endl;
//只有棧不為空,查看棧頂,并且執(zhí)行出棧操作
while (!s.empty())
{
//查看棧頂元素
cout << "棧頂元素為:" << s.top() << endl;
//出棧
s.pop();
}
cout << "棧的大小:" << s.size() << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
queue容器
queue容器——基本概念
是先進(jìn)先出的數(shù)據(jù)結(jié)構(gòu)
**隊(duì)列容器允許從一端新增元素,從另一端移除元素
隊(duì)列中只有隊(duì)頭和隊(duì)尾才可以被外界使用,因此隊(duì)列不允許有遍歷行為
隊(duì)列中進(jìn)數(shù)據(jù)稱為—入隊(duì)push
隊(duì)列中出數(shù)據(jù)稱為—出隊(duì)pop
**
queue容器——常用接口
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <queue>
#include <string>
using namespace std;
class Person
{
public:
Person(string name, int age)
{
this->m_Name = name;
this->m_Age = age;
}
string m_Name;
int m_Age;
};
//隊(duì)列 Queue
void test01()
{
//創(chuàng)建隊(duì)列
queue<Person>q;
//準(zhǔn)備數(shù)據(jù)
Person p1("唐僧", 30);
Person p2("孫悟空", 30);
Person p3("豬八戒", 30);
Person p4("沙僧", 30);
//入隊(duì)
q.push(p1);
q.push(p2);
q.push(p3);
q.push(p4);
cout << "隊(duì)列的大小為:" << q.size() << endl;
//判斷只要隊(duì)列不為空,查看對(duì)頭,查看隊(duì)尾,出隊(duì)
while (!q.empty())
{
//查看對(duì)頭元素
cout << "對(duì)頭元素 --- 姓名:" << q.front().m_Name << " 對(duì)頭元素 --- 年齡:" << q.front().m_Age << endl;
//查看對(duì)尾元素
cout << "對(duì)尾元素 --- 姓名:" << q.back().m_Name << " 對(duì)尾元素 --- 年齡:" << q.back().m_Age << endl;
q.pop();
}
cout << "隊(duì)列的大小為:" << q.size() << endl;
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
list容器
list容器基本概念
在STL中這個(gè)鏈表是雙向鏈表
由于鏈表的存儲(chǔ)方式并不是連續(xù)的內(nèi)存空間,因此鏈表list中的迭代器只支持前移和后移,屬于雙向迭代器
list的優(yōu)點(diǎn)∶
采用動(dòng)態(tài)存儲(chǔ)分配,不會(huì)造成內(nèi)存浪費(fèi)和溢出
鏈表執(zhí)行插入和刪除操作十分方便,修改指針即可,不需要移動(dòng)大量元素
list的缺點(diǎn):
鏈表靈活,但是空間(指針域)和時(shí)間(遍歷)額外耗費(fèi)較大
List有一個(gè)重要的性質(zhì),插入操作和刪除操作都不會(huì)造成原有l(wèi)ist迭代器的失效,這在vector是不成立的。
list和vector是經(jīng)常被使用的容器
list容器——構(gòu)造函數(shù)
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
//加上const防止修改代碼
//容器中的數(shù)據(jù)不可以修改了
//*it = 100; err
cout << *it << " ";
}
cout << endl;
}
void test01()
{
//創(chuàng)建list容器
list<int>L1;//默認(rèn)構(gòu)造
//添加數(shù)據(jù)
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
//遍歷容器
printList(L1);
//區(qū)間方式構(gòu)造
list<int>L2(L1.begin(), L1.end());
printList(L2);
//拷貝構(gòu)造
list<int>L3(10, 1000);
printList(L3);
//n個(gè)elem
list<int>L4(L3);
printList(L4);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
list容器——賦值和交換
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
//list容器賦值和交換
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
list<int>L2;
L2 = L1;
printList(L2);
list<int>L3;
L3.assign(L2.begin(), L2.end());
printList(L3);
list<int>L4;
L4.assign(10, 100);
printList(L4);
}
//交換
void test02()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
list<int>L2;
L2.assign(10, 100);
cout << "交換前: " << endl;
printList(L1);
printList(L2);
L1.swap(L2);
cout << "交換后:" << endl;
printList(L1);
printList(L2);
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
List容器——大小操作
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
//list容器賦值和交換
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
printList(L1);
//判斷容器是否為空
if (L1.empty())
{
cout << "L1為空" << endl;
}
else
{
cout << "L1不為空" << endl;
cout << "L1的元素個(gè)數(shù)為:" << L1.size() << endl;
}
//重新指定大小
L1.resize(10,10000);
printList(L1);
L1.resize(2);
printList(L1);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
list容器——插入和刪除
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
//list容器賦值和交換
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L;
//尾插
L.push_back(10);
L.push_back(20);
L.push_back(30);
//頭插
L.push_front(100);
L.push_front(200);
L.push_front(300);
printList(L);
//尾刪
L.pop_back();
printList(L);
//頭刪
L.pop_front();
printList(L);
//insert插入
list<int>::iterator it = L.begin();
L.insert(++it, 1000);
printList(L);
//list容器是雙向迭代器 不能跳躍移動(dòng)
//L.insert(L.begin() + 1, 100000);//err
//printList(L);
//刪除
it = L.begin();
L.erase(++it);
printList(L);
//移除
L.push_back(10000);
L.push_back(10000);
L.push_back(10000);
L.push_back(10000);
printList(L);
L.remove(10000);//按值刪除 刪除所有的10000
printList(L);
//清空
L.clear();
printList(L);
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
list容器——數(shù)據(jù)存取
list容器底層是鏈表 存儲(chǔ)的不是連續(xù)的空間 不支持隨機(jī)訪問 不能用[]或者at直接進(jìn)行訪問
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
using namespace std;
//list容器賦值和交換
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(10);
L1.push_back(20);
L1.push_back(30);
L1.push_back(40);
//L1[0]不可以用 []訪問list容器中的元素
//L1[0];err
//L1.at(0);err
//原因是list本質(zhì)是鏈表,不是用連續(xù)的線性空間存儲(chǔ)的,迭代器也是不支持隨機(jī)訪問的
cout << "第一個(gè)元素為: " << L1.front() << endl;
cout << "最后一個(gè)元素為:" << L1.back() << endl;
//驗(yàn)證迭代器是不支持隨機(jī)訪問的
list<int>::iterator it = L1.begin();
it++;//right 支持雙向++ --
it--;//right
//it = it + 1;//err
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
list容器——反轉(zhuǎn)和排序
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <algorithm>
using namespace std;
//list容器賦值和交換
void printList(const list<int>&d)
{
for (list<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
void test01()
{
list<int>L1;
L1.push_back(20);
L1.push_back(10);
L1.push_back(50);
L1.push_back(40);
L1.push_back(30);
cout << "反轉(zhuǎn)前:" << endl;
printList(L1);
//反轉(zhuǎn)
L1.reverse();
cout << "反轉(zhuǎn)后:" << endl;
printList(L1);
}
//如果返回為true,則讓兩個(gè)參數(shù)進(jìn)行交換,如果返回false則不動(dòng)
bool myCompare(int v1, int v2)
{
//降序 就讓第一個(gè)數(shù) > 第二個(gè)數(shù)
return v1 > v2;
}
//排序
void test02()
{
list<int>L1;
L1.push_back(20);
L1.push_back(10);
L1.push_back(50);
L1.push_back(40);
L1.push_back(30);
//排序
cout << "排序前: " << endl;
printList(L1);
//所有不支持隨機(jī)訪問迭代器的容器,不可以用標(biāo)準(zhǔn)的算法
//不支持隨機(jī)訪問迭代器的容器,內(nèi)部會(huì)提供對(duì)應(yīng)的一些算法
//sort(L1.begin(),L1.end());//err
L1.sort();//默認(rèn)排序規(guī)則 從小到大 升序
cout << "排序后: " << endl;
printList(L1);
L1.sort(myCompare);
printList(L1);
}
int main()
{
//test01();
test02();
system("pause");
return EXIT_SUCCESS;
}
list容器——排序案例
案例描述:將Person自定義數(shù)據(jù)類型進(jìn)行排序,Person中屬性有姓名、年齡、身高排序
規(guī)則:按照年齡進(jìn)行升序,如果年齡相同按照身高進(jìn)行降序文章來源:http://www.zghlxwxcb.cn/news/detail-488728.html
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <list>
#include <string>
using namespace std;
class Person
{
public:
Person(string name,int age,int height)
{
this->m_Name = name;
this->m_Age = age;
this->m_Height = height;
}
string m_Name;
int m_Age;
int m_Height;
};
//指定排序規(guī)則
bool comparePerson(Person &p1,Person &p2)
{
if (p1.m_Age == p2.m_Age)
{
//年齡相同 按照身高降序
return p1.m_Height > p2.m_Height;
}
else
{
//按照年齡 升序
return p1.m_Age < p2.m_Age;
}
}
void test01()
{
list<Person>L1;
Person p1("劉備", 35, 175);
Person p2("曹操", 45, 180);
Person p3("孫權(quán)", 40, 170);
Person p4("趙云", 25, 190);
Person p5("關(guān)羽", 35, 160);
Person p6("張飛", 35, 200);
L1.push_back(p1);
L1.push_back(p2);
L1.push_back(p3);
L1.push_back(p4);
L1.push_back(p5);
L1.push_back(p6);
for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++)
{
cout << "姓名:" << (*it).m_Name << " 年齡:" << (*it).m_Age << " 身高: " << (*it).m_Height << endl;
}
//排序
cout << "-----------------------------" << endl;
cout << "排序后:" << endl;
L1.sort(comparePerson);
for (list<Person>::iterator it = L1.begin(); it != L1.end(); it++)
{
cout << "姓名:" << (*it).m_Name << " 年齡:" << (*it).m_Age << " 身高: " << (*it).m_Height << endl;
}
}
int main()
{
test01();
system("pause");
return EXIT_SUCCESS;
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-488728.html
到了這里,關(guān)于STL——stack容器、queue容器、list容器的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!