第八章 IO类

Posted sunbines

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第八章 IO类相关的知识,希望对你有一定的参考价值。

#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类的主要内容,如果未能解决你的问题,请参考以下文章

使用Java实现面向对象编程——第八章 File IO

Thymeleaf(第八章)模板布局

Java基础知识二次学习--第八章 流

使用java实现面向对象 第八章

IO流入门-第八章-BufferedWriter

Python入门到精通精品第八章 - 异常