C++Primer第九章课后习题程序题源程序

9.4

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

bool search_vec(vector<int>::iterator beg, vector<int>::iterator end, int val)
{
    for (; beg != end; beg++)
        if (*beg == val)
            return true;
    return false;
}

int main()
{
    vector<int> ilist = { 1, 2, 3, 4, 5, 6, 7 };
    cout << search_vec(ilist.begin(), ilist.end(), 3) << endl;
    cout << search_vec(ilist.begin(), ilist.end(), 8) << endl;

    return 0;
}

9.5

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

 vector<int>::iterator search_vec(vector<int>::iterator beg, vector<int>::iterator end, int val)
{
    for (; beg != end; beg++)
        if (*beg == val)
            return beg;
    return end;
}

int main()
{
    vector<int> ilist = { 1, 2, 3, 4, 5, 6, 7 };
    cout << search_vec(ilist.begin(), ilist.end(), 3)-ilist.begin() << endl;
    cout << search_vec(ilist.begin(), ilist.end(), 8)-ilist.begin()<< endl;

    return 0;
}

9.13

#include<iostream>
#include<vector>
#include<list>

using namespace std;

int main()
{
    list<int> ilist = { 1, 2, 3, 4, 5, 6, 7 };
    vector<int> ivec = { 7, 6, 5, 4, 3, 2, 1 };
    //vector<double>ivec(ilist);
    vector<double>dvec(ilist.begin(), ilist.end());
    //vector<double>dvec1(ivec);
    vector<double> dvec1(ivec.begin(), ivec.end());

    cout << dvec.capacity() << " " << dvec.size() << " "
        << dvec[0] << " " << dvec[dvec.size() - 1] << endl;
    cout << dvec.capacity() << " " << dvec1.size() << " "
        << dvec1[0] << " " << dvec1[dvec1.size() - 1] << endl;

    return 0;
}

9.14

#include<iostream>
#include<vector>
#include<list>
#include<string>


using namespace std;

int main()
{
    list<char *>slist = { "hello", "world", "!" };
    vector<string> svec;

    //svec=slist;
    svec.assign(slist.begin(), slist.end());

    cout << svec.capacity() << " "<< svec.size()<< " "
        << svec[0] << " " << svec[svec.size() - 1] << endl;

    return 0;

}

9.15

#include<iostream>
#include<vector>

using namespace std;

int main()
{
    vector<int> ivec = { 1, 2, 3, 4, 5, 6, 7 };
    vector<int> ivec1 = { 1, 2, 3, 4, 5, 6, 7 };
    vector<int> ivec2 = { 1, 2, 3, 4, 5 };
    vector<int> ivec3 = { 1, 2, 3, 4, 5, 6, 8 };
    vector<int> ivec4 = { 1, 2, 3, 4, 5, 7, 6 };

    cout << (ivec == ivec1) << endl;
    cout << (ivec == ivec2) << endl;
    cout << (ivec == ivec3) << endl;
    cout << (ivec == ivec4) << endl;
    ivec1.push_back(8);
    ivec1.pop_back();
    cout << ivec1.capacity() << " " << ivec1.size() << endl;
    cout << (ivec == ivec1) << endl;

    return 0;
}

9.16

#include<iostream>
#include<vector>
#include<list>


using namespace std;

bool l_v_equal(vector<int> &ivec, list<int> &ilist)
{
    if (ilist.size() != ivec.size())
        return false;

    auto lb = ilist.cbegin();
    auto le = ilist.cend();
    auto vb = ivec.cbegin();
    for (; lb != le; lb++, vb++)
        if (*lb != *vb)
            return false;
    return true;
}
int main()
{
    vector<int> ivec = { 1, 2, 3, 4, 5, 6, 7 };
    list<int> ilist = { 1, 2, 3, 4, 5, 6, 7 };
    list<int> ilist1= { 1, 2, 3, 4, 5 };
    list<int> ilist2 = { 1, 2, 3, 4, 5, 6, 8 };
    list<int> ilist3 = { 1, 2, 3, 4, 5, 7, 6 };

    cout << l_v_equal(ivec, ilist) << endl;
    cout << l_v_equal(ivec, ilist1) << endl;
    cout << l_v_equal(ivec, ilist2) << endl;
    cout << l_v_equal(ivec, ilist3) << endl;

    return 0;
}

9.19

#include<iostream>
#include<deque>
#include<string>

using namespace std;

int main()
{
    deque<string> sd;

    string word;
    while (cin >> word)
        sd.push_back(word);
    for (auto si = sd.cbegin(); si != sd.cend(); si++)
        cout << *si << endl;
    return 0;

}

9.20

#include<iostream>
#include<deque>
#include<string>
#include<list>

using namespace std;

int main()
{
    list<int> ilist = { 1, 2, 3, 4, 5, 6, 7, 8 };
    deque<int> odd_d, even_d;

    for (auto iter = ilist.cbegin(); iter != ilist.cend(); iter++)
        if (*iter & 1)
            odd_d.push_back(*iter);
        else
            even_d.push_back(*iter);

    cout << "奇数值有:";
    for (auto iter = odd_d.cbegin(); iter != odd_d.cend(); iter++)
        cout << *iter << " ";
    cout << endl;

    cout << "偶数值有";
    for (auto iter = even_d.cbegin(); iter != even_d.cend(); iter++)
        cout << *iter << " ";

    return 0;


}

9.21

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

using namespace std;

int main()
{
    vector<string> svec;

    string word;
    auto iter = svec.begin();
    while (cin >> word)
        iter = svec.insert(iter, word);

    for (auto iter = svec.cbegin(); iter != svec.cend(); iter++)
        cout << *iter << endl;

    return 0;
}

9.22


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

using namespace std;

int main()
{
    vector<int> iv = { 1, 1, 2, 1 };
    int some_val = 1;

    vector<int>::iterator iter = iv.begin();
    int org_size = iv.size(), new_ele = 0;

    while (iter != (iv.begin() + org_size / 2 + new_ele))
        if (*iter == some_val)
        {
        iter = iv.insert(iter, 2 * some_val);
        new_ele++;
        iter++;
        iter++;
        }
        else
            iter++;

    for (iter = iv.begin(); iter != iv.end(); iter++)
        cout << *iter << endl;

    return 0;
}
#include<iostream>
#include<vector>
#include<string>

using namespace std;

int main()
{
    vector<int> iv = { 1, 1, 2, 1 };
    int some_val = 1;

    vector<int>::iterator iter = iv.begin();
    int org_size = iv.size(), i = 0;

    while (i <= org_size / 2)
    {
        if (*iter == some_val)
        {
            iter = iv.insert(iter, 2 * some_val);
            iter++;
            iter++;
        }
        else
            iter++;
        i++;
    }

    for (iter = iv.begin(); iter != iv.end(); iter++)
        cout << *iter << endl;

    return 0;
}

9.24

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

using namespace std;

int main()
{
    vector<int> iv(7);

    cout << iv.at(0) << endl;
    cout << iv[0] << endl;
    cout << iv.front() << endl;
    cout << *(iv.begin()) << endl;

    return 0;
}

9.26

#include<iostream>
#include<vector>
#include<string>
#include<list>

using namespace std;

int main()
{

    int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 55, 89 };
    vector<int> iv;
    list<int> il;

    iv.assign(ia, ia + 11);
    il.assign(ia, ia + 11);

    vector<int>::iterator iiv = iv.begin();
    while (iiv != iv.end())
        if (!(*iiv & 1))
            iiv = iv.erase(iiv);
        else
            iiv++;
    list<int>::iterator iil = il.begin();
    while (iil != il.end())
        if (*iil & 1)
            iil = il.erase(iil);
        else
            iil++;

    for (iiv = iv.begin(); iiv != iv.end(); iiv++)
        cout << *iiv << " ";
    cout << endl;
    for (iil = il.begin(); iil != il.end(); iil++)
        cout << *iil << " ";
    cout << endl;

    return 0;
}

9.44

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

using namespace std;

void replace_string(string &s, const string &oldVal, const string &newVal)
{
    int p = 0;
    while ((p = s.find(oldVal, p))!= string::npos)
    {
        s.replace(p, oldVal.size(), newVal);
        p += newVal.size();
    }
}

int main()
{
    string s = "tho thru tho!";
    replace_string(s, "thu", "through");
    cout << s << endl;

    replace_string(s, "tho", "though");
    cout << s << endl;

    replace_string(s, "through", "");
    cout << s << endl;

    return 0;
}

9.45

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

using namespace std;

void name_string(string &name, const string &prefix, const string &suffix)
{
    name.insert(name.begin(), 1, ' ');
    name.insert(name.begin(), prefix.begin(), prefix.end());
    name.append(" ");
    name.append(suffix.begin(), suffix.end());
}
int main()
{
    string s = "James Bond";
    name_string(s, "Mr.", "II");
    cout << s << endl;

    s = "M";
    name_string(s, "Mrs.", "III");
    cout << s << endl;

    return 0;
}

9.46

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

using namespace std;

void name_string(string &name, const string &prefix, const string &suffix)
{
    name.insert(0, " ");
    name.insert(0, prefix);
    name.insert(name.size(), " ");
    name.insert(name.size(), suffix);
}

int main()
{
    string s = "James Bond";
    name_string(s, "Mr.", "II");
    cout << s << endl;

    s = "M";
    name_string(s, "Mrs.", "III");
    cout << s << endl;

    return 0;
}

9.47

#include<iostream>
#include<string>

using namespace std;

void find_char(string &s, const string &chars)
{
    cout << "在" << s << "中查找" << chars << "中字符" << endl;
    string::size_type pos = 0;
    while ((pos = s.find_first_of(chars, pos)) != string::npos)
    {
        cout << "pos: " << ",char: " << s[pos] << endl;
        pos++;
    }
}

int main()
{
    string s = "ab2c3d7R4E6";
    cout << "查找所有数字" << endl;
    find_char(s, "0123456789");
    cout << endl << "查找所有的字母" << endl;
    find_char(s, "abcdefghijklmnopqrstuvwxyz"\
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    return 0;
}
#include<iostream>
#include<string>

using namespace std;

void find_not_char(string &s, const string &chars)
{
    cout << "在" << s << "中查找不在" << chars << "中字符" << endl;
    string::size_type pos = 0;
    while ((pos = s.find_first_not_of(chars, pos)) != string::npos)
    {
        cout << "pos: " << ",char: " << s[pos] << endl;
        pos++;
    }
}

int main()
{
    string s = "ab2c3d7R4E6";
    cout << endl << "查找所有的字母" << endl;
    find_not_char(s, "abcdefghijklmnopqrstuvwxyz"\
        "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
    cout <<endl<< "查找所有数字" << endl;
    find_not_char(s, "0123456789");

    return 0;
}

9.49

#include<iostream>
#include<fstream>
#include<string>

using namespace std;

void find_longest_word(ifstream &in)
{
    string s, longest_word;
    int max_length = 0;

    while (in >> s)
    {
        if (s.find_first_of("bdfghjklpqty") != string::npos)
            continue;
        cout << s << " ";
        if (max_length < s.size())
        {
            max_length = s.size();
            longest_word = s;
        }
    }
    cout << endl << "最长字符串:" << longest_word << endl;
}

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cerr << "无法打开输入文件" << endl;
        return -1;
    }

    find_longest_word(in);

    return 0;
}

9.50

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

using namespace std;

int main()
{
    vector<string> vs = { "123", "+456", "-789" };
    int sum = 0;

    for (auto iter = vs.begin(); iter != vs.end(); iter++)
        sum += stoi(*iter);

    cout << "和:" << sum << endl;
    return 0;
}
#include<iostream>
#include<string>
#include<vector>

using namespace std;

int main()
{
    vector<string> vs = {"12.3","-4.56","7.8e-2"};
    float sum = 0;

    for (auto iter = vs.begin(); iter != vs.end(); iter++)
        sum += stof(*iter);

    cout << "和:" << sum << endl;

    return 0;

}

9.51

#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED

#include<iostream>
#include<string>
#include<stdexcept>

using namespace std;

class date{
public:
    friend ostream& operator<<(ostream&, const date&);

    date() = default;
    date(string &ds);

    unsigned y() const{ return year; }
    unsigned m() const{ return month; }

    unsigned d() const{ return day; }

private :
    unsigned year, month, day;
};

const string month_name[] = { "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" };

const string month_abbr[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sept", "Oct", "Nov", "Dec" };

const int days[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

inline int get_month(string &ds, int &end_of_month)
{
    int i, j;
    for (i = 0; i < 12; i++)
    {
        for (j = 0; j < month_abbr[i].size(); j++)
            if (ds[j] != month_abbr[i][j])
                break;
        if (j == month_abbr[i].size())
            break;
    }
    if (i == 12)
        throw invalid_argument("不是合法月份名");

    if (ds[j] == ' ')
    {
        end_of_month = j + 1;
        return i + 1;
    }

    for (; j < month_name[i].size(); j++)
        if (ds[j] != month_name[i][j])
            break;

    if (j == month_name[i].size() && ds[j] == ' ')
    {
        end_of_month = j + 1;
        return i + 1;
    }

    throw invalid_argument("不是合法月份名");
}

inline int get_day(string &ds, int month, int &p)
{
    size_t q;
    int day =
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值