第一次发表技术博客,谨以此几句,抒发自己的激动之情。
刚刚从博奥(国企专做MFC界面)辞职,想好好修炼下内功:算法与数据结构。遂从网络上找到一个无名小题磨磨枪,实现一个数组的倒序存储。如a,b,c,d到d,c,b,a; 开始以为很简单,但是手实在太生了,很多基础知识都忘了。现在从这个小程序,逐一开始扩展:
#include "stdafx.h"
#include
using namespace std;
void reverse_order(char * p, int len)
{
char tmp;
if (len%2)
{
for(int i=0; i<(len-1)/2; i++)
{
tmp = p[i];
p[i] = p[len-i-1];
p[len-i-1] = tmp;
}
}
else
{
for(int i=0; i
问题1:在用VS2010写一段代码,首先不是算法问题,但是调试的时候发现报 Stack around the variable 'xxx' was corrupted 的错误,这不是什么高大上的错误,后来发现是 数组越界 造成的。问题发现是从一篇博客借鉴的:http://blog.youkuaiyun.com/delphiwcdj/article/details/6336808 仔细阅读他的博客,再去对应找自己的数组是否也存在数组越界,发现果然出现在13-14和22-23行代码(13-14和22-23行的代码是本来是一样的,但是为了表达清晰,13-14是正确的不越界的代码,而22-23是错误越界代码)。在22-23行代码中,当
i=0 就马上出现了p[0] = p[len] 的数组越界问题,解决之后,程序正常运行!

问题2:优化设计---将函数打包为函数模板,练习一下
// test1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
using namespace std;
template //These placeholders, in this case, T, are used to specify the types of the function’s
//parameters , to specify the function’s return type and to declare variables
//within the body of the function definition.
void reverse_order(T * p, int len)
{
char tmp;
if (len%2)
{
for(int i=0; i<(len-1)/2; i++)
{
tmp = p[i];
p[i] = p[len-i-1];
p[len-i-1] = tmp;
}
}
else
{
for(int i=0; i
本文作者分享了从一家专注于MFC界面的国有企业辞职后,决心深入学习算法与数据结构的心路历程。通过解决一个简单的数组倒序问题,作者反思并逐步扩展了自己的知识体系。文章中详细记录了从遇到数组越界错误到成功解决的过程,并通过代码实例展示了优化设计的重要性。此外,作者还尝试将原有函数打包为函数模板,以此作为进一步提升编程技巧的练习。
1134

被折叠的 条评论
为什么被折叠?



