关于const字符串数组存储一个奇怪的现象
// ex6, ch 8
#include <iostream>
#include <cstring>
template <typename T>
T ret_max(T [], int);
template <> char * ret_max(char *[], int);
int main()
{
using namespace std;
int ar_int[6] = {3, 7, 2, 5, 6, 4};
double ar_doub[4] = {2.1, 3.4, 1.8, 4.2};
char * str[5] = {
"I really can't understand you.",
"You're my dearest baby.",
"How much do you love me?",
"I really can't understand you.",
"I really can't understand you."
};
char * s;
cout << "Max of the six integers: " << ret_max(ar_int, 6) << endl;;
cout << "Max of the four integers: " << ret_max(ar_doub, 4) << endl;
cout << "The five strings and their addresses:" << endl;
for (int i = 0; i < 5; i++)
{
cout << str[i] << endl;
cout << (unsigned)str[i] << endl;
}
cout << "The largest of the five and its address:" << endl;
s = ret_max(str, 5);
cout << s << endl;
cout << (unsigned)s << endl; // 用unsigned打印指针内容,请还有其他打印法可以显示0x进制的模式吗?
return 0;
}
template <typename T>
T ret_max(T ar[], int n)
{
T max = ar[0];
for(int i = 0; i < n; i++)
if(max < ar[i])
max = ar[i];
return max;
}
template <> char * ret_max(char * str[], int n)
{
int i, max = 0;
for(i = 0; i < n; i++)
if(strlen(str[max]) < strlen(str[i]))
max = i;
return str[max];
}
运行结果:
Max of the six integers: 7
Max of the four integers: 4.2
The five strings and their addresses:
I really can't understand you.
4214892 // 这些长度一致的数组,竟然地址一样!
You're my dearest baby.
4214923
How much do you love me?
4214947
I really can't understand you.
4214892 // 同上
I really can't understand you.
4214892 // 同上
The largest of the five and its address:
I really can't understand you.
4214892
相关推荐
freecodecamp-javascript:我的一些奇怪的解决方案
freecodecamp-javascript 只是我一些奇怪的解决方案
javascript中一些数据类型以及奇怪的特性
NULL 博文链接:https://rogerfederer.iteye.com/blog/1883737
StrangeDay:工地代码奇怪的一天
StrangeDay:工地代码奇怪的一天
js常见经典面试题汇总
js常见面试题汇总 1.js的数据类型 2.双等和三等的区别 3.js中布尔值为false的六种情况 4.let const var 区别 5.普通函数和箭头函数的区别 6.数组有哪些方法 7.map()对比forEach() 8.for in 对比 for of区别 9.扁平化数组代码实现 10.数组去重的方法 11.防抖和节流 12.事件循环机制 13.原型与原型链 14.localStorage、sessionStorage、cookie区别 15.判断数据类型的方式以及区别 (typeof instanceof 他们两者的区别) 16.null typeof为什么是一个object 17.事件冒泡与事件捕获原理 18.父div和子div都绑定了click事件,点击子div触发事件,这个事件的回调顺序 19.阻止冒泡的方式及作用 20.事件委托的理解 21.call、apply、bind的区别 22.js闭包 23.内存泄漏怎么理解 24.哪些操作会造成内存泄露
cpp代码-C和C++奇怪内容 ## # __VA_ARGS__和... (可变参数) 宽字符
cpp代码-C和C++奇怪内容 ## # __VA_ARGS__和... (可变参数) 宽字符


