欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C++Primer(第10章课后程序题源代码)

程序员文章站 2022-12-10 13:28:14
10.1 #include #include #include #include using namespace std; int main(int arg...

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  ls;
    string word;
    while (in >> word)
        ls.push_back(word);

    cout > word;

    cout 

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  vi;
    int val;
    while (in >> val)
        vi.push_back(val);

    cout 

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  vd;
    double val;
    while (in >> val)
        vd.push_back(val);

    cout 

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 

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  vi;
    int val;
    while (in >> val)
    {
        vi.push_back(val);
        cout 

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  lst;
    vector vec;
    int val;
    while (in >> val)
    {
        lst.push_back(val);
    }
    copy(lst.begin(), lst.end(), back_inserter(vec));

    cout 
#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 

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  &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  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  &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  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 = 5;
}

int main(int argc, char *argv[])
{
    ifstream in(argv[1]);
    if (!in)
    {
        cout  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 

10.15

#include

using namespace std;

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

    cout 

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 

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 = 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  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()  &vc, const string &s)
{
    auto p = find_if(vc.begin(), vc.end(), bind(check_size, s, _1));

    cout  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  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 

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 

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  in_iter(in);
    istream_iterator eof;
    vector words;
    while (in_iter != eof)
        words.push_back(*in_iter++);

    for (auto word : words)
        cout 

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 

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  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  vi;
    int v;
    while (in >> v)
        vi.push_back(v);

    for (auto r_iter = vi.crbegin(); r_iter != vi.crend();++r_iter)
        cout 

10.35

#include
#include
#include
#include

using namespace std;

int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        cout  vi;
    int v;
    while (in >> v)
        vi.push_back(v);

    for (auto r_iter = vi.cend(); r_iter != vi.begin();)
        cout 

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 
#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 

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  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 
#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  li;
    copy(vi.rbegin() + vi.size() - 7, vi.rbegin() + vi.size() - 3 + 1, back_inserter(li));
    copy(li.begin(), li.end(), out_iter);
    cout 

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  &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  words;
    string word;
    while (in >> word)
        words.push_back(word);

    elimDups(words);
    return 0;
}