C++ primer5 6.27/6.33/6.42/6.44/6.47/6.51

6.26 编写接受两个实参的main函数,把实参的内容连接成一个string对象并输出。

//6.26 这题非常的重要!!!
#include <iostream>
#include <string>
using namespace std;
//argv指向char*,argc代表字符串的数量
int main(int argc, char **argv)
{  //特殊点:使用argv中的实参时,一定要记得可选参数从argv[1]开始,argv[0]保存的是程序的名字。
    string str;
    for (int i = 1; i != argc; ++i)
        str += string(argv[i]) + " ";

    cout << str << endl;

    system("pause");
    return 0;
}

6.27 编写一个函数,它的参数是initializer_list类型的对象,函数的功能是计算列表中所有元素的和。

//6.27
#include <iostream>
#include <initializer_list>
using namespace std;
int sum(initializer_list<int> const& il) 
{   //参数是initializer_list<int>型对象
    int sum = 0;
    for (auto i : il) 
         sum += i;
    return sum;
}

int main(int argc, char **argv)
{  
    auto il = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    std::cout << sum(il) << std::endl; //计算列表元素的和

    system("pause");
    return 0;
}

6.33 编写递归函数,输出vector对象内容

//6.33
#include <iostream>
#include <vector>
using namespace std;
using Iter = vector<int>::const_iterator;

void print(Iter first, Iter last)
{
    if (first != last)
    {
        cout << *first << " ";
        print(++first, last); //递归函数输出vector对象的内容
    }
}

int main( )
{  
    vector<int> vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    print(vec.cbegin(), vec.cend());

    system("pause");
    return 0;
}

6.42 给make_plural函数(201页)的第二个形参赋予默认实参’s’,利用新版本的函数输出单词success和failure的单数和复数形式。

//6.42
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using Iter = vector<int>::const_iterator;
                                                   //赋予默认实参's'
string make_plural(size_t ctr, const string &word, const string &ending = "s")
{
    return (ctr > 1) ? word + ending : word; //如果值大于1,返回word的复数形式
}

int main( )
{  
    cout << "singual: " << make_plural(1, "success", "es") << " "
         << make_plural(1, "failure") << endl;  //值1,输出单数形式
    cout << "plural : " << make_plural(2, "success", "es") << " "
         << make_plural(2, "failure") << endl;; //值2,输出复数形式

    system("pause");
    return 0;
}

6.44 将189页的isShorter函数改写成内联函数

//6.44
#include <iostream>
#include <string>
using namespace std;

// defining in the header is better.
inline bool is_shorter(const string &lft, const string &rht) 
{
    return lft.size() < rht.size();
}

int main( )
{  
    cout << is_shorter("pezy", "mooophy") << endl;

    system("pause");
    return 0;
}

6.47 改写205页的练习中使用递归输出vector内容的程序,使其有条件地输出与执行过程有关的信息。例如,每次调用时输出vector对象的大小。分别在打开和关闭调试器的情况下编译并执行这个程序。

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

void printVec(vector<int> &vec)
{
#ifndef NDEBUG
    cout << "vector size: " << vec.size() << endl;//每次调用时输出vector对象vec的大小
#endif
    if (!vec.empty())
    {
        auto tmp = vec.back();
        vec.pop_back(); //输出一个元素
        printVec(vec); //递归函数
        cout << tmp << " ";
    }
}

int main( )
{  
    vector<int> vec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    printVec(vec);
    cout << endl;

    system("pause");
    return 0;
}

6.51 编写函数f的4个版本,令其各输出一条可以区分的消息。

//6.51 函数重载 函数匹配
#include <iostream>
using namespace std;

void f()
{
    cout << "f()" << endl;
}
void f(int)
{
    cout << "f(int)" << endl;
}
void f(int, int)
{
    cout << "f(int, int)" << endl;
}
void f(double, double)
{
    cout << "f(double, double)" << endl;
}


int main( )
{  
    //f(2.56, 42); // 错误,f模棱两可,存在二义性
    f(42);
    f(42, 0);
    f(2.56, 3.14);

    system("pause");
    return 0;
}

6.54/55/56 编写函数声明,令其接受两个int型形参并且返回类型也是int;然后声明一个vector对象,令其元素是指向该函数的指针。编写4个函数,分别对两个int值执行加减乘除运算;在上面创建的vector对象中保存这些函数的指针。调用上述vector对象中的每个元素并输出其结果。

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

//6.54 函数f有两个int形参,返回int;
inline int f(const int, const int);
typedef decltype(f) fp; //fp只是函数类型而不是函数指针

//6.55 加减乘除
inline int NumAdd(const int n1, const int n2)  { return n1 + n2; }
inline int NumSub(const int n1, const int n2)  { return n1 - n2; }
inline int NumMul(const int n1, const int n2)  { return n1 * n2; }
inline int NumDiv(const int n1, const int n2)  { return n1 / n2; }

vector<fp*> vec{ NumAdd, NumSub, NumMul, NumDiv }; //vector元素是指向该函数的指针

int main( )
{  //6.56 调用vector对象中的每个元素并输出其结果
   for (auto it = vec.cbegin(); it != vec.cend(); ++it)
        cout << (*it)(2, 2) << std::endl; //对2和2进行加减乘除得4,0,4,1

    system("pause");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值