c++实现类似python的map一样,批量操作一个vector的功能【python一样写c++、三】
python里有一个东西,叫map。
它可以实现像这样,对list每个元素进行操作,并返回新的list(python3是迭代器)
像这样
a=list(map(int,input().split()))
b=list(map(lambda x:x*2,a))
遇到这个,我们就会像:c++可不可以也搞一个,对vector每个元素进行操作的函数?
emm,就叫my_map好了。
有了想法,那就开干呗!
老样子,先放成品(完整程序和注释版在后面)
template<typename Ori,typename Func_Type>
auto my_map(Func_Type my_func,vector<Ori> &x){
using return_type = typename result_of<Func_Type(Ori)>::type;
vector<return_type> tmp;
for(auto i :x)
tmp.push_back(my_func(i));
return tmp;
}
int main()
{
auto s=input().split();
cout<<s;
auto a=my_map(my_int,s);//my_int是函数,string转int
cout<<a;
auto b=my_map(Dao_Shu(),a);//Dao_Shu是一个重载了()的类(结构体)
cout<<b;
auto ping_fang=[](int x) { return x*x;};
auto c=my_map(ping_fang,a);//ping_fang是lambda表达式(平方的作用)
cout<<c;
long long sum=0;//同理,可以实现这种操作:求前缀和
auto leijia=[=](int x) mutable{ return sum+=x;};
auto d=my_map(leijia,a);
cout<<d;
}
本文作者XXOY
那个input().split(),和cout<<vector可以看我之前的文章
input().split()
这次的代码虽然短,但或许确实是(目前)技术含量最高的一篇了。
0.前置芝士:
(1):c++11的新特性(比如auto、lambda)
(2):c++的template(模板)与函数指针
1.首先考虑要实现的功能:
传一个函数与一个vector,返回对vector中每一个元素使用这个函数的新vector。
于是就有了第一版
template<typename T,typename T2>
vector<T2> my_map(T2 (*co)(T),vector<T> yuan){
vector<T2> x;
for(auto i:yuan)
x.push_back(co(i));
return x;
}
传递一个函数指针进去,然后进行操作。
但这样只能搞普通的函数,不能操纵lambda,会报错。
进行了一番百度、google以后发现lambda本质是一个重载过()的类(某种意义上)
于是有了第二版:
template<typename Ori,typename Func_Type>
auto my_map(Func_Type my_func,vector<Ori> &x){
auto k=my_func(yuan[0]);
vector<decltype(k)> tmp;
for(auto i :x)
tmp.push_back(my_func(i));
return tmp;
}
为什么搞一个k呢?因为那时我不知道如何获取一个函数返回值的类型,只能傻傻的先去操作一下先。
会遇到什么问题呢?空的vector会报错。
遂继续百度(这足足花了两天)。
才知道有种东西叫result_of,专门用来获取返回值类型的。
于是--->第三版
template<typename Ori,typename Func_Type>
auto my_map(Func_Type my_func,vector<Ori> &x){
using return_type = typename result_of<Func_Type(Ori)>::type;
vector<return_type> tmp;
for(auto i :x)
tmp.push_back(my_func(i));
return tmp;
}
//result_of 是获取返回值类型的意思。
//可以不需要T2 (*my_func)(T)像这样定义函数,直接把它当一个类用就行了
//对于lambda:某种意义上,它本质上就是一个重载了()的类
OK,写完,收工。
现在,它可以实现如下骚操作。
auto s=input().split();
//s的类型是vector<string>
cout<<s;
auto a=my_map(my_int,s);
//my_int是函数,string转int
//a的类型是vector<int>
cout<<a;
//Dao_Shu是一个重载了()的类(结构体)
auto b=my_map(Dao_Shu(),a);
//DS是一个类型为Dao_Shu的变量
//也可以auto b=my_map(DS,a);
//b是vector<double>
cout<<b;
auto ping_fang=[](int x) { return x*x;};
//ping_fang是lambda表达式(平方的作用)
auto c=my_map(ping_fang,a);
//c是vector<int>
cout<<c;
long long sum=0;
auto leijia=[=](int x) mutable{ return sum+=x;};
//同理,可以实现这种操作:求前缀和
auto d=my_map(leijia,a);
//d是vector<long long>
cout<<d;
完整代码:
#include<bits/stdc++.h>
using namespace std;
//split()和input()的实现
struct mystring:string{//继承string类
mystring() :string() {}
mystring& operator=(const string &c) {
(*this).assign(c);
return *this;
}
vector<string> split(char c=' '){
vector<string> x;
int last=0,len=0;
for(int i=0;i<size();++i)
if(at(i)==c){
x.push_back(substr(last,len));
last=i+1;
len=0;
}
else ++len;
if(last<size())
x.push_back(substr(last,len));
return x;
}
}s;
mystring input(){
mystring s;
getline(cin,s);
return s;
}
//重载cout<<
namespace py_pr{
template<typename T>
inline ostream& out_put(ostream& o,const T & x){
return o<<x;
}
inline ostream& out_put(ostream& o,const string& x){
return o<<"\""<<x<<"\"";
}
inline ostream& out_put(ostream& o,const char* & x){
return o<<"\""<<x<<"\"";
}
inline ostream& out_put(ostream& o,const char & x){
return o<<"\'"<<x<<"\'";
}
template<typename T1,typename T2>
inline ostream& out_put(ostream& o,const pair<T1,T2> & x){
out_put(o,x.first);
o<<": ";
out_put(o,x.second);
return o;
}
}
template<typename T>
ostream& operator<<(ostream &o,vector<T> &x){
o<<"[";
for(auto i=x.begin();i<x.end();++i){
//可以直接for(auto i:x),但是我不知道怎么特判第一个来控制","
if(i!=x.begin()) o<<", ";
py_pr::out_put(o,*i);
}
o<<"]"<<endl;
return o;
}
template<typename Ori,typename Func_Type>
auto my_map(Func_Type my_func,vector<Ori> &x){
using return_type = typename result_of<Func_Type(Ori)>::type;
vector<return_type> tmp;
for(auto i :x)
tmp.push_back(my_func(i));
return tmp;
}
//result_of 是获取返回值类型的意思。
//可以不需要T2 (*my_func)(T)像这样定义函数,直接把它当一个类用就行了
//对于lambda:某种意义上,它本质上就是一个重载了()的类
int my_int(const string x){
return stoi(x);
}
struct Dao_Shu{
double operator() (double x){
return 1.0/x;
}
}DS;
int main()
{
auto s=input().split();
//s的类型是vector<string>
cout<<s;
auto a=my_map(my_int,s);
//my_int是函数,string转int
//a的类型是vector<int>
cout<<a;
//Dao_Shu是一个重载了()的类(结构体)
auto b=my_map(Dao_Shu(),a);
//DS是一个类型为Dao_Shu的变量
//也可以auto b=my_map(DS,a);
//b是vector<double>
cout<<b;
auto ping_fang=[](int x) { return x*x;};
//ping_fang是lambda表达式(平方的作用)
auto c=my_map(ping_fang,a);
//c是vector<int>
cout<<c;
long long sum=0;
auto leijia=[=](int x) mutable{ return sum+=x;};
//同理,可以实现这种操作:求前缀和
auto d=my_map(leijia,a);
//d是vector<long long>
cout<<d;
}
最后的最后Q&A环节:
1.Q:为什么要搞这种东西呢?
A:因为我开心。
2.不能做到的:直接传stoi之类的函数。
原因大概是其中有缺省参数,参数表不匹配。会编译不通过
解决办法:包个壳(感谢@たくやりん 提供的方法)
比如这样int my_int(string x){ return stoi(x); }
更多内容,包括那个input().split(),和cout<<vector可以看我的主页。
input().split()
cout<<
我是XXOY,我们下次再见。
c++实现类似python的map一样,批量操作一个vector的功能【python一样写c++、三】的更多相关文章
- 《python解释器源码剖析》第1章--python对象初探
1.0 序 对象是python中最核心的一个概念,在python的世界中,一切都是对象,整数.字符串.甚至类型.整数类型.字符串类型,都是对象.换句话说,python中面向对象的理念观测的非常彻底,面 ...
- python之map、filter、reduce、lambda函数 转
python之map.filter.reduce.lambda函数 转 http://www.cnblogs.com/kaituorensheng/p/5300340.html 阅读目录 map ...
- Python【map、reduce、filter】内置函数使用说明(转载)
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- python基础——map/reduce
python基础——map/reduce Python内建了map()和reduce()函数. 如果你读过Google的那篇大名鼎鼎的论文“MapReduce: Simplified Data Pro ...
- 【转】Python 中map、reduce、filter函数
转自:http://www.blogjava.net/vagasnail/articles/301140.html?opt=admin 介绍下Python 中 map,reduce,和filter 内 ...
- [spark][python]Spark map 处理
map 就是对一个RDD的各个元素都施加处理,得到一个新的RDD 的过程 [training@localhost ~]$ cat names.txtYear,First Name,County,Sex ...
- 使用Python实现Map Reduce程序
使用Python实现Map Reduce程序 起因 想处理一些较大的文件,单机运行效率太低,多线程也达不到要求,最终采用了集群的处理方式. 详细的讨论可以在v2ex上看一下. 步骤 MapReduce ...
- python的map函数的使用方法详解以及使用案例(处理每个元素的自增、自减、平方等)
1.用我们之前学过的求一下平方(只有一个列表) #求平方 num=[1,5,6,2,7,8] a=[] for n in num: a.append(n**2) print (a) C:\python ...
- [译]PYTHON FUNCTIONS - MAP, FILTER, AND REDUCE
map, filter, and reduce Python提供了几个函数,使得能够进行函数式编程.这些函数都拥有方便的特性,他们可以能够很方便的用python编写. 函数式编程都是关于表达式的.我们 ...
- python中map()和dict()的用法
map()用法 map()是python的内置函数,会根据提供的函数对指定序列做映射. 语法: map(func, iter, ...) 其中func为一个功能函数,iter表示可迭代参数序列.map ...
随机推荐
- MySQL 常用命令(1)------连接、添加用户与授权
一.连接MySQL 格式: mysql -h主机地址 -u用户名 -p用户密码 1.连接到本机上的MYSQL 进入目录mysql\bin,再键入命令mysql -u root -p,回车后提示你输密码 ...
- C++ 读取文本, 读取( 单字符/ 一行/ 全部 )
C++ 读取文本 介绍三种读取方式: 逐字符读取(注意不是字节) 读取一行 读取全部 示例代码: #include <iostream> #include <string> # ...
- (jmeter笔记)模拟用户各种场景压测
插件:standard set 1.jp@gc - Ultimate Thread Group(波浪式压测,最终线程组) Start Threads Count :设置启用并发数 Initial De ...
- TypeScript - 继承(extends) - 抽象类(abstract)
(function () { /** * abstract 开头的是抽象类, * 抽象类和其他区别不大,只是不能用来创建对象 * 抽象类专门是让别人继承的的类 */ abstract class An ...
- PostScript语言教程(六、图形变换)
6.1.坐标系变换 POSTSCRIPT图形操作是在一个坐标系中,这个坐标系被称为用户坐标系或用户空间,该坐标系独立于任何物理设备.POSTSCRIPT在用户空间中进行绘制,并将结果传输到特定的打印机 ...
- MFC中利用CFileDialog选择文件并读取文件所遇到的问题和解决方法
在用MFC编写一个上位机时,需要实现选择和读取一个二进制文件,本来以为很简单的但是在实现过程中遇到很多问题,所幸都一一解决,这里做一下记录. 首先在实现文件选择,在界面上设置一个按钮,并在点击事件函数 ...
- element 换肤
官网操作 https://element.eleme.cn/#/zh-CN/component/custom-theme 然后 执行 et -i 报错了!!! 查了一下,说的是node版本过高?那我就 ...
- S32DS中链接文件及启动代码学习
S32DS中链接文件及启动代码学习 一.链接文件 <Linker Files>文件夹中有linker_flash.ld文件和linker_ram.ld文件. Linker File称为链接 ...
- Linux 使用Postfix与Dovecot部署邮件系统
电子邮件系统 电子邮件系统基于邮件协议来完成电子邮件的传输,常见的邮件协议有下面这些. 简单邮件传输协议(Simple Mail Transfer Protocol,SMTP):用于发送和中转发出的电 ...
- Netty基本编写
一. public class Server { public static void main(String[] args) throws Exception { //1 创建线两个程组 //一个是 ...