为编程而生,方便刷题与调试,话不多说,直上源码。
原文链接:C++ STL基本容器的输入输出与初始化接口(vector、deque、stack,map、链表、树等)
目录
1. vector
#include "iostream"
#include "vector"
using namespace std;
void printVector(vector<int>& v) {
for (vector<int>::iterator it = v.begin(); it != v.end(); it++) {
cout << *it << " ";
}
cout << endl;
}
int main()
{
vector<int> a = { 1,2,3,8 };
printVector(a);
return 0;
2. deque
#include "iostream"
#include "deque"
using namespace std;
void printDeque(const deque<int>& d)
{
for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
deque<int> a = { 4,2,3,8 };
printDeque(a);
return 0;
}
3. List
#include "iostream"
#include "list"
#include "vector"
using namespace std;
void printList(const list<int>&L)
{
for (list<int>::const_iterator it = L.begin(); it != L.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
list<int> x = { 4,2,3,8 };
printList(x);
return 0;
}
4. stack
#include "iostream"
#include "stack"
#include "vector"
using namespace std;
void printStack(stack<int> d)
{
while (!d.empty())
{
cout << d.top() << " ";
d.pop();
}
cout << endl;
}
int main()
{
vector<int> input = { 4,2,3,8 };
stack<int> a;
for (int i = 0; i < input.size(); i++) {
a.push(input[i]);
}
printStack(a);
return 0;
}
5. set/multiset
#include "iostream"
#include "set"
#include "vector"
using namespace std;
void printSet(const set<int>& st)
{
for (set<int>::const_iterator it = st.begin(); it != st.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
int main()
{
set<int> x = { 4,2,3,8 };
multiset<int> mx = { 2,2,3,3 };
printSet(x);
return 0;
}
6. map/multimap/unordered_map
#include "iostream"
#include "map"
#include "vector"
#include "unordered_map"
using namespace std;
void printMap(map<int, char>&m) {
for (map<int, char>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key=" << it->first << " value=" << it->second << endl;
}
cout << endl;
}
void printMultimap(multimap<int, char>&m) {
for (multimap<int, char>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key=" << it->first << " value=" << it->second << endl;
}
cout << endl;
}
void printUordermap(unordered_map<int, char>&m) {
for (unordered_map<int, char>::iterator it = m.begin(); it != m.end(); it++) {
cout << "key=" << it->first << " value=" << it->second << endl;
}
cout << endl;
}
int main()
{
map<int, char> x = { {4,'a'},{3,'b'} ,{2,'c'} ,{1,'d'} };
multimap<int, char> mx = { {4,'a'},{3,'b'} ,{2,'c'} ,{3,'d'} };
unordered_map<int, char> ux = { {4,'a'},{3,'b'} ,{2,'c'} ,{1,'d'} };
printMap(x);
printMultimap(mx);
printUordermap(ux);
return 0;
}
7. Linklist
#include "iostream"
#include "vector"
using namespace std;
struct ListNode {
int val;
ListNode * next;//指向下一个节点
ListNode(int x) :val(x), next(NULL) {} //有参构造函数
};
void CreatPostList(ListNode* Head, vector<int>& nums)//尾插法
{
ListNode* p = Head;
for (int i = 1; i < nums.size(); ++i)
{
ListNode* NewListNode = new ListNode(nums[i]);//创建新节点并赋值
p->next = NewListNode;//让上一节点的next指向当前新建节点
p = NewListNode;
}
}
void CreatPreList(ListNode* Head, vector<int>& nums)//头插法
{
for (int i = 0; i < nums.size(); ++i)
{
ListNode* NewListNode = new ListNode(nums[i]);//创建新节点并赋值
NewListNode->next = Head->next;
Head->next = NewListNode;
}
}
void PrintList(ListNode* Head)
{
ListNode* p = Head;
while (p != NULL)
{
cout << p->val << ' ';
p = p->next;
}
cout << endl;
}
int main()
{
vector<int> nums = { 1,2,6,3,4,5,6 };
ListNode* Head = new ListNode(1);//创建头结点
//CreatPreList(Head,nums);
//Head = Head->next;
CreatPostList(Head, nums);
PrintList(Head);
return 0;
}
8. Tree
#include "iostream"
#include <stack>
#include <queue>
#include "vector"
using namespace std;
struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
TreeNode* CreateBiTree(string& str) //!一定要传指针或者引用!!!
{
int n = str.size();
if (n == 0)return nullptr;
TreeNode* root = new TreeNode(str[0] - '0');
std::cout << 1 << " ";
queue<TreeNode*> q;
q.push(root);
int i = 1;
while (!q.empty()&&i<n) {
TreeNode* front = q.front();
q.pop();
if (str[i] != '#') {
TreeNode* T = new TreeNode(str[i] - '0');
std::cout << T->val << " ";
front->left = T;
q.push(front->left);
}
else std::cout << '#' << " ";
i++;
if (str[i] != '#'&&i < n) {
TreeNode* T = new TreeNode(str[i] - '0');
std::cout << T->val << " ";
front->right = T;
q.push(front->right);
}
else std::cout << '#' << " ";
i++;
}
return root;
}
//先序遍历
void PreOrderTraverseRecursion(TreeNode* root)
{
if (root == nullptr) return;
cout << root->val << " "; //操作
PreOrderTraverseRecursion(root->left);
PreOrderTraverseRecursion(root->right);
}
void PreOrderTraverseIteration(TreeNode* root)
{
stack<TreeNode*> st;
if (root) st.push(root);
while (!st.empty()) {
TreeNode* nd = st.top();
st.pop();
cout << nd->val << " ";
if (nd->right) st.push(nd->right);
if (nd->left) st.push(nd->left);
}
}
//中序遍历
void InOrderTraverseRecursion(TreeNode* root)
{
if (root == nullptr) return;
InOrderTraverseRecursion(root->left);
cout << root->val << " "; //操作
InOrderTraverseRecursion(root->right);
}
void InOrderTraverseIteration(TreeNode* root)
{
stack<TreeNode*> st;
TreeNode* cur = root;
while (cur || !st.empty()) {
while (cur) { //可以将此while与外层while合并成一个,效果完全相同,如下
st.push(cur);
cur = cur->left;
}
cur = st.top();
st.pop();
cout << cur->val << " ";
cur = cur->right;
}
}
//后续遍历
void PostOrderTraverseRecursion(TreeNode* root)
{
if (root == nullptr) return;
PostOrderTraverseRecursion(root->left);
PostOrderTraverseRecursion(root->right);
cout << root->val << " "; //操作
}
void PostOrderTraverseIteration(TreeNode* root)
{
stack<TreeNode*> st;
if (root) st.push(root);
TreeNode* pre = nullptr;
while (!st.empty()) {
TreeNode* nd = st.top();
if (!nd->left && !nd->right || pre && (nd->left == pre || nd->right == pre)) //叶子节点:叶子节点分为两种,一类是原本就是叶子节点的节点;
{ //另一类是父节点的左孩子or右孩子已经出栈,其自身退化成叶节点
st.pop(); //这里的or,逻辑上当父节点同时有左右孩子是表示和
cout << nd->val << " ";
pre = nd;
}
else { //非叶子节点
if (nd->right) st.push(nd->right);
if (nd->left) st.push(nd->left);
}
}
}
//层序遍历
void layerTraverse(TreeNode* root)
{
queue<TreeNode*> q;
if (root) q.push(root);
while (!q.empty()) {
TreeNode* front = q.front();
q.pop();
cout << front->val << " ";
if (front->left) q.push(front->left);
if (front->right) q.push(front->right);
}
}
int TreeDepth(TreeNode* root)
{
if (root == nullptr) return 0;
int left = TreeDepth(root->left);
int right = TreeDepth(root->right);
return left > right ? left + 1 : right + 1;
}
int main()
{
string str = "1234#56#7##8";
cout << "创建二叉树:";
TreeNode* root=CreateBiTree(str);
std::cout << '\n'<< endl;
cout << "先序遍历二叉树:";
PreOrderTraverseRecursion(root);
cout << "\n中序遍历二叉树:";
InOrderTraverseRecursion(root);
cout << "\n后序遍历二叉树:";
PostOrderTraverseRecursion(root);
cout << "\n\n非递归:" << endl;
cout << "先序遍历二叉树:";
PreOrderTraverseIteration(root);
cout << "\n中序遍历二叉树:";
InOrderTraverseIteration(root);
cout << "\n后序遍历二叉树:";
PostOrderTraverseIteration(root);
cout << "\n\n层序遍历二叉树:";
layerTraverse(root);
cout << endl;
int h= TreeDepth(root);
std::cout << "\n二叉树深度为:" << h<< endl;
return 0;
}