使用tuple返回多个值
17.4编写并测试findbook函数
#include<iostream>
#include<vector>
#include<string>
#include<tuple>
#include<algorithm>
#include"Sales_data.h"
using namespace std;
typedef tuple<vector<Sales_data>::size_type,vector<Sales_data>::const_iterator,vector<Sales_data>::const_iterator> matches; vector<matches> findbook(const vector<vector<Sales_data>> &files,const string &book)
{
vector<matches> ret;
for(auto it=files.begin();it!=files.end();++it)
{
auto found=equal_range(it->begin(),it->end(),book,compareIsbn);
if(found.first!=found.second)
ret.push_back(make_tuple(it-files.begin(),found.first,found.second));
}
return ret;
} void reportResult(istream &in,ostream &os,const vector<vector<Sales_data>> &files)
{
string s;
while(in>>s)
{
auto trans=findbook(files,s);
if(trans.empty())
{
cout<<s<<" not found in any stores "<<endl;
continue;
}
for(const auto &store:trans)
{
os<<"stores "<<get<>(store)<<" sales: "
<<accumulate(get<>(store),get<>(store),Sales_data(s))
<<endl;
}
}
} int main()
{ }
17.5重写findbook,令其返回一个pair,包含一个索引和一个迭代器pair。
typedef pair<vector<Sales_data>::size_type,pair<vector<Sales_data>::const_iterator,vector<Sales_data>::const_iterator>> match;
vector<match> findbook1(const vector<vector<Sales_data>> &files,const string &book)
{
vector<match> ret;
for(auto it=files.begin();it!=files.end();++it)
{
auto found=equal_range(it->begin(),it->end(),book,compareIsbn);
if(found.first!=found.second)
ret.push_back(make_pair(it-files.begin(),make_pair(found.first,found.second)));
}
return ret;
}
使用tuple返回多个值的更多相关文章
- C++11中的tuple应用:让函数返回多个值
在没有tuple之前,如果函数需要返回多个值,则必须定义一个结构体,有了C++11,可以基于tuple直接做了,下面是个示例: // 编译:g++ -std=c++11 -g -o x x.cpp # ...
- Python学习教程(learning Python)--2.3.5 Python返回多个值问题
本节主要学习Python的函数是如何同时返回多个值的问题. 在学习Python的时候惊奇的发现,Python的函数可以同时返回多个值,很有意思. #define function sum def su ...
- python- 如何return返回多个值
函数的return 语句只能返回一个值,可以是任何类型. 因此,我们可以“返回一个 tuple类型,来间接达到返回多个值 ”. 例: x 除以 y 的余数与商的函数 def F1 ( x, ...
- java 一个函数EnumMap返回多个值
java 一个函数如何返回多个值 在开发过程中,经常会有这种情况,就是一个函数需要返回多个值,这是一个问题!! 网上这个问题的解决方法: 1.使用map返回值:这个方法问题是,你并不知道如何返回值 ...
- python return 返回多个值
python return 返回多个值,实际返回的是一个tuple: #!/usr/bin/env python # -*- coding: utf-8 -*- def rt(): return 1, ...
- java 一个函数如何返回多个值
在开发过程中,经常会有这种情况,就是一个函数需要返回多个值,这是一个问题!! 网上这个问题的解决方法: 1.使用map返回值:这个方法问题是,你并不知道如何返回值的key是什么,只能通过doc或者通过 ...
- Tuple元组 、 ValueTuple 值元组详解
Tuple元组 Tuple是C# 4.0时出的新特性,.Net Framework 4.0以上版本可用. 元组是一种数据结构,具有特定数量和元素序列,与数组不同,元祖中的元素可以不同的数据类型.比如设 ...
- C# 函数返回多个值的方法
有时候我们需要一个函数返回多个值,网上更多是用out实现,我个人很喜欢用tuple方法. tuple是一个元组,最多支持7个元素,再多需要嵌套等方法实现. 使用元组定义函数的方法如下: public ...
- 函数返回多个值(c/c++)
当我们在处理一个数组的时候常常会碰到这样的问题:输入一个数组,和数组的一个元素,返回该元素所在行数和列数.这样就需要返回多组两个值,且组数不定.上述类型的函数在c语言程序里面存在两个问题.第一,函数只 ...
随机推荐
- Discuz <= 7.2 SQL注入漏洞详情
在<高级PHP应用程序漏洞审核技术>[1]一文里的"魔术引号带来的新的安全问题"一节里,有 提到通过提取魔术引号产生的“\”字符带来的安全问题,同样这个问题在这里又一次 ...
- 练习PYTHON协程之GREENLET
STACKLESS就算了,了解一下原理即可. GREENLET,GEVENT,EVENTLET这些,比较好测试,还是都 撸一次,得个印象. 测试代码都是网上的大路货. from greenlet im ...
- Android开源项目发现---ImageView 篇(持续更新)
1. PhotoView 支持双击或双指缩放的ImageView 在ViewPager等Scrolling view中正常使用,相比上面的AndroidTouchGallery,不仅支持ViewPag ...
- 2.6.2 Notification的功能与用法
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout ...
- 17.1.1.1 Setting the Replication Master Configuration 设置复制的master 配置:
17.1.1.1 Setting the Replication Master Configuration 设置复制的master 配置: 在一个复制master,你必须启用binary loggin ...
- 【HDOJ】1597 find the nth digit
二分. #include <stdio.h> #include <math.h> int main() { int case_n; double n, tmp, l, r; i ...
- oracle core 概述
oracle数据库系统的架构及其复杂,其提供的特性也非常的多.作为一种关系型数据库,oracle提供的基本特性: transaction concurrency read consistent 而支撑 ...
- 简单的单页c#生成静态页源码
protected void BtGroup_ServerClick(object sender, EventArgs e) { //产业群首页 ...
- UVA_11796_Dog_Distance_(计算几何)
描述 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...
- 【转】BlueDroid介绍
原文网址:http://www.cnblogs.com/hzl6255/p/3887547.html 自从Android 4.2开始,Android开始使用自己的蓝牙协议栈BlueDroid,而不是b ...