mirror of
https://github.com/Estom/notes.git
synced 2026-11-29 07:35:23 +08:00
容器
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include<unordered_map>
|
||||
#include<unordered_set>
|
||||
#include<vector>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
@@ -54,6 +55,8 @@ int main(){
|
||||
people["yin"]++;
|
||||
|
||||
cout<<people["yin"]<<endl;
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
}
|
||||
@@ -27,7 +27,7 @@
|
||||

|
||||
|
||||
|
||||
## 2 queue和priority_queue
|
||||
## 2 queue
|
||||
|
||||
### 概念
|
||||
* queue是基于deque实现的,也可以用vector或list
|
||||
@@ -38,7 +38,165 @@
|
||||

|
||||
|
||||
|
||||
## 3 bitset特殊容器
|
||||
## 3 priority_queue
|
||||
|
||||
|
||||
|
||||
### 初始化
|
||||
|
||||
* Type 就是数据类型,Container 就是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector),Functional 就是比较的方式,当需要用自定义的数据类型时才需要传入这三个参数,使用基本数据类型时,只需要传入数据类型,**默认是大顶堆**
|
||||
```
|
||||
//构造函数
|
||||
priority_queue<Type, Container, Functional>
|
||||
|
||||
//升序队列
|
||||
priority_queue <int,vector<int>,greater<int> > q;
|
||||
//降序队列
|
||||
priority_queue <int,vector<int>,less<int> >q;
|
||||
```
|
||||
|
||||
### 基本操作
|
||||
|
||||
> 和队列基本操作相同:
|
||||
> * top 访问队头元素
|
||||
> * empty 队列是否为空
|
||||
> * size 返回队列内元素个数
|
||||
> * push 插入元素到队尾 (并排序)
|
||||
> * emplace 原地构造一个元素并插入队列
|
||||
> * pop 弹出队头元素
|
||||
> * swap 交换内容
|
||||
```C++
|
||||
#include<iostream>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
//对于基础类型 默认是大顶堆
|
||||
priority_queue<int> a;
|
||||
//等同于 priority_queue<int, vector<int>, less<int> > a;
|
||||
|
||||
// 这里一定要有空格,不然成了右移运算符↓
|
||||
priority_queue<int, vector<int>, greater<int> > c; //这样就是小顶堆
|
||||
priority_queue<string> b;
|
||||
|
||||
for (int i = 0; i < 5; i++)
|
||||
{
|
||||
a.push(i);
|
||||
c.push(i);
|
||||
}
|
||||
while (!a.empty())
|
||||
{
|
||||
cout << a.top() << ' ';
|
||||
a.pop();
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
while (!c.empty())
|
||||
{
|
||||
cout << c.top() << ' ';
|
||||
c.pop();
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
b.push("abc");
|
||||
b.push("abcd");
|
||||
b.push("cbd");
|
||||
while (!b.empty())
|
||||
{
|
||||
cout << b.top() << ' ';
|
||||
b.pop();
|
||||
}
|
||||
cout << endl;
|
||||
return 0;
|
||||
}
|
||||
```
|
||||
|
||||
### pair优先队列
|
||||
|
||||
* 默认的比较顺序是:pari的比较,先比较第一个元素,第一个相等比较第二个
|
||||
```C++
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
int main()
|
||||
{
|
||||
priority_queue<pair<int, int> > a;
|
||||
pair<int, int> b(1, 2);
|
||||
pair<int, int> c(1, 3);
|
||||
pair<int, int> d(2, 5);
|
||||
a.push(d);
|
||||
a.push(c);
|
||||
a.push(b);
|
||||
while (!a.empty())
|
||||
{
|
||||
cout << a.top().first << ' ' << a.top().second << '\n';
|
||||
a.pop();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
### 自定义类型的优先队列
|
||||
* 自定义重载比较运算符><的类和结构体。
|
||||
* 自定义重载函数调用运算符()的类或者结构体。需要接受两个参数
|
||||
* 或者使用自定义的函数指针。需要接受两个参数
|
||||
* 或者使用lambda比较函数。需要接受两个参数。
|
||||
```C++
|
||||
#include <iostream>
|
||||
#include <queue>
|
||||
using namespace std;
|
||||
|
||||
//方法1
|
||||
struct tmp1 //运算符重载<
|
||||
{
|
||||
int x;
|
||||
tmp1(int a) {x = a;}
|
||||
bool operator<(const tmp1& a) const
|
||||
{
|
||||
return x < a.x; //大顶堆
|
||||
}
|
||||
};
|
||||
|
||||
//方法2
|
||||
struct tmp2 //重写仿函数
|
||||
{
|
||||
bool operator() (tmp1 a, tmp1 b)
|
||||
{
|
||||
return a.x < b.x; //大顶堆
|
||||
}
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
tmp1 a(1);
|
||||
tmp1 b(2);
|
||||
tmp1 c(3);
|
||||
priority_queue<tmp1> d;
|
||||
d.push(b);
|
||||
d.push(c);
|
||||
d.push(a);
|
||||
while (!d.empty())
|
||||
{
|
||||
cout << d.top().x << '\n';
|
||||
d.pop();
|
||||
}
|
||||
cout << endl;
|
||||
|
||||
priority_queue<tmp1, vector<tmp1>, tmp2> f;
|
||||
f.push(c);
|
||||
f.push(b);
|
||||
f.push(a);
|
||||
while (!f.empty())
|
||||
{
|
||||
cout << f.top().x << '\n';
|
||||
f.pop();
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
|
||||
## 4 bitset特殊容器
|
||||
|
||||
|
||||
### 头文件
|
||||
|
||||
@@ -2,22 +2,31 @@
|
||||
#include<stack>
|
||||
#include<vector>
|
||||
#include<deque>
|
||||
#include<queue>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
|
||||
//stack test
|
||||
deque<int> deq{2,3,4,5};
|
||||
// deque<int> deq{2,3,4,5};
|
||||
|
||||
stack<int> stk{deq};
|
||||
int m = stk.top();
|
||||
stk.pop();
|
||||
stk.push(111);
|
||||
cout<<m<<endl;
|
||||
for(int n:deq){
|
||||
cout<<n<<endl;
|
||||
}
|
||||
// stack<int> stk{deq};
|
||||
// int m = stk.top();
|
||||
// stk.pop();
|
||||
// stk.push(111);
|
||||
// cout<<m<<endl;
|
||||
// for(int n:deq){
|
||||
// cout<<n<<endl;
|
||||
// }
|
||||
|
||||
// vector<int> vec;
|
||||
vector<int> vec;
|
||||
priority_queue<int> pir(vec,[](const int a,const int b){
|
||||
if(a>b){
|
||||
return true;
|
||||
}
|
||||
else{
|
||||
return false;
|
||||
}})
|
||||
return 0;
|
||||
}
|
||||
@@ -85,7 +85,7 @@ s1.swap(s2); // s1 = "East",s2 = "West"
|
||||
```
|
||||
|
||||
## 2.8 查找子串和字符
|
||||
* string 类有一些查找子串和字符的成员函数,它们的返回值都是子串或字符在 string 对象字符串中的位置(即下标)。如果查不到,则返回 string::npos。string: :npos 是在 string 类中定义的一个静态常量。这些函数如下:
|
||||
* string 类有一些查找子串和字符的成员函数,它们的返回值都是子串或字符在 string 对象字符串中的位置(即下标)。如果查不到,则返回 **string::npos**。string: :npos 是在 string 类中定义的一个静态常量。这些函数如下:
|
||||
|
||||
|
||||
* find:从前往后查找子串或字符出现的位置。
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include<iostream>
|
||||
#include<sstream>
|
||||
#include<string>
|
||||
using namespace std;
|
||||
|
||||
int main(){
|
||||
// istringstream s("145,5343,42,44");
|
||||
// int a;
|
||||
// s>>a;
|
||||
// cout<<a<<endl;
|
||||
|
||||
string str="12345678";
|
||||
cout<<str.erase(3,1)<<endl;
|
||||
cout<<str<<endl;
|
||||
|
||||
// char* helo;
|
||||
// s.getline(helo,10,',');
|
||||
return 0;
|
||||
}
|
||||
@@ -13,11 +13,11 @@
|
||||
|
||||
## 0 stream基础知识
|
||||
|
||||
> IO对象没有拷贝或赋值
|
||||
* IO对象没有拷贝或赋值
|
||||
|
||||
### 管理IO的状态
|
||||
|
||||
> 继承自`basic_ios`和`basic_ios_base`基类
|
||||
* 继承自`basic_ios`和`basic_ios_base`基类
|
||||
* 用来记录stream可能出现的状态。
|
||||
|
||||

|
||||
@@ -52,13 +52,12 @@ int main(){
|
||||
|
||||
### 管理输出缓冲区
|
||||
|
||||
导致缓冲区刷新的方法
|
||||
|
||||
* 程序正常结束,main函数return之后,缓冲区刷新
|
||||
* 缓冲区满时,缓冲区刷新。
|
||||
* 流操纵符endl、flush、ends,刷新缓冲区
|
||||
* 每个输出操作后,使用流操纵符unitbuf设置流的内部状态,来清空缓冲区。unitbuf是流的属性。
|
||||
* 一个输出流可能被关联到另一个流。关联到流的缓冲区会被刷新。cin、cerr、cout相互关联。
|
||||
* 导致缓冲区刷新的方法
|
||||
* 程序正常结束,main函数return之后,缓冲区刷新
|
||||
* 缓冲区满时,缓冲区刷新。
|
||||
* 流操纵符endl、flush、ends,刷新缓冲区
|
||||
* 每个输出操作后,使用流操纵符unitbuf设置流的内部状态,来清空缓冲区。unitbuf是流的属性。
|
||||
* 一个输出流可能被关联到另一个流。关联到流的缓冲区会被刷新。cin、cerr、cout相互关联。
|
||||
|
||||
```
|
||||
cout<<""<<endl;
|
||||
@@ -71,11 +70,6 @@ cout<<nounitbuf;//回到正常的刷新方式
|
||||
|
||||
### 字符类型的IO预定义
|
||||
|
||||
* IO 类本质上都是模板。可以针对任何类型IO。
|
||||
|
||||
```
|
||||
steam<type,traits>
|
||||
```
|
||||
* 针对常用的字符提供了以下IO方式。也可以自己根据定义实现二进制IO的过程。
|
||||
```
|
||||
typedef basic_ios<char> ios;
|
||||
@@ -124,41 +118,43 @@ typedef basic_osyncstream<wchar_t> wosyncstream;
|
||||
* cerr\wcerr写入到标准 C 错误流 stderr, 无缓冲(全局对象)
|
||||
* clog\wclog写入到标准 C 错误流 stderr
|
||||
|
||||
### 常用的istream方法
|
||||
## 2 stream的通用方法
|
||||
|
||||
### istream方法
|
||||
|
||||
| 有格式输入 ||
|
||||
|---|---|
|
||||
| operator>> | 提取带格式数据 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| **operator>>** | 提取带格式数据|
|
||||
| 无格式输入 |
|
||||
| get | 从流中读并取走(移除类似指针向下一个元素移动)一个字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| peek | 仅读出但不取走(不移除类似指针并未移动)一个字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| unget | 撤销流中刚取走(移除,类似指针向后退回一个位置)的字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| putback | 往输入流中退回一个字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| getline | 一直读并取走字符,直至找到给定字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| ignore | 读且取走并舍弃字符,直至发现给定字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| read | 读并取走一块字符 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| readsome | 读并取走已经可用的字符块 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| gcount | 返回上次无格式输出操作所取走的字符数量 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| **get(void)** | 从流中读并取走(移除类似指针向下一个元素移动)一个字符,关键是这几个函数他妈的参数是什么。真让人头大。以后要是再写文档。请把参数写明白。!!!!!!|
|
||||
| peek | 仅读出但不取走(不移除类似指针并未移动)一个字符 |
|
||||
| unget | 撤销流中刚取走(移除,类似指针向后退回一个位置)的字符 |
|
||||
| putback | 往输入流中退回一个字符 |
|
||||
| getline | 一直读并取走字符,直至找到给定字符。这函数有毒,不能使用 |
|
||||
| ignore | 读且取走并舍弃字符,直至发现给定字符 |
|
||||
| **read(char*,int)** | 读并取走一块字符 |
|
||||
| readsome | 读并取走已经可用的字符块 |
|
||||
| gcount | 返回上次无格式输出操作所取走的字符数量 |
|
||||
| 寻位 |
|
||||
| tellg | 返回输入位置指示器 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| seekg | 设置输入位置指示器 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| tellg | 返回输入位置指示器 |
|
||||
| seekg | 设置输入位置指示器 |
|
||||
| 杂项 |
|
||||
| sync | 与底层存储设备同步 (std::basic_istream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| sync | 与底层存储设备同步|
|
||||
|
||||
|
||||
### 常用的ostream方法
|
||||
### ostream方法
|
||||
|
||||
| 有格式输出 ||
|
||||
|---|---|
|
||||
| operator<< | 插入带格式数据 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| **operator<<** | 插入带格式数据 |
|
||||
| 无格式输出 |
|
||||
| put | 插入字符 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| write | 插入字符块 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| **put(char)** | 插入字符 |
|
||||
| **write(char*,int)** | 插入字符块 |
|
||||
| 寻位 |
|
||||
| tellp | 返回输出位置指示器 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| seekp | 设置输出位置指示器 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| tellp | 返回输出位置指示器 |
|
||||
| seekp | 设置输出位置指示器 |
|
||||
| 杂项 |
|
||||
| flush | 与底层存储设备同步 (std::basic_ostream<CharT,Traits> 的公开成员函数) [编辑] |
|
||||
| flush | 与底层存储设备同步 |
|
||||
|
||||
|
||||
### 输入方法
|
||||
@@ -179,7 +175,17 @@ gets(m); //不能写成m=gets();
|
||||
char ch;
|
||||
ch=getchar(); //不能写成getchar(ch);
|
||||
```
|
||||
### 输出方法
|
||||
|
||||
### 总结
|
||||
|
||||
> 很关键。
|
||||
> * 关于输入输出流的方法。有很多很迷的地方,他们的版本、参数都有很大的区别。从现在开始,必须严格限制,只用一下方法。优先级如下
|
||||
> 1. 流运算法>><<这两个优先级最高。凡是能用则必然使用。
|
||||
> 2. 模板函数方法getline(stream,string,delim)。这是一个通用的非绑定方法。而且可以设置字符的截止运算符。
|
||||
> 3. 流对象的字符操作函数。istream.get()和ostream.put()都是针对单个字符的操作。可以使用。
|
||||
> 4. 涉及到缓冲区的时候。使用刘对象的istream.read()和ostream.write()方法。能读取固定大小的数据。
|
||||
> * 除此之外的其他方法,肯定都是多余的。其他的方法都不必使用。
|
||||
## 1 iostream
|
||||
|
||||
### 头文件
|
||||
|
||||
14
工作日志/2021年3月15日-每日计划.md
Normal file
14
工作日志/2021年3月15日-每日计划.md
Normal file
@@ -0,0 +1,14 @@
|
||||
## 今天的计划
|
||||
|
||||
* 刷完今天的十道题
|
||||
* 刷完昨天的十道题
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
## 今天的收获
|
||||
|
||||
* 在正则表达式匹配和整数匹配哪里。把正则表达式的相关内容整理了一遍。主要通过regex/regex_match/regex_search/smatch/sregex_ietrator五个类或方法来实现正则表达式的匹配。同时对C++支持的正则表达式规则有了认真的学习。
|
||||
* 流输入输出的方法。以前对各种get/put方法有很多凌乱。今天彻底确定了流对象的所有能用的操作。最基本的支持格式化的流输入输出运算符。getline函数能够提供到终止字符的输入。read/write是用来写缓冲区或者二进制数据的。get/put使用了读取一个字符的。其他的东西。都不用记忆也不用学。
|
||||
* 把优先队列相关的内容进行总结了一下。对容器相关的操作有了新的认识。优先队列的创建、使用方法。最主要的是。所有容器的通用的方法。那些贴图太难看了。日后用到或者有不明白的地方,再回来整理md形式的表格。另外。以后记忆函数,必须包括函数的参数和返回值。如果没有参数和返回值,根本不知道怎么用。因为C++ intelli提示太垃圾了。根本看不懂。
|
||||
@@ -25,8 +25,8 @@
|
||||
|
||||

|
||||
|
||||
1. **理解问题**(确定**问题抽象**(将应用问题抽象为数学问题)、**问题分类**(搜索、排序。))
|
||||
2. **选择策略**(选择合适的**数据结构**、选择合适的**算法思想**)
|
||||
1. **问题分析**(确定**问题抽象**(将应用问题抽象为数学问题)、**问题分类**(搜索、排序。))
|
||||
2. **策略选择**(选择合适的**数据结构**、选择合适的**算法思想**)
|
||||
3. **算法设计**(确定**算法技术**,设计**算法流程**,数学递推关系和伪代码)
|
||||
4. **正确性证明**(查看伪代码的流程的正确性,**算法特例**)
|
||||
5. **算法分析**(分析算法**执行效率**)
|
||||
|
||||
4
算法/B类:数据结构算法/说明.md
Normal file
4
算法/B类:数据结构算法/说明.md
Normal file
@@ -0,0 +1,4 @@
|
||||
## 1 说明
|
||||
|
||||
* 依赖与特定数据结构的处理操作。
|
||||
|
||||
Reference in New Issue
Block a user