C++ primer习题记录——第十章

10.1:

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>

int main()
{
    vector<int>vec = { 1,2,3,3,3,4,3,3,5,6,7 };
    auto num = count(vec.begin(), vec.end(), 3);
    cout << "数字3重复的次数为: " << num << endl;
    return 0;
}

10.2:

#include <iostream>
using namespace std;
#include<list>
#include<algorithm>

int main()
{
    list<string> ls = { "asd","sad","dsa","sad" };
    string str;
    while (cin >> str)
    {
        auto num = count(ls.begin(), ls.end(), str);
        cout << "sad重复的次数为: " << num << endl;
    }
    
    return 0;
}

10.3:

#include <iostream>
using namespace std;
#include<vector>
#include<numeric>

int main()
{
    vector<int>vec = { 1,2,3,3,3,4,3,3,5,6,7 };
    auto sum = accumulate(vec.cbegin(), vec.cend(), 0);
    cout << "和: " << sum << endl;
    return 0;
}

10.5:

#include <iostream>
using namespace std;
#include<vector>


int main()
{
    vector<const char*>vec;
    char c[] = {'b','s','d','f' ,'\0'};
    vec.push_back(c);
    vector<const char*>vec2;
    char c2[] = { 'b','s','d','f' ,'\0' };
    vec2.push_back(c2);
    bool b = equal(vec.begin(), vec.end(), vec2.begin());
    cout << b << endl;
    
    return 0;
}

10.6:

#include <iostream>
using namespace std;
#include<vector>

int main()
{
    vector<int>vec = {1,2,3,4,5};
    fill_n(vec.begin(), vec.size(), 0);
    for (auto v : vec) { cout << v << " "; }cout << endl;
    
    return 0;
}

10.9:

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
void elimDups(vector<string>& word)
{
    sort(word.begin(), word.end());
    cout << "sort排序后: " << endl;
    for (auto v : word) { cout << v << " "; }cout << endl;
    auto end_unique = unique(word.begin(), word.end());
    cout << "unique后:" << endl;
    for (auto v : word) { cout << v << " "; }cout << endl;
    //cout << *end_unique << endl;
    word.erase(end_unique, word.end());
    cout << "erase后:" << endl;
    for (auto v : word) { cout << v << " "; }cout << endl;
}
int main()
{
    vector<string>ves{ "a", "v", "a", "s", "v", "a", "a" };
    cout << "输入后:" << endl;
    for (auto v : ves) { cout << v << " "; }cout << endl;
    elimDups(ves);
    return 0;
}

10.11:

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
void elimDups(vector<string>& word)
{
    sort(word.begin(), word.end());
    auto end_unique = unique(word.begin(), word.end());
    word.erase(end_unique, word.end());
    cout << "erase后:" << endl;
    for (auto v : word) { cout << v << " "; }cout << endl;
}
bool isshorter(const string& s1, const string& s2)
{
    return s1.size() < s2.size();
}
int main()
{
    vector<string>ves{ "the", "quick", "red", "fox", "jumps", "over", "the","slow","red","turtle"};
    cout << "输入后:" << endl;
    for (auto v : ves) { cout << v << " "; }cout << endl;
    elimDups(ves);
    stable_sort(ves.begin(), ves.end(), isshorter);
    cout << "stable_sort排序后: " << endl;
    for (auto v : ves) { cout << v << " "; }cout << endl;
    return 0;
}

10.12:

#include <iostream>
using namespace std;
#include"Sales_data.h"
#include<vector>
#include<algorithm>
bool compareIsbn(const Sales_data& s1, const Sales_data& s2)
{
	return s1.isbn().size() < s2.isbn().size();
}
int main()
{
	vector<Sales_data>ves{ {"a"},{"aa"},{"aaa"},{"aaz"},{"aac"}};
	stable_sort(ves.begin(), ves.end(), compareIsbn);
	for (auto v : ves) { cout << v.isbn() << " "; }cout << endl;

	system("pause");
	return 0;
}

10.13:

#include <iostream>
using namespace std;
#include"Sales_data.h"
#include<vector>
#include<algorithm>
bool greater5(const string&s)
{
	return s.size() >= 5;
}
int main()
{
	vector<string>ves{ "asd","asdqwed","qweqweqweqw","asd","qwe","a","saddaasd" };
	auto ret = partition(ves.begin(), ves.end(), greater5);
	for (auto beg = ves.cbegin(); beg != ret; ++beg)
	{
		cout << *beg << " ";
	}cout << endl;

	system("pause");
	return 0;
}

10.14:

#include <iostream>
using namespace std;

int main()
{
	auto add = [](const int& a, const int& b) {return a + b; };
	cout << add(10, 20) << endl;
	system("pause");
	return 0;
}

10.15:

void test(int a)
{
	auto f = [a](int b) {return a + b; };
}

10.16;

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>

void elimDups(vector<string>& word)
{
    sort(word.begin(), word.end());
    auto end_unique = unique(word.begin(), word.end());
    word.erase(end_unique, word.end());
}
void biggies(vector<string>& vs, vector<string>::size_type sz)
{
    elimDups(vs);
    stable_sort(vs.begin(), vs.end(), [](const string& s1, const string& s2) {return s1.size() < s2.size(); });
    auto it = find_if(vs.begin(), vs.end(), [sz](const string& s) {return s.size() >= sz; });
    auto count = vs.end() - it;
    cout << "满足size >= sz的元素数目是: " << count << endl;
    for_each(it, vs.end(), [](const string& s) {cout << s << " "; });
    cout << endl;
}
int main()
{
    vector<string>ves{ "the", "quick", "red", "fox", "jumps", "over", "the","slow","red","turtle" };
    cout << "输入后:" << endl;
    for (auto v : ves) { cout << v << " "; }cout << endl;
    biggies(ves, 4);
    return 0;
}

10.17:

#include <iostream>
using namespace std;
#include"Sales_data.h"
#include<vector>
#include<algorithm>

int main()
{
	vector<Sales_data>ves{ {"a"},{"aa"},{"aaa"},{"aaz"},{"aac"} };
	stable_sort(ves.begin(), ves.end(), 
		[](const Sales_data& s1, const Sales_data& s2) 
        {return s1.isbn().size() < s2.isbn().size(); });
	for (auto v : ves) { cout << v.isbn() << " "; }cout << endl;
	system("pause");
	return 0;
}

10.18:

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>

void elimDups(vector<string>& word)
{
    sort(word.begin(), word.end());
    auto end_unique = unique(word.begin(), word.end());
    word.erase(end_unique, word.end());
}
void biggies(vector<string>& vs, vector<string>::size_type sz)
{
    elimDups(vs);
    stable_sort(vs.begin(), vs.end(), [](const string& s1, const string& s2) {return s1.size() < s2.size(); });
    auto it = partition(vs.begin(), vs.end(), [sz](const string& s) {return s.size() < sz; });
    auto count = vs.end() - it;
    cout << "满足size >= sz的元素数目是: " << count << endl;
    for_each(it, vs.end(), [](const string& s) {cout << s << " "; });
    cout << endl;
}
int main()
{
    vector<string>ves{ "the", "quick", "red", "fox", "jumps", "over", "the","slow","red","turtle" };
    cout << "输入后:" << endl;
    for (auto v : ves) { cout << v << " "; }cout << endl;
    biggies(ves, 4);
    return 0;
}

10.19:

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>

void elimDups(vector<string>& word)
{
    sort(word.begin(), word.end());
    auto end_unique = unique(word.begin(), word.end());
    word.erase(end_unique, word.end());
}
void biggies(vector<string>& vs, vector<string>::size_type sz)
{
    elimDups(vs);
    stable_sort(vs.begin(), vs.end(), [](const string& s1, const string& s2) {return s1.size() < s2.size(); });
    auto it = stable_partition(vs.begin(), vs.end(), [sz](const string& s) {return s.size() < sz; });
    auto count = vs.end() - it;
    cout << "满足size >= sz的元素数目是: " << count << endl;
    for_each(it, vs.end(), [](const string& s) {cout << s << " "; });
    cout << endl;
}
int main()
{
    vector<string>ves{ "the", "quick", "red", "fox", "jumps", "over", "the","slow","red","turtle" };
    cout << "输入后:" << endl;
    for (auto v : ves) { cout << v << " "; }cout << endl;
    biggies(ves, 4);
    return 0;
}

10.20:

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>

int main()
{
    vector<string>ves{ "theasdsad","fox", "jumps", "over", "the","slow","red","turtlesss" };
    auto num = count_if(ves.begin(), ves.end(), [](const string& s) {return s.size() > 6; });
    cout << num << endl;
    return 0;
}

10.21:

#include <iostream>
using namespace std;

void func()
{
    int i = 10;
	auto f = [&]()->bool {if (i > 0) { --i; return true; }
	else { return false; } };
	while (f())
	{
		cout << i << endl;
	}
}
int main()
{
	func();
	return 0;
}

10.22:

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
using std::placeholders::_1;
bool shorter6(const string& s,string::size_type sz)
{
    return s.size() <= sz;
}
int main()
{
    vector<string>ves{ "theasdsad","fox", "jumps", "over", "the","slow","red","turtlesss" };
    auto num = count_if(ves.begin(), ves.end(),bind(shorter6,_1,6));
    cout << num << endl;
    return 0;
}

10.24:

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
using std::placeholders::_1;
bool check_size(const string& s,string::size_type sz)
{
    return s.size() < sz;
}
int main()
{
    vector<int>ves{ 0,1,2,3,4,5,6,7,8,9,10};
    string s = "asdf";
    auto it = find_if(ves.begin(), ves.end(),bind(check_size,s,_1));
    if (it != ves.end())
        cout << *it << endl;
    else
        cout << "Not found" << endl;
    return 0;
}

10.25:

#include <iostream>
using namespace std;
#include<vector>
#include<algorithm>
#include<functional>
using std::placeholders::_1;
void elimDups(vector<string>& word)
{
    sort(word.begin(), word.end());
    auto end_unique = unique(word.begin(), word.end());
    word.erase(end_unique, word.end());
}
bool func(const string& s, string::size_type sz)
{
    return s.size() < sz;
}
void biggies(vector<string>& vs, vector<string>::size_type sz)
{
    elimDups(vs);
    stable_sort(vs.begin(), vs.end(), [](const string& s1, const string& s2) {return s1.size() < s2.size(); });
    auto it = partition(vs.begin(), vs.end(), bind(func, _1, sz));
    auto count = vs.end() - it;
    cout << "满足size >= sz的元素数目是: " << count << endl;
    for_each(it, vs.end(), [](const string& s) {cout << s << " "; });
    cout << endl;
}
int main()
{
    vector<string>ves{ "the", "quick", "red", "fox", "jumps", "over", "the","slow","red","turtle" };
    cout << "输入后:" << endl;
    for (auto v : ves) { cout << v << " "; }cout << endl;
    biggies(ves, 4);
    return 0;
}

10.27:

#include <iostream>
using namespace std;
#include<vector>
#include<list>
#include<algorithm>
int main()
{
    vector<int>vec = { 1,2,2,3,3,4,4,4,4,5,6 };
    list<int>li;
    unique_copy(vec.begin(), vec.end(), back_inserter(li));
    for (auto l : li) { cout << l << " "; }cout << endl;
    return 0;
}

10.28:

#include <iostream>
using namespace std;
#include<vector>
#include<list>
#include<algorithm>
int main()
{
    vector<int>vec = { 1,2,3,4,5,6,7,8,9 };
    vector<int>vec2;
    list<int>vec3;
    vector<int>vec4;
    copy(vec.begin(), vec.end(), back_inserter(vec2));
    for (auto l : vec2) { cout << l << " "; }cout << endl;
    copy(vec.begin(), vec.end(), front_inserter(vec3));
    for (auto l : vec3) { cout << l << " "; }cout << endl;
    copy(vec.begin(), vec.end(), inserter(vec4,vec4.begin()));
    for (auto l : vec4) { cout << l << " "; }cout << endl;
    return 0;
}

10.29:

#include <iostream>
using namespace std;
#include<vector>
#include<fstream>
#include<algorithm>
int main()
{
    ifstream ifs("1.txt");
    istream_iterator<string> in(ifs), eof;
    vector<string>ves(in, eof);
    ostream_iterator<string> out_iter(cout, " ");
    copy(ves.begin(), ves.end(), out_iter);
    cout << endl;
    return 0;
}

10.30:

#include <iostream>
using namespace std;
#include<vector>
#include<fstream>
#include<algorithm>
int main()
{
    istream_iterator<int> in(cin), eof;
    vector<int>vec;
    copy(in, eof, back_inserter(vec));
    sort(vec.begin(), vec.end());
    ostream_iterator<int> out_iter(cout, " ");
    copy(vec.begin(), vec.end(), out_iter);
    cout << endl;
    return 0;
}

10.31:

#include <iostream>
using namespace std;
#include<vector>
#include<fstream>
#include<algorithm>
int main()
{
    istream_iterator<int> in(cin), eof;
    vector<int>vec;
    while (in != eof)
    {
        vec.push_back(*in++);
    }
    sort(vec.begin(), vec.end());
    ostream_iterator<int> out_iter(cout, " ");
    unique_copy(vec.cbegin(), vec.cend(), out_iter);
    cout << endl;
    return 0;
}

10.32:

#include <iostream>
using namespace std;
#include<vector>
#include<fstream>
#include<algorithm>
#include<numeric>
#include"Sales_item.h"
int main()
{
    istream_iterator<Sales_item> in(cin), eof;
    vector<Sales_item>vec;
    while (in != eof)
    {
        vec.push_back(*in++);
    }
    sort(vec.begin(), vec.end(), [](const Sales_item& s1, const Sales_item& s2) 
        {return s1.isbn().size() < s2.isbn().size(); });
    for (auto beg = vec.begin(), end = beg; beg != vec.end(); beg = end)
    {
        end = find_if(beg, vec.end(), [beg](const Sales_item& s) {return beg->isbn() != s.isbn(); });
        cout << accumulate(beg, end, Sales_item(beg->isbn())) << endl;
    }
    return 0;
}

10.33:

#include <iostream>
using namespace std;
#include<vector>
#include<fstream>
#include<algorithm>
int main()
{
    ifstream ifs("1.txt");
    istream_iterator<int> in(ifs), eof;
    ofstream ofs("2.txt"), ofs2("3.txt");
    ostream_iterator<int> out_iter(ofs, " "), out_iter2(ofs2, "\n");
    for (; in != eof; ++in)
    {
        if (*in % 2 == 0)
        {
            *out_iter = *in;
        }
        else
        {
            *out_iter2 = *in;
        }
    }
    return 0;
}

10.34:

#include <iostream>
using namespace std;
#include<vector>

int main()
{
    vector<int>vec = { 1,2,3,4,5,6,7,8,9 };
    for (auto r_beg = vec.crbegin(); r_beg != vec.crend(); r_beg++)
    {
        cout << *r_beg << " ";
    }cout << endl;
    return 0;
}

10.35;

#include <iostream>
using namespace std;
#include<vector>

int main()
{
    vector<int>vec = { 1,2,3,4,5,6,7,8,9 };
    for (auto last = vec.cend(); last !=vec.begin(); --last)
    {
        cout << *(last-1) << " ";
    }cout << endl;
    return 0;
}

10.36:

#include <iostream>
using namespace std;
#include<list>

int main()
{
    list<int>li = { 1,2,3,4,0,2,0,4,8 };
    auto it = find(li.crbegin(), li.crend(), 0);
    
    return 0;
}

10.37:

#include <iostream>
using namespace std;
#include<list>
#include<vector>
int main()
{
    vector<int>vi = { 1,2,3,4,0,2,0,4,8,10 };
    list<int>li;
    copy(vi.crbegin()+4, vi.crend()-3, back_inserter(li));
    for (auto s : li) { cout << s << " "; }cout << endl;
    
    return 0;
}

10.42:

#include <iostream>
using namespace std;
#include<list>

void elimDups(list<int>& i)
{
    i.sort();
    i.unique();
}

int main()
{
    list <int>li = { 1,2,2,4,2,3,4,5,67,5,1 };
    elimDups(li);
    for (auto l : li) { cout << l << " "; }cout << endl;
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值