ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

012:编程填空:Printer

2021-09-05 11:29:47  阅读:224  来源: 互联网

标签:Printer int 012 each threshold 填空 fn first


描述

完成以下程序,使得输入的整数x,以及若干正整数,将
大于x的正整数输出;然后输入若干字符串,将字符串长度大于x的字符串输出

#include<iostream>
#include<algorithm>
#include<vector>
#include<bitset>

using namespace std;


class Printer{
// 在此处补充你的代码
int main(){

	int t;
	cin >> t;
	while(t--) {
		int n,x;
		cin>>x>>n;
		
		vector<int> intVec;
		for(int i = 0;i < n; ++i) {
			int y;
			cin >> y;
			intVec.push_back(y);
		}
		for_each(intVec.begin(), intVec.end(), Printer(x));
		cout<<endl;
		
		vector<string> strVec;
		for(int i = 0;i < n; ++i) {
			string str;
			cin >> str;
			strVec.push_back(str);
		}
		for_each(strVec.begin(), strVec.end(), Printer(x));
		cout<<endl;
	}
	return 0;
}

输入

第一行是整数t,表示一共t组数据
每组数据有三行 
第一行是整数x和整数 n 
第二行是n个整数 
第三行是n个不带空格的字符串

输出

对每组数据
先按原序输出第一行中大于x的正整数(数据保证会有输出) 
再按原序输出第二行中长度大于x的字符串 (数据保证会有输出)

样例输入

2
5 6
1 3 59 30 2 40
this is hello please me ha
1 1
4
this

样例输出

59,30,40,
please,
4,
this,

class Printer
{
    int threshold;

public:
    Printer(int _threshold) : threshold(_threshold) {}
    void operator()(int x)
    {
        if (x > threshold)
            cout << x << ',';
    }
    void operator()(string str)
    {
        if (str.size() > threshold)
            cout << str << ',';
    }
};

简单的仿函数。for_each的使用参考http://www.cplusplus.com/reference/algorithm/for_each/

function template
<algorithm>
std::for_each
template <class InputIterator, class Function>
   Function for_each (InputIterator first, InputIterator last, Function fn);
Apply function to range
Applies function fn to each of the elements in the range [first,last).

The behavior of this template function is equivalent to:

template<class InputIterator, class Function>
  Function for_each(InputIterator first, InputIterator last, Function fn)
{
  while (first!=last) {
    fn (*first);
    ++first;
  }
  return fn;      // or, since C++11: return move(fn);
}

标签:Printer,int,012,each,threshold,填空,fn,first
来源: https://blog.csdn.net/apple_55860091/article/details/120111831

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有