6.26 编写接受两个实参的main函数,把实参的内容连接成一个string对象并输出。
#include <iostream>
#include <string>
using namespace std;
int main ( int argc, char * * argv)
{
string str;
for ( int i = 1 ; i != argc; ++ i)
str + = string ( argv[ i] ) + " " ;
cout << str << endl;
system ( "pause" ) ;
return 0 ;
}
6.27 编写一个函数,它的参数是initializer_list类型的对象,函数的功能是计算列表中所有元素的和。
#include <iostream>
#include <initializer_list>
using namespace std;
int sum ( initializer_list< int > const & il)
{
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对象内容
#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) ;
}
}
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的单数和复数形式。
#include <iostream>
#include <string>
#include <vector>
using namespace std;
using Iter = vector< int > :: const_iterator;
string make_plural ( size_t ctr, const string & word, const string & ending = "s" )
{
return ( ctr > 1 ) ? word + ending : word;
}
int main ( )
{
cout << "singual: " << make_plural ( 1 , "success" , "es" ) << " "
<< make_plural ( 1 , "failure" ) << endl;
cout << "plural : " << make_plural ( 2 , "success" , "es" ) << " "
<< make_plural ( 2 , "failure" ) << endl; ;
system ( "pause" ) ;
return 0 ;
}
6.44 将189页的isShorter函数改写成内联函数
#include <iostream>
#include <string>
using namespace std;
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对象的大小。分别在打开和关闭调试器的情况下编译并执行这个程序。
#include <iostream>
#include <vector>
using namespace std;
void printVec ( vector< int > & vec)
{
#ifndef NDEBUG
cout << "vector size: " << vec. size ( ) << endl;
#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个版本,令其各输出一条可以区分的消息。
#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 ( 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;
inline int f ( const int , const int ) ;
typedef decltype ( f) fp;
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 } ;
int main ( )
{
for ( auto it = vec. cbegin ( ) ; it != vec. cend ( ) ; ++ it)
cout << ( * it) ( 2 , 2 ) << std:: endl;
system ( "pause" ) ;
return 0 ;
}