创作过程中难免有不足,若您发现本文内容有误,恳请不吝赐教。
提示:以下是本篇文章正文内容,下面案例可供参考。
一、列表
1.创建和遍历
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
list<int>::iterator it = lt.begin();
while (it != lt.end())
{
cout << *it << " ";
it++;
}
cout << endl;
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
return 0;
}
2.reverse
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(2);
lt.push_back(3);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.reverse();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
return 0;
}
3.sort(升序)
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(4);
lt.push_back(3);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.sort();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
return 0;
}
4.greater(降序)
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(4);
lt.push_back(3);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.sort(greater<int>());
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
return 0;
}
5.unique(去重)
#include<iostream>
#include<list>
using namespace std;
int main()
{
list<int> lt;
lt.push_back(1);
lt.push_back(4);
lt.push_back(3);
lt.push_back(3);
lt.push_back(5);
lt.push_back(3);
lt.push_back(6);
lt.push_back(3);
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
lt.unique();
for (auto e : lt)
{
cout << e << " ";
}
cout << endl;
return 0;
}
可以先排好序再用unique;
二、栈
1.创建和遍历
#include<iostream>
#include<stack>
using namespace std;
int main()
{
stack<int> st;
st.push(1);
st.push(6);
st.push(8);
st.push(2);
st.push(4);
while (!st.empty())
{
cout << st.top() << " ";
st.pop();
}
return 0;
}
三、队列
1.
#include<iostream>
#include<queue>
using namespace std;
int main()
{
queue<int> q;
q.push(1);
q.push(6);
q.push(8);
q.push(2);
q.push(4);
while (!q.empty())
{
cout << q.front() << " ";
q.pop();
}
return 0;
}
四、题目
1.最小栈
//力扣代码
//关键:空间换时间
class MinStack {
public:
MinStack() {
}
void push(int val) {
_st.push(val);
if(_minst.empty() || _minst.top()>=val)
_minst.push(val);
}
void pop() {
if(_st.top() == _minst.top())
_minst.pop();
_st.pop();
}
int top() {
return _st.top();
}
int getMin() {
return _minst.top();
}
stack<int> _st;
stack<int> _minst;
};
2.栈的压入、弹出序列
//力扣代码
class Solution {
public:
bool IsPopOrder(vector<int>& pushV, vector<int>& popV) {
stack<int> st;
size_t pushi = 0,popi = 0;
while(pushi < pushV.size())
{
st.push(pushV[pushi++]);
while(!st.empty() && st.top() == popV[popi])
{
st.pop();
popi++;
}
}
return st.empty();
}
};
3.二叉树的层序遍历
//力扣代码
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> q;
int levelSize = 0;
if(root)
{
q.push(root);
levelSize = 1;
}
vector<vector<int>> vv;
while(!q.empty())
{
vector<int> v;
while(levelSize--)
{
TreeNode* front =q.front();
q.pop();
v.push_back(front->val);
if(front->left)
q.push(front->left);
if(front->right)
q.push(front->right);
}
vv.push_back(v);
levelSize=q.size();
}
return vv;
}
};
总结
例如:以上就是今天要讲的内容,本文仅仅简单介绍了c++的基础知识。