1.list-基本使用
#include <iostream>
#include <list>
using namespace std;
void main() {
list<int> lt;
//从头部添加
lt.push_front(10);
lt.push_front(20);
lt.push_front(30);
//从尾部添加
lt.push_back(40);
lt.push_back(50);
lt.push_back(60);
//循环遍历
for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {
cout << *it << endl;
}
list<int>::iterator it = lt.begin();
//连续相加允许(++)
//支持'++'、'--'运算符
it++;
it--;
cout << endl << "支持++、--运算符" << endl;
cout << *it << endl;
//注意:不支持间断
//不支持'+'、'-'运算度
// it = it - 1; 错误调用
getchar();
}
执行代码
30
20
10
40
50
60
支持++、–运算符
30
2.list-删除
#include <iostream>
#include <list>
using namespace std;
void main() {
list<int> lt;
//从头部添加
lt.push_front(10);
lt.push_front(20);
lt.push_front(30);
//从尾部添加
lt.push_back(40);
lt.push_back(50);
lt.push_back(60);
cout << endl << "删除方式1:根据位置删除" << endl;
//删除方式1
list<int>::iterator it= lt.begin();
it++;
//删除:删除第二个元素
lt.erase(it);
//循环遍历
for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {
cout << *it << endl;
}
cout << endl << "删除方式2:直接根据内容删除" << endl;
//删除方式2
//直接根据内容删除
lt.remove(30);
//循环遍历
for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {
cout << *it << endl;
}
cout << endl << "删除方式3:区间删除" << endl;
//"删除方式3:区间删除
//开始位置
list<int>::iterator it_begin = lt.begin();
//结束位置
list<int>::iterator it_end = lt.begin();
it_end++;
it_end++;
//删除元素(如果已经被删除的元素不能够在删除)
lt.erase(it_begin, it_end);
//循环遍历
for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {
cout << *it << endl;
}
getchar();
}
执行代码
删除方式1:根据位置删除
30
10
40
50
60
删除方式2:直接根据内容删除
10
40
50
60
删除方式3:区间删除
50
60
3.list-插入
#include <iostream>
#include <list>
using namespace std;
void main() {
list<int> lt;
//从尾部添加
lt.push_back(40);
lt.push_back(50);
lt.push_back(60);
//插入
lt.insert(lt.begin(), 30);
//循环遍历
for (list<int>::iterator it = lt.begin(); it != lt.end(); it++) {
cout << *it << endl;
}
getchar();
}
执行代码
30
40
50
60
4.set-基本使用(元素唯一,默认从小到大排列)
#include <iostream>
#include <set>
using namespace std;
void main() {
set<int> st;
st.insert(40);
st.insert(10);
st.insert(30);
st.insert(20);
//删除
set<int>::iterator it = st.begin();
st.erase(it);
for (set<int>::iterator it = st.begin(); it != st.end(); it++) {
cout << *it << endl;
}
getchar();
}
执行代码
20
30
40
5set-从大到小排列
#include <iostream>
#include <set>
#include <functional>
using namespace std;
void main() {
set<int, greater<int> > st;
st.insert(40);
st.insert(10);
st.insert(30);
st.insert