C++ Primer 第5版章节题 第十章

复制代码
节练习 10.1
chapter10_1.cpp
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
    vector<int> v({1,2,34,5,6,7,85,3,5,6,3,3,4,35,1,3,5,6,34,5,3,5,6,7,87,54,5,3,4});
    int c = count(v.begin(),v.end(),3);
    cout << "c = " << c << endl;
    return 0;
}
编译: g++ -std=c++11 -o chapter10_1  chapter10_1.cpp

节练习 10.2
chapter10_1.cpp
#include <algorithm>
#include <iostream>
#include <list>
using namespace std;

int main() {
    list<string> v({"1","2","34","5","6","7","85","3","5","6","3","3","4","35","1","3","5","6","34","5","3","5","6","7","87","54","5","3","4"});
    int c = count(v.begin(),v.end(),"6");
    cout << "c = " << c << endl;
    return 0;
}
编译: g++ -std=c++11 -o chapter10_1  chapter10_1.cpp

节练习 10.3
chapter10.cpp
// #include <algorithm>
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;


int main()
{
   vector<int> v = {3,23,455,64,5,22,456,789,864,234,5,6,7,89,97};
   int sum = accumulate(v.begin(),v.end(), 0);
   cout << "sum = " << sum << endl;
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.6
chapter10.cpp
// #include <algorithm>
#include <iostream>
#include <vector>
// #include <numeric>
using namespace std;


int main()
{
   vector<int> v(10);
   fill_n(v.begin(), v.size(),0);
   vector<int>::iterator it = v.begin();
   while (it != v.end())
   {
    cout << *it << " ";
    it ++;
   }
   cout << endl;
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.8(来自deepseek)

back_inserter 不是算法,而是一个迭代器适配器。它创建了一个特殊的迭代器,当你向它写入时,它会调用容器的 push_back()。

责任分离原则

算法的责任:处理已存在的元素范围,不负责管理容器内存

迭代器的责任:提供元素访问接口,如何响应写入由迭代器决定

适配器:转换接口行为

容器的责任:管理内存和元素存储

复制代码
节练习 10.9
chapter10.cpp
#include <algorithm>
#include <iostream>
#include <vector>
// #include <numeric>
using namespace std;

void elimDups(vector<string> v) {
    vector<string>::iterator it = v.begin();
    cout << "the vector is :";
    while (it != v.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;

    sort(v.begin(), v.end());
    it = v.begin();
    cout << "sort: the vector is :";
    while (it != v.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;

    auto unique_end = unique(v.begin(), v.end());
    it = v.begin();
    cout << "unique: the vector is :";
    while (it != v.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;

    v.erase(unique_end,v.end());
    it = v.begin();
    cout << "erase: the vector is :";
    while (it != v.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;
     
}

int main()
{
   vector<string> vec = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
   elimDups(vec);
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.11
chapter10.cpp
#include <algorithm>
#include <iostream>
#include <vector>
// #include <numeric>
using namespace std;

bool isShorter(const string& s1, const string& s2) {
    return s1.size() < s2.size();
}

void elimDups(vector<string> &v) {
    vector<string>::iterator it = v.begin();
    cout << "the vector is :";
    while (it != v.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;

    sort(v.begin(), v.end());
    it = v.begin();
    cout << "sort: the vector is :";
    while (it != v.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;

    auto unique_end = unique(v.begin(), v.end());
    it = v.begin();
    cout << "unique: the vector is :";
    while (it != v.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;

    v.erase(unique_end,v.end());
    it = v.begin();
    cout << "erase: the vector is :";
    while (it != v.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;
     
}

int main()
{
   vector<string> vec = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
   elimDups(vec);
   vector<string>::iterator it = vec.begin();
    cout << "stable_sort: the vector is :";
    while (it != vec.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;

   stable_sort(vec.begin(), vec.end(), isShorter);
   it = vec.begin();
    cout << "stable_sort: the vector is :";
    while (it != vec.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;
   
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.12
chapter10.cpp
#include <algorithm>
#include <iostream>
#include <vector>
#include "Sales_data.h"
using namespace std;

bool compareIsbn(Sales_data sale1, Sales_data sale2) {
    return sale1.isbn().size() < sale2.isbn().size();
}

void elimDups(vector<string> &v) {
    vector<string>::iterator it = v.begin();
    cout << "the vector is :";
    while (it != v.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;

    sort(v.begin(), v.end());
    it = v.begin();
    cout << "sort: the vector is :";
    while (it != v.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;

    auto unique_end = unique(v.begin(), v.end());
    it = v.begin();
    cout << "unique: the vector is :";
    while (it != v.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;

    v.erase(unique_end,v.end());
    it = v.begin();
    cout << "erase: the vector is :";
    while (it != v.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;
     
}

int main()
{
   vector<string> vec = {"ISBN 978-7-121-15535-2", "ISBN 978-7-121-15555-2", "ISBN 878-7-121-15535-2", "ISBN 978-7-321-15535-2", "ISBN 978-7-121-19535-2", "ISBN 978-3-121-15535-2", "ISBN 976-7-121-15535-2", "ISBN 978-7-121-95535-2", "ISBN 978-7-121-15585-2", "ISBN 978-7-321-15535-2"};
   elimDups(vec);
   vector<string>::iterator it = vec.begin();
    cout << "stable_sort: the vector is :";
    while (it != vec.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;

   stable_sort(vec.begin(), vec.end(), compareIsbn);
   it = vec.begin();
    cout << "stable_sort: the vector is :";
    while (it != vec.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;
   
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.13
chapter10.cpp
#include <algorithm>
#include <iostream>
#include <vector>
#include "Sales_data.h"
using namespace std;

bool fun(const string& s) {
    return s.size() >= 5;
}

int main()
{
   vector<string> v = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
   auto end_partition = partition(v.begin(),v.end(),fun);
   v.erase(end_partition, v.end());
   vector<string>::iterator it  = v.begin();
    cout << "stable_sort: the vector is :";
    while (it != v.end())
    {
        cout << *it << " ";
        it ++;
    }
    cout << endl;
   
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

// 10.3节 biggest 方法
#include <algorithm>
#include <iostream>
#include <vector>
#include "Sales_data.h"
using namespace std;

void elemDups(vector<string> &words) {
    sort(words.begin(), words.end());
    auto end_unique = unique(words.begin(), words.end());
    words.erase(end_unique, words.end());
}

string make_plural(size_t ctr, const string &word, const string &ending) {
    return (ctr > 1) ? word + ending  : word;
}

void biggiest(vector<string> &words, vector<string>::size_type sz) {
    elemDups(words);
    stable_sort(words.begin(), words.end(), [](const string  &a, const string &b) {return a.size() < b.size();});
    auto wc = find_if(words.begin(), words.end(),[sz](const string &a) {
        return a.size() >= sz;
    });
    auto count = words.end() - wc;
    cout << count << " " << make_plural(count, "word","s") << "of length " << sz << " or longer" << endl;
    for_each(wc, words.end(), [](const string &s) {cout << s << " ";});
    cout << endl;
}
int main()
{
   vector<string> v = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
   biggiest(v, 5);
   return 0;
}

节练习 10.14
chapter10.cpp
#include <iostream>
using namespace std;

int main()
{
  auto sum = [] (int a, int b) {return (a + b);};
  cout << "sum = " << sum(18 , 8) << endl;
   
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.15
chapter10.cpp
#include <iostream>
using namespace std;

void fun(int a) {
    auto sum = [a] (int b) {return (a + b);};
    cout << "sum = " << sum(8) << endl;
}
int main()
{
    fun(28);
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.16
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void biggist(vector<string> vec, vector<string>::size_type sz) {
    auto fun = [sz] (string s) {return (s.size() >= sz);};
    sort(vec.begin(), vec.end(), [](string s1, string s2) {return (s1.size() < s2.size());});
    auto end_find = find_if(vec.begin(), vec.end(),[sz](string s){return (s.size() >= sz);});
    vec.erase(vec.begin(), end_find);
    vector<string>::iterator it = vec.begin();
    for_each(vec.begin(), vec.end(),[] (string s){
        cout << s << " ";
    });
    cout << endl;

}
int main()
{
    vector<string> v = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
    biggist(v,5);
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.17
chapter10.cpp
#include <algorithm>
#include <iostream>
#include <vector>
#include "Sales_data.h"
using namespace std;

void elimDups(vector<string> &v) {
    vector<string>::iterator it = v.begin();
    cout << "the vector is :";
    while (it != v.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;

    sort(v.begin(), v.end());
    it = v.begin();
    cout << "sort: the vector is :";
    while (it != v.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;

    auto unique_end = unique(v.begin(), v.end());
    it = v.begin();
    cout << "unique: the vector is :";
    while (it != v.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;

    v.erase(unique_end,v.end());
    it = v.begin();
    cout << "erase: the vector is :";
    while (it != v.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;
     
}

int main()
{
   vector<string> vec = {"ISBN 978-7-121-15535-2", "ISBN 978-7-121-15555-2", "ISBN 878-7-121-15535-2", "ISBN 978-7-321-15535-2", "ISBN 978-7-121-19535-2", "ISBN 978-3-121-15535-2", "ISBN 976-7-121-15535-2", "ISBN 978-7-121-95535-2", "ISBN 978-7-121-15585-2", "ISBN 978-7-321-15535-2"};
   elimDups(vec);
   vector<string>::iterator it = vec.begin();
    cout << "stable_sort: the vector is :";
    while (it != vec.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;

   stable_sort(vec.begin(), vec.end(), [] (Sales_data sale1, Sales_data sale2){return sale1.isbn().size() < sale2.isbn().size();});
   it = vec.begin();
    cout << "stable_sort: the vector is :";
    while (it != vec.end())
    {
        cout << *it << endl;
        it ++;
    }
    cout << endl;
   
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.18
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void biggist(vector<string> vec, vector<string>::size_type sz) {
    auto fun = [sz] (string s) {return (s.size() >= sz);};
    sort(vec.begin(), vec.end(), [](string s1, string s2) {return (s1.size() < s2.size());});
    auto end_partition = partition(vec.begin(), vec.end(),[sz](string s){return (s.size() >= sz);});
   
    vec.erase(end_partition, vec.end());
    vector<string>::iterator it = vec.begin();
    for_each(vec.begin(), vec.end(),[] (string s){
        cout << s << " ";
    });
    cout << endl;

}
int main()
{
    vector<string> v = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
    biggist(v,5);
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.19
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void biggist(vector<string> vec, vector<string>::size_type sz) {
    auto fun = [sz] (string s) {return (s.size() >= sz);};
    sort(vec.begin(), vec.end(), [](string s1, string s2) {return (s1.size() < s2.size());});
    auto end_partition = stable_partition(vec.begin(), vec.end(),[sz](string s){return (s.size() >= sz);});
   
    vec.erase(end_partition, vec.end());
    vector<string>::iterator it = vec.begin();
    for_each(vec.begin(), vec.end(),[] (string s){
        cout << s << " ";
    });
    cout << endl;

}
int main()
{
    vector<string> v = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
    biggist(v,5);
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.20
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void biggist(vector<string> vec, vector<string>::size_type sz) {
    auto fun = [sz] (string s) {return (s.size() >= sz);};
    sort(vec.begin(), vec.end(), [](string s1, string s2) {return (s1.size() < s2.size());});
    auto count = count_if(vec.begin(), vec.end(),[sz](string s){return (s.size() >= sz);});
    cout << "count = " << count << endl;

}
int main()
{
    vector<string> v = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
    biggist(v,6);
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.21
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void fun() {
    int a = 28;
    auto f = [&a]() -> bool {
        if (a > 0) {
            a --;
            return false;
        }
        return true;
    };
    while (!f())
    {
        cout << "a = " << a << endl;
    }

    cout << "a1 = " << a << endl;
    
}
int main()
{
    vector<string> v = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
    fun();
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.22
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace placeholders;

bool func1(string s1, string s2) {
    return (s1.size() < s2.size());
}

bool func2(string s,vector<string>::size_type sz) {
    return (s.size() < sz);
}

int main()
{
    vector<string> v = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
    vector<string>::size_type sz = 6;
    sort(v.begin(), v.end(), func1);
    auto count = count_if(v.begin(), v.end(), bind(func2,_1,sz));
    cout << "fun count = " << count << endl;
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.24
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace placeholders;

bool check_size(int a,string s) {
    return (a > s.size());
}

int main()
{
    vector<int> v = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
    string s("123456");
    auto it = find_if(v.begin(), v.end(),bind(check_size,_1,s));
    cout << "data is " << *it << endl;
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.25
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
using namespace std;
using namespace placeholders;

bool check_size(vector<string>:: size_type sz, string s) {
    return (s.size() >= sz);
}

void biggist(vector<string> vec, vector<string>::size_type sz) {
    sort(vec.begin(), vec.end(), [](string s1, string s2) {return (s1.size() < s2.size());});
    auto end_partition = partition(vec.begin(), vec.end(),bind(check_size, sz, _1));
   
    vec.erase(end_partition, vec.end());
    vector<string>::iterator it = vec.begin();
    for_each(vec.begin(), vec.end(),[] (string s){
        cout << s << " ";
    });
    cout << endl;

}
int main()
{
    vector<string> v = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
    biggist(v,5);
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.27
chapter10.cpp
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
#include <iterator>
// #include <functional>
using namespace std;

int main()
{
    vector<string> v = {"the", "quick", "red", "fox", "jumps", "over", "the", "slow", "red", "turtle"};
    list<string> v1;
    sort(v.begin(), v.end());
    unique_copy(v.begin(),v.end(),back_inserter(v1));
    list<string>::iterator it = v1.begin();
    while (it != v1.end())
    {
        cout << *it << " " << endl;
        it ++;
    }
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.28
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <list>
using namespace std;

int main()
{
    vector<int> v = {1,2,3,4,5,6,7,8,9};
    vector<int> v1;
    list<int> l2;
    vector<int> v3;

    vector<int>::iterator it = v.begin();

    vector<int>::iterator it1 = v1.begin();
    copy(v.begin(), v.end(), back_inserter(v1));
    it1 = v1.begin();
    cout << "back_inserter : ";
    while(it1 != v1.end()) {
        cout << *it1 << " ";
        it1 ++;
    }
    cout << endl;
    
    copy(v.begin(), v.end(), front_inserter(l2));
    cout << "front_inserter : ";
    for (int num : l2) {
        cout << num << " ";
    }
    cout << endl;

    auto it3 = inserter(v3,v3.begin());
    copy(v.begin(), v.end(), it3);
    cout << "inserter : ";
    for_each(v3.begin(), v3.end(),[](int a) {cout << a << " ";});
    cout << endl; 

    
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.29
chapter10.cpp
#include <iostream>
#include <vector>
// #include <algorithm>
#include <iterator>
#include <fstream>
using namespace std;

int main()
{
    vector<string> v;
    ifstream is("test.txt");
    istream_iterator<string> string_it(is);
    istream_iterator<string> eol;
    while (string_it != eol)
    {
        v.push_back(*string_it);
        string_it ++;
    }

    for (string s : v) {
        cout << s <<endl;
    }
    cout << endl;
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.30
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <fstream>
using namespace std;

int main()
{
    ostream_iterator<int> out_it(cout, " ");
    istream_iterator<int> int_it(cin);
    istream_iterator<int> eof;
    vector<int> v(int_it, eof);
    sort(v.begin(), v.end());
    copy(v.begin(), v.end(), out_it);
    cout << endl;
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp

节练习 10.31
chapter10.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <fstream>
using namespace std;

int main()
{
    ostream_iterator<int> out_it(cout, " ");
    istream_iterator<int> int_it(cin);
    istream_iterator<int> eof;
    vector<int> v(int_it, eof);
    sort(v.begin(), v.end());
    unique_copy(v.begin(), v.end(), out_it);
    cout << endl;
   return 0;
}
编译: g++ -std=c++11 -o chapter10  chapter10.cpp
相关推荐
零雲2 小时前
java面试:@Resource和@Autowired的区别
java·开发语言·面试
liu****2 小时前
01_NumPy讲义
开发语言·python·numpy·python高级语法
一路往蓝-Anbo2 小时前
C语言从句柄到对象 (一) —— 全局变量的噩梦与“多实例”的救赎
c语言·开发语言·stm32·单片机·嵌入式硬件·物联网
低频电磁之道2 小时前
C++中类的this指针
开发语言·c++
世转神风-3 小时前
qt-通信协议基础-double转成QbyteArray-小端系统
开发语言·qt
web3.08889993 小时前
小红书笔记评论API接口详情展示
开发语言·笔记·python
手抄二进制3 小时前
使用Anaconda创建python环境并链接到Jupyter
开发语言·python·jupyter
水饺编程3 小时前
Visual Studio 软件操作:添加附加依赖项
c语言·c++·windows·visual studio
古城小栈3 小时前
go-zero 从入门到实战 全指南(包的)
开发语言·后端·golang