您的位置:首页 > 编程语言 > C语言/C++

C++Primer第五版 10.3.2节练习

2015-09-18 07:49 459 查看
练习10.14:编写一个lambda,接受两个int,返回它们的和。

练习10.15:编写一个lambda,捕获它所在函数的int,并接受一个int函数。lambda应该返回捕获的int和int参数的和。

练习10.16:使用lambda编写你自己版本的biggies。

练习10.17:重写10.3.1节练习10.12(第345页)的程序,在对sort的调用中使用lambda来代替函数compareIsbn.

练习10.18:重写biggies,用partition代替find_if。我们在10.3.1节练习10.13(第345页)中介绍了partition算法。

练习10.19:用stable_partition重写前一题的程序,与stable_sort类似,在划分后的序列中维持原有元素的顺序。

答:见练习10.14.cpp - 练习10.19.cpp

练习10.14

/*
*练习10.14 
*2015/8/18 
*问题描述:练习10.14:编写一个lambda,接受两个int,返回它们的和。 
*说明:lambda的入门例子 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

#include <iostream>

using namespace std;

int main()
{
    auto f = [](int &a, int &b) { return a + b; }; //lambda表达式 
     int a, b;
    cout << "please input two numbers..." << endl;
    while(cin >> a >> b)
    {
        cout << "sum of a and b is: " << f(a,b) << endl;
    }
    return 0;
 }


练习10.15

/*
*练习10.15 
*2015/8/18 
*问题描述:练习10.15:编写一个lambda,捕获它所在函数的int,并接受一个int函数。Lambda应该返回捕获的int和int参数的和。 
*说明:lambda的入门例子 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

#include <iostream>

using namespace std;

int sum(const int &a, const int &b)
{
    auto f = [a](int b){return a + b;};
    return f(b);
}

int main()
{
   int a = 0, b = 0;
   cout << "please input two numbers..." << endl;
   while(cin >> a >> b)
   {
    cout << "sum of a and b is: " << sum(a,b) << endl;
   }
   return 0;
 }


练习10.16

/*
*练习10.16 
*2015/8/18 
*问题描述:练习10.16:使用lambda编写你自己版本的biggies。 
*说明:复习前面elimDups的写法,make_plural参考了201页写法,书上说得很清楚 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

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

using namespace std;

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

void elimDups(vector<string> words)
{
    stable_sort(words.begin(), words.end(),isShorter);
    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 biggies(vector<string> &words, vector<string>::size_type sz)
{
    elimDups(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> words = {"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
    vector<string>::size_type sz = 5;
    biggies(words,sz);
    return 0;
}


练习10.17

/*
*练习10.17 
*2015/8/18 
*问题描述:练习10.17:重写10.3.1节练习10.12(第345页)的程序,在对sort的调用中使用lambda来代替函数compareIsbn. 
*说明:在练习10.12的基础上改动 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

/*
*练习10.12 
*2015/8/14 
*题目描述:练习10.12:编写名为compareIsbn的函数,比较两个Sales_data对象的isbn()。使用这个函数排序一个保存Sales_data对象的vector。
*说明:其实这条道题并不像描述的那么容易,如果真正去写了,你会发现有好多坑再等着你.题目中会有两个主要的坑再等着你 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

struct Sales_data{

    string bookNo;
    unsigned units_sold = 0;
    double revenue = 0.0;

    Sales_data() = default;
    string isbn() const {return bookNo;}

    bool operator==(const Sales_data& rhs) //这是第二个坑, 
    {                                      //Sales_data里没有 == 这个运算,试问 两个结构体,如何判断相等?,这个 == 会直接影响到 unique  
        if(bookNo == rhs.bookNo)
            return true;
        else 
            return false;
    }

}; 

bool CompareISBN(const Sales_data &s1, const Sales_data &s2) //第一个坑,如果前后不加const,你会很崩溃的 
{
    return s1.isbn() < s2.isbn();
}

void elimDups(vector<Sales_data> &words)
{

    stable_sort(words.begin(), words.end(), [](const Sales_data &s1, const Sales_data &s2){return s1.isbn() < s2.isbn();});//修改的地方,lambda代替CompareIsbn函数 

    for(auto i = 0; i != words.size(); ++i)
        cout << words[i].bookNo << " ";
        cout << endl;   

    auto end_unique = unique(words.begin(), words.end());

    cout << "After unique..." << endl; 
    for(auto i = 0; i != words.size(); ++i)
        cout << words[i].bookNo << " ";
        cout << endl;   
    words.erase(end_unique,words.end());

    cout << "After erase..." << endl;

    stable_sort(words.begin(), words.end(), [](const Sales_data &s1, const Sales_data &s2){return s1.isbn() < s2.isbn();});
    for(auto i = 0; i != words.size(); ++i)
        cout << words[i].bookNo << " ";
        cout << endl;

}

int main()
{
    Sales_data a,b,c,d;
    a.bookNo = "hello";
    a.units_sold = 10;
    a.revenue = 3;

    b.bookNo = "good";
    b.units_sold = 10;
    b.revenue = 3;

    c.bookNo = "good";
    c.units_sold = 10;
    c.revenue = 2;

    d.bookNo = "bad";
    d.units_sold = 5;
    d.revenue = 2;

    vector<Sales_data> vec;
    vec.push_back(a);
    vec.push_back(b);
    vec.push_back(c);
    vec.push_back(d);

   elimDups(vec);

    return 0;
}


练习10.18

/*
*练习10.18 
*2015/8/18 
*问题描述:练习10.18:重写biggies,用partition代替find_if。我们在10.3.1节练习10.13(第345页)中介绍了partition算法。
*说明:在练习10.16的基础上改动 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

/*
*练习10.16 
*2015/8/18 
*问题描述:练习10.16:使用lambda编写你自己版本的biggies。 
*说明:复习前面elimDups的写法,make_plural参考了201页写法,书上说得很清楚 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

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

using namespace std;

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

void elimDups(vector<string> words)
{
    stable_sort(words.begin(), words.end(),isShorter);
    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 biggies(vector<string> &words, vector<string>::size_type sz)
{
    elimDups(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 wc = partition(words.begin(), words.end(),[sz](const string &a){return a.size() < sz;});//使用partition代替find_if函数 
    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> words = {"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
    vector<string>::size_type sz = 5;
    biggies(words,sz);
    return 0;
}


练习10.19

/*
*练习10.19
*2015/8/18 
*问题描述:练习10.19:用stable_partition重写前一题的程序,与stable_sort类似,在划分后的序列中维持原有元素的顺序。
*说明:在练习10.18的基础上改动,与原来程序不同的地方,stable_partition后,单词顺序和之前一致 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/
/*
*练习10.18 
*2015/8/18 
*问题描述:练习10.18:重写biggies,用partition代替find_if。我们在10.3.1节练习10.13(第345页)中介绍了partition算法。
*说明:在练习10.16的基础上改动 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

/*
*练习10.16 
*2015/8/18 
*问题描述:练习10.16:使用lambda编写你自己版本的biggies。 
*说明:复习前面elimDups的写法,make_plural参考了201页写法,书上说得很清楚 
*作者:Nick Feng 
*邮箱:nickgreen23@163.com 
*/

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

using namespace std;

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

void elimDups(vector<string> words)
{
    stable_sort(words.begin(), words.end(),isShorter);
    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 biggies(vector<string> &words, vector<string>::size_type sz)
{
    elimDups(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 wc = partition(words.begin(), words.end(),[sz](const string &a){return a.size() < sz;});
    //使用partition代替find_if函数 
    auto wc = stable_partition(words.begin(), words.end(),[sz](const string &a){return a.size() < sz;});
    //使用stable_partition代替partition 
    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> words = {"the","quick","red","fox","jumps","over","the","slow","red","turtle"};
    vector<string>::size_type sz = 5;
    biggies(words,sz);
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: