为生存而奔跑

   :: 首页 :: 联系 :: 聚合  :: 管理
  271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

留言簿(5)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 319518
  • 排名 - 75

最新评论

阅读排行榜

评论排行榜

百度一个笔试题
#include<iostream>
using namespace std;

struct complex_t
{
    
int real;
    
int imag;
}
;

int create(complex_t *p, unsigned int n)
{
    p 
= new complex_t[n];
    
if(p == NULL)
        
return -1;
    
else return 0;
}


int main()
{
    complex_t 
*comps = NULL;
    unsigned 
int num = 0;
    cin
>>num;
    
if(create(comps,num) < 0)
    
{
        printf(
"create failed\n");
        
return -1;
    }


    
/*if(comps == NULL)
    {
        cout<<"comps is NULL\n";
        return -1;
    }
*/


    
//comps = new complex_t[num];

    
long long int sum = 0;
    unsigned 
int pos = 0;
    cin
>>pos;
    
while(pos<num)
    
{
        cin
>>comps[pos].real>>comps[pos].imag;
        cin
>>comps[pos+1].real>>comps[pos+1].imag;
        sum 
+= comps[pos].real*comps[pos+1].real + comps[pos].imag*comps[pos+1].imag;
        pos
+=2;
    }

    cout
<<"sum is "<<sum<<endl;
    
return 0;
}

很容易被忽悠的一个地方是,create函数的第一个参数,类型时complex_t * p。 然后,在create里面给p分配了一块存储空间。 乍一看,因为是指针做参数,所以会传递回去。其实不然。在这里,相当于,首先,main函数中调用create函数时,把comps赋值给p。即指针p指向与comps相同的一段存储空间。  但是,在create里面,p=new complex_t[n],使得p又指向了一块新的存储空间。而此时,comps还是指向原来的存储空间。所以,在create里面对p做的更改对comps并没有影响。

一个解决方法是使用指向指针的指针。如下
#include<iostream>
using namespace std;

struct complex_t
{
    
int real;
    
int imag;
}
;

int create(complex_t **p, unsigned int n)
{
    
*= new complex_t[n];
    
if(p == NULL)
        
return -1;
    
else return 0;
}


int main()
{
    complex_t 
*comps = NULL;
    unsigned 
int num = 0;
    cin
>>num;
    
if(create(&comps,num) < 0)
    
{
        printf(
"create failed\n");
        
return -1;
    }


    
/*if(comps == NULL)
    {
        cout<<"comps is NULL\n";
        return -1;
    }
*/


    
//comps = new complex_t[num];

    
long long int sum = 0;
    unsigned 
int pos = 0;
    cin
>>pos;
    
while(pos<num)
    
{
        cin
>>comps[pos].real>>comps[pos].imag;
        cin
>>comps[pos+1].real>>comps[pos+1].imag;
        sum 
+= comps[pos].real*comps[pos+1].real + comps[pos].imag*comps[pos+1].imag;
        pos
+=2;
    }

    cout
<<"sum is "<<sum<<endl;
    
return 0;
}

另外一种方法是,在main函数中申请空间,而不是在create函数中。
看下面的例子
bool isDigit(char ch)
{
    
return ch>='0'&&ch<='9';
}


int maxContinueNum(const char * inputstr, char * outputstr)
{
    
int i,j;
    
int maxlen=0;
    
int start;
    i
=0;
    
while(inputstr[i]!='\0')
    
{
        
if(isDigit(inputstr[i]))
        
{
            j
=i+1;
            
while(inputstr[j]!='\0' && isDigit(inputstr[j]))
                j
++;
            
if(j-i>maxlen)
            
{
                maxlen
=j-i;
                start
=i;
            }

            i
=j;
        }

        
else i++;
    }

    
for(i=0;i<maxlen;i++)
        outputstr[i]
=inputstr[i+start];
    outputstr[i]
='\0';
    
return maxlen;
}

int main()
{
    
char input[]={"abcd12345ed125ss123456789"};
    
char *output = new char[100];
    cout
<< maxContinueNum(input, output) << endl;
    
    cout
<<output<<endl;
}

outputstr在main函数中申请内存,在maxContinueNum函数中更改其中的值。

posted on 2011-05-04 23:43 baby-fly 阅读(574) 评论(0)  编辑 收藏 引用 所属分类: Effective STL / C++

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理