為生存而奔跑
::
首頁
::
聯系
::
聚合
::
管理
271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks
留言簿
(5)
給我留言
查看公開留言
查看私人留言
我參與的團隊
隨筆分類
Algorithm(73)
C#(19)
Design Pattern(16)
Effective STL / C++ (12)
Information Retrival / Data Mining(13)
Java(25)
Linux kernel(2)
MFC(16)
Python(5)
TopCoder(1)
Ubuntu&Linux(56)
技術(12)
無聊(2)
雜(22)
隨筆檔案
2011年5月 (1)
2011年4月 (6)
2011年3月 (21)
2011年2月 (9)
2011年1月 (12)
2010年12月 (2)
2010年11月 (3)
2010年10月 (6)
2010年8月 (13)
2010年7月 (11)
2010年6月 (7)
2010年5月 (21)
2010年4月 (15)
2010年3月 (16)
2010年1月 (5)
2009年12月 (18)
2009年11月 (18)
2009年10月 (19)
2009年9月 (8)
2009年8月 (42)
2009年7月 (15)
2009年4月 (3)
相冊
Girl
搜索
積分與排名
積分 - 326945
排名 - 74
最新評論
1.?re: Invoke與BeginInvoke
講得很好,清晰明了
--YJJ
2.?re: Invoke與BeginInvoke
講的這么好, 為啥沒有人頂呢
--zhouandke
3.?re: 數組分割問題
轉載請注明
--呵呵
4.?re: HDU 3415 單調隊列
話說,sum數組為什么只開10W就能過,如果n=100000,k=100000,明顯要開20W啊
--KissLL
5.?re: GDB 單步調試
文章太強大了。
--kangear
閱讀排行榜
1.?GDB 單步調試(33330)
2.?Emacs教程(20825)
3.?解決“windows無法連接到選定網絡 網絡可能不在區域中”(11466)
4.?Invoke與BeginInvoke(9583)
5.? Eclipse下搭建SWT開發環境(7996)
評論排行榜
1.?C/C++沒有數組(12)
2.?HDU 3415 單調隊列(8)
3.?Ubuntu Linux常見中文輸入法匯總(7)
4.?word畫圖里自選圖形里面的連接符不能用(5)
5.?VMware Tools installation cannot be started manually while Easy Install is in progress.(3)
指針做參數
百度一個筆試題
#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)
{
*
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
;
}
另外一種方法是,在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
閱讀(647)
評論(0)
編輯
收藏
引用
所屬分類:
Effective STL / C++
只有注冊用戶
登錄
后才能發表評論。
【推薦】100%開源!大型工業跨平臺軟件C++源碼提供,建模,組態!
相關文章:
指針做參數
C++虛函數表解析(轉)
More Effective C++ 不要對數組使用多態
Clause 19:相等equality 和等價 equivalence
Clause 21:總是讓比較函數在等值情況下返回false
STL中仿函數(functors)、類成員和mem_fun的使用
STL之仿函數,適配器簡介
STL lower_bound upper_bound equal_range
Clause 22 不要直接修改set或multiset中的鍵值
Clause 19 刪除元素的方法
網站導航:
博客園
IT新聞
BlogJava
博問
Chat2DB
管理
Copyright @ baby-fly
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster
久久精品无码一区二区app
|
久久天天躁狠狠躁夜夜avapp
|
美女久久久久久
|
久久婷婷五月综合国产尤物app
|
久久乐国产精品亚洲综合
|
久久被窝电影亚洲爽爽爽
|
91精品国产9l久久久久
|
中文字幕无码久久精品青草
|
久久影院亚洲一区
|
亚洲中文字幕伊人久久无码
|
久久久久久极精品久久久
|
99久久精品九九亚洲精品
|
久久精品国产清自在天天线
|
久久久精品人妻一区二区三区蜜桃
|
色婷婷久久综合中文久久一本
|
久久久久久亚洲精品无码
|
日日狠狠久久偷偷色综合96蜜桃
|
青青热久久国产久精品
|
久久99国产精品久久99小说
|
久久亚洲AV成人无码
|
无码八A片人妻少妇久久
|
久久综合给合久久狠狠狠97色69
|
亚洲精品国产自在久久
|
伊人久久大香线蕉综合Av
|
国产欧美久久久精品影院
|
亚洲国产成人久久综合一
|
青青青青久久精品国产
|
久久国产福利免费
|
国产激情久久久久影院
|
亚洲国产一成久久精品国产成人综合
|
久久国产高清字幕中文
|
久久久久综合中文字幕
|
久久久久99精品成人片试看
|
激情综合色综合久久综合
|
亚洲午夜无码久久久久
|
国产精品xxxx国产喷水亚洲国产精品无码久久一区
|
日韩亚洲欧美久久久www综合网
|
久久亚洲中文字幕精品一区
|
成人国内精品久久久久一区
|
久久不见久久见免费影院www日本
|
久久亚洲sm情趣捆绑调教
|