C++Primer第五版 10.3.1节练习

本文介绍了使用C++实现的几种排序与容器管理方法,包括使用stable_sort与自定义比较函数isShorter进行字符串排序、利用compareIsbn函数对Sales_data对象进行排序,以及采用partition算法按条件对容器进行划分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

练习10.11:编写程序,使用stable_sort和isShorter将传递给你的elimDups版本的vector排序。打印vector的内容,验证你的程序的正确性。
答:见练习10.11.cpp

练习10.12:编写名为compareIsbn的函数,比较两个Sales_data对象的isbn()。使用这个函数排序一个保存Sales_data对象的vector。
答: 见练习10.12.cpp

练习10.13:标准库定义了名为partition的算法,它接受一个谓词,对容器内容进行划分,使得谓词为true的值会排在容器的前半部分,而使谓词为false的值会排在后半部分。算法返回一个迭代器,指向最后一个使谓词为ture的元素之后的位置。编写函数,接受一个string,返回一个bool值,指出string是否有5个或更多字符。使用次函数划分words。打印出长度大于等于5的元素。
答:见练习10.13.cpp

练习10.11

/*
*练习10.11
*2015/8/12 
*问题描述:练习10.11:编写程序,使用stable_sort和isShorter将传递给你的elimDups版本的vector排序。打印vector的内容,验证你的程序的正确性。
*说明:字符串去重复排序 
*作者:Nick Feng
* 邮箱:nickgreen23@163.com 
*/

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

using namespace std;

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


void elimDups(vector<string> &words)
{
    sort(words.begin(), words.end());

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

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


int main()
{
    string word;
    vector<string> words;
    while(cin >> word)
        words.push_back(word);
    elimDups(words);

    stable_sort(words.begin(), words.end(), isShorter);
    for(const auto &s : words)
        cout << s << " ";
        cout << endl;
    return 0;
}

练习10.12

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


#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
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(), 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(), CompareISBN);
    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.13

/*
*练习10.13
*题目描述:练习10.13:标准库定义了名为partition的算法,它接受一个谓词,对容器内容进行划分,使得谓词为true的值会排在容器的前半部分,而使谓词为false的值会排在后半部分。算法返回一个迭代器,指向最后一个使谓词为ture的元素之后的位置。编写函数,接受一个string,返回一个bool值,指出string是否有5个或更多字符。使用次函数划分words。打印出长度大于等于5的元素。 
*说明:partition的使用
*作者:Nick Feng
*邮箱:mail.163.com 
*/

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

using namespace std;

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

void find_str(vector<string> &vec)
{
    auto partition_end = partition(vec.begin(), vec.end(),compare_str);
    vec.erase(partition_end,vec.end());
}

int main()
{
    string word;
    vector<string> vec;
    cout << "please input strings..." << endl;
    while(cin >> word)
        vec.push_back(word);
    find_str(vec);
    for(auto it =  vec.begin(); it != vec.end(); ++it)
        cout << *it << " ";
        cout << endl;

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值