21.
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int compare(const int val, const int *p)
{
if (val < *p)
{
cout << "the biggest is " << *p << endl;
return *p;
}
else if (val > *p)
{
cout << "the biggest is " << val << endl;
return val;
}
else
{
cout << "they are same" << endl;
return 0;
}
}
int compare2(const int val, const int *p)
{
return (val > *p) ? val : *p;
}
int main()
{
int m = 10;
int b = 15;
int *p = &b;
cout << compare(m, p) << endl;
srand((unsigned) time (NULL));//产生随机种子
int a[10];
for (auto &i : a)
{
i = rand()%100;
}
cout << "please enter a number: " ;
int j;
cin >> j;
cout << "the biggest is :" << compare2(j, a) << endl;
cout << "the arr is: " << endl;
for (auto i : a)
{
cout << i << " ";
}
cout << endl;
return 0;
}
6.22
/*
* 当函数以值传递的方式使用指针,所有改变都局限于函数内部,当函数执行完毕后,即不会改变指针本身的值
* 也不会改变指针所指的内容
* 当函数以值传递的方式使用指针是,如果函数内部通过解引用的方式直接访问内存,函数执行完毕后,
* 修改了指针所指的内容
* 当函数以引用的方式使用指针时,既改变了指针所指向的内存,也改变了指针所指向的值
*/
#include <iostream>
using namespace std;
//既不交换指针,也不交换指针所指的内容
void swap1(int *p, int *q)
{
int *temp;
temp = p;
p = q;
q = temp;
}
//交换指针所指的内容
void swap2(int *p, int *q)
{
int temp = *p;
*p = *q;
*q = temp;
}
//函数交换指针本身的值,即交换指针所指的内存地址
void swap3(int *&p, int *&q)
{
int *temp = p;
p = q;
q = temp;
}
int main()
{
int a = 5, b = 10;
int *p = &a, *q = &b;
cout << "before swap:" << endl;
cout << "p's value is: " << p << ", q's value is: " << q << endl;
cout << "p point to the value is: " << *p << ", q point to the value is " << *q << endl;
swap1(p, q);
cout << "after swap:" << endl;
cout << "p's value is: " << p << ", q's value is: " << q << endl;
cout << "p point to the value is: " << *p << ", q point to the value is " << *q << endl;
a = 5, b = 10;
p = &a, q = &b;
cout << "before swap:" << endl;
cout << "p's value is: " << p << ", q's value is: " << q << endl;
cout << "p point to the value is: " << *p << ", q point to the value is " << *q << endl;
swap2(p, q);
cout << "after swap:" << endl;
cout << "p's value is: " << p << ", q's value is: " << q << endl;
cout << "p point to the value is: " << *p << ", q point to the value is " << *q << endl;
a = 5, b = 10;
p = &a, q = &b;
cout << "before swap:" << endl;
cout << "p's value is: " << p << ", q's value is: " << q << endl;
cout << "p point to the value is: " << *p << ", q point to the value is " << *q << endl;
swap3(p, q);
cout << "after swap:" << endl;
cout << "p's value is: " << p << ", q's value is: " << q << endl;
cout << "p point to the value is: " << *p << ", q point to the value is " << *q << endl;
return 0;
}
6.23
#include <iostream>
using namespace std;
//int型数据使用此函数无效
void print1(const int *p)
{
if (p)
{
while (*p)
{
cout << *p++;
}
}
}
void print2(const int *beg, const int *end)
{
while (beg != end)
{
cout << *beg++ << endl;
}
}
void print3(const int ia[], size_t size)
{
for (size_t i = 0; i != size; i++)
{
cout << ia[i] << endl;
}
}
int main()
{
int i = 0, j[2] = {0, 1};
cout << "begin" << endl;
print1(&i);
print1(j);
//auto a = begin(j);
//auto b = end(j);
print2(begin(j), end(j));
print3(j, sizeof(j)/sizeof(*j));
return 0;
}
6.24
如果传输的参数数组不是10个元素,调用次函数会出问题
6.25
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
if (argc < 2)
{
cout << "the argument is not enough!!" << endl;
cout << "Usage: a.out argu[1] argu[2]";
return 0;
}
string str;
for (int i = 0; i < argc; i++)
{
str += argv[i];
}
cout << "after connect: " << str << endl;
return 0;
}
6.26
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
for (int i = 0; i < argc; i++)
{
cout << "argc[" << i << "]: = " << argv[i] << endl;
}
return 0;
}
6.27
#include <iostream>
using namespace std;
int mysum(initializer_list<int> num)
{
int sum = 0;
for (auto it : num)
{
sum += it;
}
return sum;
}
int main()
{
cout << "1,2,3,4,5 : " << mysum({1,2,3,4,5}) << endl;
cout << "1 2 3: " << mysum({1,2,3}) << endl;
return 0;
}
6.28
const string &
6.29
引用类型的主要优势是可以直接操作所引用的对象以及避免拷贝较为复杂的类型对象和容器对象。因为initializer_list对象的元素永远是常量值,所以我们不可能通过设定引用类型来个更改循环控制变量的内容。只有当initializer_list对象的元素类型是类类型或者容器类型(如string)时,才有必要吧范围for循环的循环控制变量设为引用类型。
6.30
for循环中的return语句是非法的,没有返回任何值。for循环可能会一直执行完毕,但是循环结束后缺少一个return语句(编译器无法识别这个错误)。