迭代器

This commit is contained in:
yinkanglong_lab
2021-03-06 14:54:56 +08:00
parent 63c2e49897
commit 76df1f7708
15 changed files with 293 additions and 73 deletions

View File

@@ -3,6 +3,11 @@
"python.linting.enabled": true,
"python.pythonPath": "C:\\Python\\python.exe",
"files.associations": {
"xstring": "cpp"
"xstring": "cpp",
"deque": "cpp",
"initializer_list": "cpp",
"list": "cpp",
"vector": "cpp",
"xutility": "cpp"
}
}

View File

@@ -0,0 +1,124 @@
# lambda表达式
## 1 简介
### 对象分类
对象的分类
* 基础类型的对象
* 复合类型的对象
* 类类型的对象
* 函数对象
### 可调用对象
可调用对象是可以使用函数调用运算符`()`的对象。
* 函数
* 函数指针
* 重载了函数调用运算符的类
* lambda表达式
## 2 使用
### Lambda表达式定义
* 形式。仅仅是形式上不同的函数。capture list捕获列表是外部的局部变量的列表捕获后可以在函数内部使用。
```
[capture list](parameter list) -> return type {function body}
```
* 没有参数和返回值的lambda表达式
```C++
auto l=[]{
cout<<"hello world"<<endl;
}
```
* lambda表达式的定义完成后直接调用
```C++
[]{ cout<<"helloworld"<<endl;}();
```
* 有参数的lambda表达式
```
auto l = [](int a,int b){cout<<a+b<<endl;};
```
* 有返回值的lambda表达式
```
auto l = [](int m,int n)->int{cout<<m+n<<endl;return m+n;};
```
### lambda表达式的调用
* 使用调用运算符进行调用
```
auto f = []{return 42;};
cout<<f()<<endl;
```
* 使用lambda表达式和find_if算法
```
//使用find_if和lambda表达式
vector<string> words={"abc","a","bc","feiao"};
int sz =4;
auto wc = find_if(words.begin(),words.end(),
[sz](const string &a){
return a.size()>=sz;
}
);
cout<<(*wc)<<endl;
return 0;
```
* 使用lambda表达式和for_each算法
```
// for_each给每一个元素的操作
int sz2 = 3;
for_each(words.begin(),words.end(),
[sz2](const string&a){
if(a.size()>=sz2){
cout<<a<<endl;
}
}
);
```
### 捕获列表
* 捕获列表的捕获方式
![](2021-03-06-14-01-43.png)
```
int a = 1;
auto f = [&a]{
return a;
};
```
### 返回类型
可以指定返回值的类型。
* 单个返回语句可以推断返回类型,不需要指定返回值类型
* 多个返回语句无法推断。需要指定返回类型。
```
transoform(vi.begin(),vi.end(),vi.begin(),
[](int i)->int{
if(i<0)return -i;
else return i;
}
);
```
## 3 bind 参数绑定
lambda 表达式通常只在一个地方使用的简单操作。如果需要在多个地方使用相同的操作通常定义一个函数而不是多次编写相同的lambda表达式。
bind函数适配器接受一个函数对象生成一个行的可调用的对象。
```C++
auto newCallable = bind(callable,arg_list);
auto g = bind(f,a,b,_2,c,_1);
```
* `_1,_2`分别表示新函数g的第一个和第二参数。

35
C++/标准库/11.cpp Normal file
View File

@@ -0,0 +1,35 @@
#include<iostream>
#include<algorithm>
#include<vector>
using namespace std;
int main(){
//使用find_if和lambda表达式
vector<string> words={"abc","a","bc","feiao"};
int sz =4;
auto wc = find_if(words.begin(),words.end(),
[&sz](const string &a){
return a.size()>=sz;
}
);
cout<<(*wc)<<endl;
// for_each给每一个元素的操作
int sz2 = 3;
for_each(words.begin(),words.end(),
[&sz2](const string&a){
if(a.size()>=sz2){
cout<<a<<endl;
}
}
);
// 捕获列表
int a=3;
auto f = [&a]{return ++a;};//默认捕获列表不可修改
a=10;
cout<< f()<<endl;
return 0;
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 262 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 114 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 170 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 147 KiB

View File

@@ -44,4 +44,80 @@ cend()
### 操作冲突
容器操作可能会使迭代器实效。
容器操作可能会使迭代器实效。
## 2 迭代器类型
除了容器定义的迭代器之外标准库头文件iterator总额外定义了几种迭代器。
* 插入迭代器。绑定到一个容器上,用来向容器中国插入元素
* 流迭代器。绑定到输入输出流上用来遍历关联的IO流
* 反向迭代器。反向移动除了forward_list外标准库容器都有方向迭代器。
* 移动迭代器。不会拷贝元素,而是移动元素。
### 插入迭代器
接受一个容器,生成迭代器,能够向容器中的指定位置添加元素。
![](2021-03-06-14-23-15.png)
* back_inserter:创建一个push_back迭代器
* front_inserter:创建一个push_front迭代器
* inserter:创建一个insert迭代器。接受一个指向容器的迭代器。元素将被插入到给定迭代器所表示的元素之前。
```
#include<iostream>
#include<iterator>
#include<vector>
#include<list>
using namespace std;
int main(){
list<int> lst ={1,2,3,4};
list<int> lst2,lst3;
//反向一个列表
copy(lst.cbegin(),lst.cend(),front_inserter(lst2));
//正向一个列表
copy(lst.cbegin(),lst.cend(),inserter(lst3,lst3.begin()));
return 0;
}
```
### 流迭代器
接受输入输出流,使用迭代器的方式读取数据。
* istream_iterator:读取输入流
![](2021-03-06-14-50-48.png)
```
/*
* 读取数据的三种方法
* 直接使用输入输出流iostream
* 使用字符串方法封装getline
* 使用迭代器读取输入输出流istream_iterator/ostream_iterator
*/
istream_iterator<int> in_iter(cin);//从cin读取数据
istream_iterator<int> eof;//尾后迭代器
istringstream in_string("hello world");
istream_iterator<string> str_it(in_string);//字符串流迭代器
int a =0;
a = *in_iter;
cout<<a<<endl;
// in_iter++;
cout<<*str_it<<endl;
str_it++;
cout<<*str_it<<endl;
```
* ostream_iterator:读取输出流
![](2021-03-06-14-51-10.png)

View File

@@ -0,0 +1,38 @@
#include<iostream>
#include<sstream>
#include<iterator>
#include<vector>
#include<list>
using namespace std;
int main(){
list<int> lst ={1,2,3,4};
list<int> lst2,lst3;
//反向一个列表
copy(lst.cbegin(),lst.cend(),front_inserter(lst2));
//正向一个列表
copy(lst.cbegin(),lst.cend(),inserter(lst3,lst3.begin()));
/*
* 读取数据的三种方法
* 直接使用输入输出流iostream
* 使用字符串方法封装getline
* 使用迭代器读取输入输出流istream_iterator/ostream_iterator
*/
istream_iterator<int> in_iter(cin);//从cin读取数据
istream_iterator<int> eof;//尾后迭代器
istringstream in_string("hello world");
istream_iterator<string> str_it(in_string);//字符串流迭代器
int a =0;
a = *in_iter;
cout<<a<<endl;
// in_iter++;
cout<<*str_it<<endl;
str_it++;
cout<<*str_it<<endl;
return 0;
}

View File

@@ -39,7 +39,10 @@ copy()
sort()
unique()
* 查找算法
find()
find_if()
## 2

View File

@@ -6,7 +6,7 @@
using namespace std;
//unique sort
void unique_sort(vectro<int> &word){
void unique_sort(vector<int> &words){
sort(words.begin(),words.end());
auto end_unique = unique(words.begin(),words.end());
words.erase(end_unique,words.end());
@@ -18,7 +18,14 @@ int main(){
vector<int> n{4,2,5,2,5,6,7};
for(int m :n){
cout<<m<<" "<<end;
cout<<m<<" ";
}
cout<<endl;
unique_sort(n);
for(int m :n){
cout<<m<<" ";
}
cout<<endl;
return 0;
}

View File

@@ -1,68 +0,0 @@
# Human-agent
## Human Conmputer Interaction
Internal Locus of Control
人具有绝对的主导作用。
=> Human-AI interaction
人没有绝对的主导权
## Human-Agent Collective
对人工智能的支持与反对,引发对人工智能能利弊的思考。
人与AI组成的共生态。
### 生态特点
* flexible autonomy 要有灵活的自主性。
autonomously whitout reference to their owner
human in/over/out of the loop
* agile teaming
dynamically合作方式动态变化
* incentive enginnering
* accountable informationg infrastructure
可以去解释原因人能理解的结果。可解释AI能够满足人的逻辑的结果的解释。能够对AI最优解提供具有说服力的解释。这也作为与人类狗同的一部分。
### Ethical AI vs. Human-Agent Collective对比
人工智能伦理学与智能体生态
伦理学衡量一件事的维度:数量、规则、价值。
面对道德问题时,如何设计系统。
DP 差分隐私保护。一种隐藏数据但能保重同态分布的方案。
## Collective Ethical Decision-making
多agent网络信任传递
分工不同构agent
联邦学习-把最好的数据以最快的速度拿出来。
多智能体-对抗->优化最后的解释。
## 案例1-劝说别人
生成短视频在淘宝上买东西。
wundt curve刺激节奏曲线。包括情绪、
利用9张图生成短视频。
## 案例2-医学系统
channel 诊断。初步分析病因,将病人分配给具体领域的专家。
## 案例3-超时招聘
分布式训练模型,用于选择合适的临时工。
value senesitive design
用来发现潜在的价值。

View File

@@ -11,8 +11,8 @@
- 知识复习——语言
- C++primer
- 基础语法√
- 面向对象
- 标准库 STL
- 面向对象
- 设计模式有道云笔记gitee 设计模式库,书)
- effective 系列
- 系列视频