c++学习第十五天

创作过程中难免有不足,若您发现本文内容有误,恳请不吝赐教。


提示:以下是本篇文章正文内容,下面案例可供参考。

一、列表

  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++的基础知识。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jacob~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值