其他未完成

This commit is contained in:
2023-08-14 17:20:39 +08:00
parent 9290e4c051
commit 4c986179b4
65 changed files with 2650 additions and 11 deletions
+26
View File
@@ -0,0 +1,26 @@
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<int, string> ms;
// 1. insert()函数
// map 在插入时会自动根据 map 的键来排序
ms.insert(pair<int, string>(1, "张三")); // pair<int, string>是map<int, string>::value_type的类型
ms.insert(pair<int, string>(4, "李四"));
ms.insert(make_pair(3, "王五")); // make_pair()函数可以自动推导出类型
ms.insert(map<int, string>::value_type(2, "赵六")); // value_type是map<int, string>的类型
ms[5] = "田七"; // 通过下标的方式插入数据
map<int, string>::iterator it = ms.begin();
for (; it != ms.end(); it++)
{
// cout << "sid = " << it->first << ",name = " << it->second << endl;
cout << "sid = " << (*it).first << ",name = " << (*it).second << endl;
}
return 0;
}
+61
View File
@@ -0,0 +1,61 @@
// 使用 map 来解决括号匹配
#include <iostream>
#include <map>
#include <stack>
#include <string>
using namespace std;
string left_c = "{[(";
map<char, char> map1;
bool valid(const char *p)
{
stack<char> stk;
int max_depth = 0;
while (*p)
{
const char &cp = *p;
// if (left_c.find(cp) != string::npos) // 如果当前拿到的是左括号
if (left_c.find(cp) != -1) // 如果当前拿到的是左括号
{
// 入栈
stk.push(*p);
}
else // 如果当前拿到的是右括号
{
// 弹栈
if (stk.empty())
return false;
const char &top = stk.top();
if (map1[cp] != top)
return false;
// 当前左右匹配的后续操作
// 求最大深度
if (max_depth < stk.size())
max_depth = stk.size();
// 弹栈前判断最大深度
stk.pop();
}
p++;
}
cout << "当前括号栈的最大深度为: " << max_depth << endl;
return stk.empty();
}
int main()
{
map1.insert(make_pair('}', '{'));
map1.insert(make_pair(']', '['));
map1.insert(make_pair(')', '('));
cout << valid("{[()]}[[[[[()]]]]]") << endl;
cout << valid("{[())}") << endl;
return 0;
}
+15
View File
@@ -0,0 +1,15 @@
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<int, string> m;
m.insert(make_pair(1, "disen"));
m.insert(make_pair(2, "lucy"));
cout << m.size() << endl;
m.erase(1); // 删除键值为 1 的元素
cout << m.size() << endl;
return 0;
}
+26
View File
@@ -0,0 +1,26 @@
#include <iostream>
#include <map>
using namespace std;
int main()
{
map<int, string> m;
m.insert(make_pair(1, "disen"));
m.insert(make_pair(2, "lucy"));
m.insert(make_pair(3, "jack"));
map<int, string>::const_iterator it = m.find(3);
if (it == m.end())
{
cout << "未查找到" << endl;
}
else
{
const pair<int, string> &p = *it;
cout << p.first << ", " << p.second << endl;
}
return 0;
}