码迷,mamicode.com
首页 > 其他好文 > 详细

第八章 IO类

时间:2018-02-02 14:19:14      阅读:181      评论:0      收藏:0      [点我收藏+]

标签:big   sort   using   include   amp   tab   std   for   第八章   

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>

using namespace std;
using namespace std::placeholders;

void biggies(vector<string> &words, vector<string>::size_type sz);
ostream &print(ostream &os, const string &s, char c);
bool check_size(const string & s1, vector<string>::size_type sz);
void elimDups(vector<string> &words);
bool isShorter(const string &s1, const string &s2);
string make_plural(vector<string>::size_type cnt, const string &s1, const string &s2);

int main()
{
    ifstream in("test.txt");
    if (!in)
    {
        cout << "无法打开此文件" << endl;
        exit(1);
    }

    string word;
    vector<string> vec;
    while (in >> word)
        vec.push_back(word);
    for (const auto &s : vec)
        cout << s << " ";
    cout << endl;
    biggies(vec, 4);
}

void biggies(vector<string> &words, vector<string>::size_type sz)
{
    elimDups(words);
    stable_sort(words.begin(), words.end(), bind(isShorter, _1, _2));
    auto wc = find_if(words.begin(), words.end(), bind(check_size, _1, sz));
    auto count = words.end() - wc;
    cout << count << " " << make_plural(count, "word", "s")
        << " length of " << sz << " or longer";
    for_each(wc, words.end(), bind(print, ref(cout), _1,  ));
}

string make_plural(vector<string>::size_type cnt, const string &s1, const string &s2)
{
    if (cnt > 1)
        return s1 + s2;
    else
        return s1;
}
ostream &print(ostream &os, const string &s, char c)
{
    return os << s << c;
}
bool check_size(const string & s1, vector<string>::size_type sz)
{
    return s1.size() > sz;
}

void elimDups(vector<string> &words)
{
    sort(words.begin(), words.end());
    auto unique_end = unique(words.begin(), words.end());
    words.erase(unique_end, words.end());
}
bool isShorter(const string &s1, const string &s2)
{
    return s1.size() < s2.size();
}

 

#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include<algorithm>
#include<functional>

using namespace std;
using namespace std::placeholders;

void biggies(vector<string> &words, vector<string>::size_type sz);
ostream &print(ostream &os, const string &s, char c);
bool check_size(const string & s1, vector<string>::size_type sz);
void elimDups(vector<string> &words);
bool isShorter(const string &s1, const string &s2);
string make_plural(vector<string>::size_type cnt, const string &s1, const string &s2);

int main()
{
	ifstream in("test.txt");
	if (!in)
	{
		cout << "无法打开此文件" << endl;
		exit(1);
	}

	string word;
	vector<string> vec;
	while (in >> word)
		vec.push_back(word);
	for (const auto &s : vec)
		cout << s << " ";
	cout << endl;
	biggies(vec, 4);
}

void biggies(vector<string> &words, vector<string>::size_type sz)
{
	elimDups(words);
	stable_sort(words.begin(), words.end(), bind(isShorter, _1, _2));
	auto wc = find_if(words.begin(), words.end(), bind(check_size, _1, sz));
	auto count = words.end() - wc;
	cout << count << " " << make_plural(count, "word", "s")
		<< " length of " << sz << " or longer";
	for_each(wc, words.end(), bind(print, ref(cout), _1, ‘ ‘));
}

string make_plural(vector<string>::size_type cnt, const string &s1, const string &s2)
{
	if (cnt > 1)
		return s1 + s2;
	else
		return s1;
}
ostream &print(ostream &os, const string &s, char c)
{
	return os << s << c;
}
bool check_size(const string & s1, vector<string>::size_type sz)
{
	return s1.size() > sz;
}

void elimDups(vector<string> &words)
{
	sort(words.begin(), words.end());
	auto unique_end = unique(words.begin(), words.end());
	words.erase(unique_end, words.end());
}
bool isShorter(const string &s1, const string &s2)
{
	return s1.size() < s2.size();
}

  

第八章 IO类

标签:big   sort   using   include   amp   tab   std   for   第八章   

原文地址:https://www.cnblogs.com/sunbines/p/8404550.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!