基本概念这里就不再浪费时间去解释,这里给出deuqe与、orwa_list、list的基本使用方法:
- deque队列:
c
#include <iostream>
#include <deque>
template <typename T>
void print(T Begin, T End);
int main() {
std::deque<int> Obj1(6);
//迭代器的类型:随机访问迭代器
std::cout << typeid(std::deque<int>::iterator::iterator_category).name() << std::endl;
//deque(队列)的赋值操作,可以使用[]的方式来进行赋值
for (int i = 0; i < Obj1.size(); i++) {
Obj1[i] = i+1;
}
print(Obj1.begin(), Obj1.end());
std::cout << "----------------------------------------------------" << std::endl;
//at方法,找到队列中,该下标的值并返回,可以用于修改值,返回值类型:T
Obj1.at(0) = 666;
std::cout << typeid(Obj1.at(7)).name() << std::endl;
print(Obj1.begin(), Obj1.end());
std::cout << "----------------------------------------------------" << std::endl;
//size方法,返回当前队列的实际大小
int a = Obj1.size();
std::cout << a << std::endl;
std::cout << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
//max_size方法,返回队列的最大尺寸
int b = Obj1.max_size();
std::cout << b << std::endl;
std::cout << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
//front方法和back方法:分别返回队列中,第一个元素和最后一个元素,返回值类型:T
int c = Obj1.front();
int d = Obj1.back();
std::cout << typeid(Obj1.front()).name() << std::endl;
std::cout << typeid(Obj1.back()).name() << std::endl;
std::cout << c << "\t" << d << std::endl;
std::cout << "----------------------------------------------------" << std::endl;
//push_front方法,向队列首添加元素,返回值类型:void
Obj1.push_front(111);
std::cout << typeid(Obj1.push_front(111)).name() << std::endl;
print(Obj1.begin(), Obj1.end());
std::cout << "----------------------------------------------------" << std::endl;
//push_back方法,向队列尾部添加元素,返回值类型:void
Obj1.push_back(12345679);
std::cout << typeid(Obj1.push_back(12345679)).name() << std::endl;
print(Obj1.begin(), Obj1.end());
std::cout << "----------------------------------------------------" << std::endl;
//pop_front方法,弹出队列首部的元素,返回值类型:void
Obj1.pop_front();
std::cout << typeid(Obj1.pop_front).name() << std::endl;
print(Obj1.begin(), Obj1.end());
std::cout << "----------------------------------------------------" << std::endl;
//pop_back方法,弹出队列尾部的元素,返回值类型:void
Obj1.pop_back();
std::cout << typeid(Obj1.pop_back).name() << std::endl;
print(Obj1.begin(), Obj1.end());
std::cout << "----------------------------------------------------" << std::endl;
//rbegin方法:反向迭代器
print(Obj1.rbegin(), Obj1.rend());
std::cout << "----------------------------------------------------" << std::endl;
//insert方法,向列表的指定位置插入元素,第一个参数:迭代器,第二个参数:想要插入的值 //第二个参数也可以是个数,代表想要插入几个,第三个参数就是想要插入的值
Obj1.insert(Obj1.begin()+4,3, 666666666);
std::cout << typeid(Obj1.insert(Obj1.begin() + 4, 666)).name() << std::endl;
print(Obj1.begin(), Obj1.end());
std::cout << "----------------------------------------------------" << std::endl;
//erase方法,删除指定位置的元素,参数是迭代器,也可以范围删除元素,第一个参数是起始位置迭代器,第二个参数是结束位置迭代器
Obj1.erase(Obj1.begin(),Obj1.end());
std::cout << typeid(Obj1.erase(Obj1.begin())).name() << std::endl;
print(Obj1.begin(), Obj1.end());
std::cout << "----------------------------------------------------" << std::endl;
return 0;
}
template<typename T>
void print(T Begin, T End)
{
T i = Begin;
for (; i != End; i++) {
std::cout << *i << "\t";
}
std::cout << std::endl;
}
forward_list单向列表:
c
#include <iostream>
#include <forward_list>
template <typename T>
void print(T Begin, T End);
int main() {
std::forward_list<int> arr(5, 000);
//迭代器类型:前向迭代器,可读可写=, == ,!= ,++i ,i++
std::cout << typeid(std::forward_list<int>::iterator::iterator_category).name() << std::endl;
print(arr.begin(), arr.end());
std::cout << "----------------------------------------------------" << std::endl;
//psuh_frint方法,前插法,向单向列表前插入元素,返回值类型:void
for (int i = 0; i < 4; i++) {
arr.push_front(i);
}
std::cout << typeid(arr.push_front(1)).name() << std::endl;
print(arr.begin(), arr.end());
std::cout << "----------------------------------------------------" << std::endl;
//pop_front方法,弹出列表第一个元素,返回值类型:void
arr.pop_front();
std::cout << typeid(arr.pop_front()).name() << std::endl;
print(arr.begin(), arr.end());
std::cout << "----------------------------------------------------" << std::endl;
return 0;
}
template<typename T>
void print(T Begin, T End)
{
T i;
for (i = Begin; i != End; i++) {
std::cout << *i << "\t";
}
std::cout << std::endl;
}
list双向链表:
c
#include <iostream>
#include <list>
template <typename T>
void print(T Begin, T End);
int main() {
std::list<int> arr(5, 666);
//迭代器类型:struct std::bidirectional_iterator_tag(双向迭代器),可读可写=, == ,!= ,++i ,i++。--i,i--
std::cout << typeid(std::list<int>::iterator::iterator_category).name() << std::endl;
print(arr.begin(), arr.end());
std::cout << "----------------------------------------------------" << std::endl;
//push_front,push_back方法:向列表同步和尾部插入元素,返回值类型:void
arr.push_front(1);
arr.push_back(5);
std::cout << typeid(arr.push_front(1)).name() << "\t" << typeid(arr.push_back(5)).name() << std::endl;
print(arr.begin(), arr.end());
std::cout << "----------------------------------------------------" << std::endl;
//insert方法,向列表指定位置插入元素,第一个参数为迭代器,指向要插入的位置,第二个参数为想要插入的值/第二个参数也可以为要插入元素的个数,第三个参数为要想插入的值
arr.insert(arr.begin(), 2);
arr.insert(arr.begin(), 3, 666);
std::cout << typeid(arr.insert(arr.begin(), 2)).name() << std::endl;
print(arr.begin(), arr.end());
std::cout << "----------------------------------------------------" << std::endl;
//pop_front,pop_back方法,弹出列表首部和尾部的值,返回值类型:void
arr.pop_front();
arr.pop_back();
std::cout << typeid(arr.pop_front).name() << "\t" << typeid(arr.pop_back).name() << std::endl;
print(arr.begin(), arr.end());
std::cout << "----------------------------------------------------" << std::endl;
//erese方法:删除指定位置的值,参数为指定位置迭代器,也可以删除范围,第一个参数为其实迭代器,第二个参数为结束迭代器
arr.erase(arr.begin());
arr.erase(arr.begin(), arr.end());
print(arr.begin(), arr.end());
std::cout << "----------------------------------------------------" << std::endl;
//reverse方法:将列表中的元素倒置
arr.reverse();
print(arr.begin(), arr.end());
std::cout << "----------------------------------------------------" << std::endl;
return 0;
}
template<typename T>
void print(T Begin, T End)
{
T i;
for (i = Begin; i != End; i++) {
std::cout << *i << "\t";
}
std::cout << std::endl;
}
如果发现文章中有错误,还请大家指出来,我会非常虚心地学习,我们一起进步!!!