程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> C++Primer(第10章課後程序題源代碼)

C++Primer(第10章課後程序題源代碼)

編輯:關於C++

10.1

#include
#include
#include
#include

using namespace std;

int main(int argc,char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    vector vi;
    int val;
    while (in >> val)
    {
        vi.push_back(val);
    }

    cout << "請輸入要搜索的整數:";
    cin >> val;

    cout << "序列中包含" << count(vi.begin(), vi.end(), val) << "個" << val;

    return 0;
}
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>

using namespace std;

int main(int argc,char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    vector<int> vi;
    int val;
    while (in >> val)
    {
        vi.push_back(val);
    }

    cout << "請輸入要搜索的整數:";
    cin >> val;

    int c = 0;
    for (auto iter = vi.begin(); iter != vi.end(); iter++)
        if (*iter == val)
            c++;
    cout << "序列中包含" <<c pre="" return="">

10.2

#include
#include
#include
#include
#include
#include

using namespace std;

int main(int argc,char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    list ls;
    string word;
    while (in >> word)
        ls.push_back(word);

    cout << "請輸入要搜索的字符串:";
    cin >> word;

    cout << "序列中包含" << count(ls.begin(), ls.end(), word) << "個" << word;

    return 0;
}

10.3

#include
#include
#include
#include
#include
#include
#include
using namespace std;

int main(int argc,char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    vector vi;
    int val;
    while (in >> val)
        vi.push_back(val);

    cout << "序列中整數之和為" << accumulate(vi.begin(), vi.end(), 0) << endl;

    return 0;
}

10.4

#include
#include
#include
#include
#include
#include
#include
using namespace std;

int main(int argc,char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    vector vd;
    double val;
    while (in >> val)
        vd.push_back(val);

    cout << "序列中浮點數之和為" << accumulate(vd.begin(), vd.end(),0.0) << endl;

    return 0;
}

10.5

#pragma warning(disable:4996)
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    char *p[] = { "Hello", "World", "!" };
    char *q[] = { strdup(p[0]), strdup(p[1]), strdup(p[2]) };
    char *r[] = { p[0], p[1], p[2] };
    cout << equal(begin(p), end(p), q) << endl;
    cout << equal(begin(p), end(p), r) << endl;
    return 0;
}

10.6

#pragma warning(disable:4996)
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    vector vi;
    int val;
    while (in >> val)
    {
        vi.push_back(val);
        cout << val << " ";
    }
    cout << endl;

    fill_n(vi.begin(), vi.size(), 0);
    for (auto iter = vi.begin(); iter != vi.end(); iter++)
        cout << *iter << " ";

    return 0;
}

10.7

#pragma warning(disable:4996)
#include
#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    list lst;
    vector vec;
    int val;
    while (in >> val)
    {
        lst.push_back(val);
    }
    copy(lst.begin(), lst.end(), back_inserter(vec));

    cout << equal(lst.begin(), lst.end(), vec.begin()) << endl;

    for (auto iter = vec.begin(); iter != vec.end(); iter++)
        cout << *iter << " ";
    cout << endl;

    return 0;
}
#pragma warning(disable:4996)
#include
#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    vector vec;
    vec.reserve(10);
    fill_n(back_inserter(vec), 10, 0);

    for (auto iter = vec.begin(); iter != vec.end(); iter++)
        cout << *iter << " ";
    cout << endl;

    return 0;
}

10.9

#pragma warning(disable:4996)
#include
#include
#include
#include
#include
#include
#include

using namespace std;

inline void output_words(vector &words)
{
    for (auto iter = words.begin(); iter != words.end(); iter++)
        cout << *iter << " ";
    cout << endl;
}

void elimDups(vector &words)
{
    output_words(words);

    sort(words.begin(), words.end());
    output_words(words);

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

    words.erase(end_unique, words.end());
    output_words(words);
}

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    vector words;
    string word;
    while (in >> word)
    {
        words.push_back(word);
    }

    elimDups(words);
    return 0;
}

10.11

#pragma warning(disable:4996)
#include
#include
#include
#include
#include
#include
#include

using namespace std;

inline void output_words(vector &words)
{
    for (auto iter = words.begin(); iter != words.end(); iter++)
        cout << *iter << " ";
    cout << endl;
}

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

void elimDups(vector &words)
{
    output_words(words);

    sort(words.begin(), words.end());
    output_words(words);

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

    words.erase(end_unique, words.end());
    output_words(words);

    stable_sort(words.begin(),words.end(), isShorter);
    output_words(words);
}

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    vector words;
    string word;
    while (in >> word)
    {
        words.push_back(word);
    }

    elimDups(words);
    return 0;
}

10.12 略 10.13

#pragma warning(disable:4996)
#include
#include
#include
#include
#include
#include
#include

using namespace std;

inline void output_words(vector::iterator beg,vector::iterator end)
{
    for (auto iter = beg;iter!=end; iter++)
        cout << *iter << " ";
    cout << endl;
}

bool five_or_more(const string &s1)
{
    return s1.size() >= 5;
}

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    vector words;
    string word;
    while (in >> word)
        words.push_back(word);
    output_words(words.begin(), words.end());

    auto iter = partition(words.begin(), words.end(), five_or_more);
    output_words(words.begin(), iter);

    return 0;
}

10.14

#include

using namespace std;

int main(int argc,char *argv[])
{
    auto sum = [](int a, int b){return a + b; };

    cout << sum(1, 1) << endl;

    return 0;
}

10.15

#include

using namespace std;

void add(int a)
{
    auto sum = [a](int b){return a + b; };

    cout << sum(1) << endl;

}
int main(int argc,char *argv[])
{
    add(4);
    add(5);

    return 0;
}

10.21

#include
#include

using namespace std;

void mutable_lambda(void)
{
    int i = 5;
    auto f = [i]()mutable->bool{if (i > 0){ i--; return false; } else return  true; };

    for (int j = 0; j < 6; j++)
        cout << f() << " ";
    cout << endl;
}

int main(int argc, char *argv[])
{
    mutable_lambda();

    return 0;
}

10.22

#include
#include
#include
#include
#include
#include

using namespace std;
using namespace std::placeholders;


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

inline void output_words(vector &words)
{
    for (auto iter = words.begin(); iter != words.end(); iter++)
        cout << *iter << " ";
    cout << endl;
}

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

void biggies(vector&words, vector::size_type sz)
{
    output_words(words);
    auto bc = count_if(words.begin(), words.end(), bind(check_size, _1, sz));
    cout << bc << " " << make_plural(bc, "word", "s") << " of length " << sz << " or longer" << endl;
}

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗" << endl;
        exit(1);
    }

    vector words;
    string word;
    while (in >> word)
        words.push_back(word);

    biggies(words, 6);

    system("pause");
    return 0;
}

10.24

#include
#include
#include
#include
#include
#include

using namespace std;
using namespace std::placeholders;


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

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

void biggies(vector &vc, const string &s)
{
    auto p = find_if(vc.begin(), vc.end(), bind(check_size, s, _1));

    cout << "第" << p - vc.begin() + 1 << "個數" << *p << "大於等於" << s << "的長度" << endl;
}

int main(int argc, char *argv[])
{
    vector vc = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    biggies(vc, "Hello");
    biggies(vc, "everyone");
    biggies(vc, "!");


    system("pause");
    return 0;
}

10.25

#include
#include
#include
#include
#include
#include

using namespace std;
using namespace std::placeholders;


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

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

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

    words.erase(end_unique, words.end());
}

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

void biggies(vector&words, vector::size_type sz)
{
    elimDups(words);
    for_each(words.begin(), words.end(), [](const string &s){cout << s << " "; });
    cout << endl;
    auto wc = partition(words.begin(), words.end(), bind(check_size, _1, sz));
    auto count = wc - words.begin();
    cout << count << " " << make_plural(count, "word", "s") << " of length " << sz << " or longer" << endl;

    for_each(words.begin(), wc, [](const string &s){cout << s << " "; });
    cout << endl;
}

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗" << endl;
        exit(1);
    }

    vector words;
    string word;
    while (in >> word)
        words.push_back(word);

    biggies(words, 6);

    system("pause");
    return 0;
}

10.27

#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    vector vi = { 1, 2, 2, 3, 4, 5, 5, 6 };
    list li;

    unique_copy(vi.begin(), vi.end(), inserter(li, li.begin()));

    for (auto v : li)
    {
        cout << v << " ";
    }
    cout << endl;

    return 0;
}

10.28

#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    vector vi = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    list li1, li2, li3;

    unique_copy(vi.begin(), vi.end(), inserter(li1, li1.begin()));
    for (auto v : li1)
        cout << v << " ";
    cout << endl;

    unique_copy(vi.begin(), vi.end(), back_inserter(li2));
    for (auto v : li2)
        cout << v << " ";
    cout << endl;

    unique_copy(vi.begin(), vi.end(), front_inserter(li3));
    for (auto v : li3)
        cout << v << " ";
    cout << endl;

    return 0;
}

10.29

#include
#include
#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }
    istream_iterator in_iter(in);
    istream_iterator eof;
    vector words;
    while (in_iter != eof)
        words.push_back(*in_iter++);

    for (auto word : words)
        cout << word << " ";
    cout << endl;



    return 0;
}

10.30

#include
#include
#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    istream_iterator in_iter(cin);
    istream_iterator eof;
    vector vi;
    while (in_iter != eof)
        vi.push_back(*in_iter++);

    sort(vi.begin(), vi.end());

    ostream_iterator out_iter(cout, " ");
    copy(vi.begin(), vi.end(), out_iter);


    return 0;
}

10.31

#include
#include
#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    istream_iterator in_iter(cin);
    istream_iterator eof;
    vector vi;
    while (in_iter != eof)
        vi.push_back(*in_iter++);

    sort(vi.begin(), vi.end());

    ostream_iterator out_iter(cout, " ");
    unique_copy(vi.begin(), vi.end(), out_iter);


    return 0;
}

10.32

#include
#include
#include
#include
#include
#include
#include
#include
#include"Sales_data.h"

using namespace std;

int main()
{
    vectorvs;
    istream_iterator in_iter(cin);
    istream_iterator eof;

    while (in_iter != eof)
        vs.push_back(*in_iter++);

    if (vs.empty())
    {
        std::cerr << "No data?" << std::endl;
        return -1;
    }
    sort(vs.begin(), vs.end(), compareIsbn);

    auto l = vs.begin();
    while (l != vs.end())
    {
        auto item = *l;
        auto r = find_if(l + 1, vs.end(), [item](const Sales_data &item1){return item1.isbn() != item.isbn(); });
        cout << accumulate(l + 1, r, item) << endl;
        l = r;
    }
    return 0;
}

10.33

#include
#include
#include
#include
#include
#include
#include
#include
#include"Sales_data.h"

using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 4)
    {
        cout << "用法:execise.exe in file ";
        cout<<"out_file1 out_file2" << endl;
        return -1;
    }

    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗!" << endl;
        exit(1);
    }

    ofstream out1(argv[2]);
    if (!out1)
    {
        cout << "打開輸出文件1失敗!" << endl;
        exit(1);
    }
    ofstream out2(argv[3]);
    if (!out2)
    {
        cout << "打開輸出文件2失敗!" << endl;
        exit(1);
    }

    istream_iterator in_iter(in);
    istream_iterator eof;
    ostream_iterator out_iter1(out1, " ");
    ostream_iterator out_iter2(out2, "\n");
    while (in_iter != eof)
    {
        if (*in_iter & 1)
            *out_iter1++ = *in_iter;
        else *out_iter2++ = *in_iter;
        in_iter++;
    }
    return 0;

}

10.34

#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cout << "用法:execise.exe in_file" << endl;
        return -1;
    }

    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗! " << endl;
        exit(1);
    }

    vector vi;
    int v;
    while (in >> v)
        vi.push_back(v);

    for (auto r_iter = vi.crbegin(); r_iter != vi.crend();++r_iter)
        cout << *r_iter << " ";
    cout << endl;

    return 0;
}

10.35

#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cout << "用法:execise.exe in_file" << endl;
        return -1;
    }

    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗! " << endl;
        exit(1);
    }

    vector vi;
    int v;
    while (in >> v)
        vi.push_back(v);

    for (auto r_iter = vi.cend(); r_iter != vi.begin();)
        cout << *(--r_iter) << " ";
    cout << endl;

    return 0;
}

10.36

#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    list li = { 0, 1, 2, 0, 3, 4, 5, 0, 6 };

    auto last_z = find(li.rbegin(), li.rend(), 0);
    last_z++;
    int p = 1;
    for (auto iter = li.begin(); iter != last_z.base(); iter++, p++);
    if (p >= li.size())
        cout << "容器中沒有0" << endl;
    else
        cout << "最後一個0在第" << p << "個位置" << endl;

    return 0;
}
#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    list li = { 0, 1, 2, 0, 3, 4, 5, 0, 6 };

    auto prev = find(li.begin(), li.end(), 0);
    if (prev == li.end())
        cout << "容器中沒有0" << endl;
    else
    {
        auto curr = prev;
        while (curr != li.end())
        {
            prev = curr;
            curr++;
            curr = find(curr, li.end(), 0);
        }
        int p = 1;
        for (auto iter = li.begin(); iter != prev; iter++, p++);
        cout << "最後一個0在第" << p << "個位置" << endl;
    }

    return 0;
}

10.37

#include
#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    ostream_iterator out_iter(cout, " ");
    vector vi = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    copy(vi.begin(), vi.end(), out_iter);
    cout << endl;

    list li;

    vector::reverse_iterator re(vi.begin() + 2);
    vector::reverse_iterator rb(vi.begin() + 7);
    copy(rb, re, back_inserter(li));
    copy(li.begin(), li.end(), out_iter);
    cout << endl;


    return 0;
}
#include
#include
#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    ostream_iterator out_iter(cout, " ");
    vector vi = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    copy(vi.begin(), vi.end(), out_iter);
    cout << endl;

    list li;
    copy(vi.rbegin() + vi.size() - 7, vi.rbegin() + vi.size() - 3 + 1, back_inserter(li));
    copy(li.begin(), li.end(), out_iter);
    cout << endl;

    return 0;
}

10.42

#include
#include
#include
#include
#include
#include
#include

using namespace std;

inline void output_words(list &words)
{
    for (auto iter = words.begin(); iter != words.end(); iter++)
        cout << *iter << " ";
    cout << endl;
}

void elimDups(list &words)
{
    output_words(words);

    words.sort();
    output_words(words);

    words.unique();
    output_words(words);
}

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout << "打開輸入文件失敗" << endl;
        exit(1);
    }

    list words;
    string word;
    while (in >> word)
        words.push_back(word);

    elimDups(words);
    return 0;
}
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved