為生存而奔跑
::
首頁(yè)
::
聯(lián)系
::
聚合
::
管理
271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks
留言簿
(5)
給我留言
查看公開(kāi)留言
查看私人留言
我參與的團(tuán)隊(duì)
隨筆分類(lèi)
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)
技術(shù)(12)
無(wú)聊(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)
相冊(cè)
Girl
搜索
積分與排名
積分 - 328893
排名 - 74
最新評(píng)論
1.?re: Invoke與BeginInvoke
講得很好,清晰明了
--YJJ
2.?re: Invoke與BeginInvoke
講的這么好, 為啥沒(méi)有人頂呢
--zhouandke
3.?re: 數(shù)組分割問(wèn)題
轉(zhuǎn)載請(qǐng)注明
--呵呵
4.?re: HDU 3415 單調(diào)隊(duì)列
話說(shuō),sum數(shù)組為什么只開(kāi)10W就能過(guò),如果n=100000,k=100000,明顯要開(kāi)20W啊
--KissLL
5.?re: GDB 單步調(diào)試
文章太強(qiáng)大了。
--kangear
閱讀排行榜
1.?GDB 單步調(diào)試(33345)
2.?Emacs教程(20836)
3.?解決“windows無(wú)法連接到選定網(wǎng)絡(luò) 網(wǎng)絡(luò)可能不在區(qū)域中”(11475)
4.?Invoke與BeginInvoke(9598)
5.? Eclipse下搭建SWT開(kāi)發(fā)環(huán)境(8011)
評(píng)論排行榜
1.?C/C++沒(méi)有數(shù)組(12)
2.?HDU 3415 單調(diào)隊(duì)列(8)
3.?Ubuntu Linux常見(jiàn)中文輸入法匯總(7)
4.?word畫(huà)圖里自選圖形里面的連接符不能用(5)
5.?<編程之美>數(shù)組分割問(wèn)題(3)
指針做參數(shù)
百度一個(gè)筆試題
#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
;
}
很容易被忽悠的一個(gè)地方是,create函數(shù)的第一個(gè)參數(shù),類(lèi)型時(shí)complex_t * p。 然后,在create里面給p分配了一塊存儲(chǔ)空間。 乍一看,因?yàn)槭侵羔樧鰠?shù),所以會(huì)傳遞回去。其實(shí)不然。在這里,相當(dāng)于,首先,main函數(shù)中調(diào)用create函數(shù)時(shí),把comps賦值給p。即指針p指向與comps相同的一段存儲(chǔ)空間。 但是,在create里面,p=new complex_t[n],使得p又指向了一塊新的存儲(chǔ)空間。而此時(shí),comps還是指向原來(lái)的存儲(chǔ)空間。所以,在create里面對(duì)p做的更改對(duì)comps并沒(méi)有影響。
一個(gè)解決方法是使用指向指針的指針。如下
#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函數(shù)中申請(qǐng)空間,而不是在create函數(shù)中。
看下面的例子
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函數(shù)中申請(qǐng)內(nèi)存,在maxContinueNum函數(shù)中更改其中的值。
posted on 2011-05-04 23:43
baby-fly
閱讀(655)
評(píng)論(0)
編輯
收藏
引用
所屬分類(lèi):
Effective STL / C++
只有注冊(cè)用戶
登錄
后才能發(fā)表評(píng)論。
【推薦】100%開(kāi)源!大型工業(yè)跨平臺(tái)軟件C++源碼提供,建模,組態(tài)!
相關(guān)文章:
指針做參數(shù)
C++虛函數(shù)表解析(轉(zhuǎn))
More Effective C++ 不要對(duì)數(shù)組使用多態(tài)
Clause 19:相等equality 和等價(jià) equivalence
Clause 21:總是讓比較函數(shù)在等值情況下返回false
STL中仿函數(shù)(functors)、類(lèi)成員和mem_fun的使用
STL之仿函數(shù),適配器簡(jiǎn)介
STL lower_bound upper_bound equal_range
Clause 22 不要直接修改set或multiset中的鍵值
Clause 19 刪除元素的方法
網(wǎng)站導(dǎo)航:
博客園
IT新聞
BlogJava
博問(wèn)
Chat2DB
管理
Copyright @ baby-fly
Powered by:
.Text
and
ASP.NET
Theme by:
.NET Monster
亚洲国产婷婷香蕉久久久久久
|
久久无码高潮喷水
|
无码人妻久久一区二区三区蜜桃
|
日韩欧美亚洲综合久久影院Ds
|
久久久久久久尹人综合网亚洲
|
色综合久久中文色婷婷
|
日韩AV毛片精品久久久
|
2021久久精品国产99国产精品
|
国产精品欧美久久久天天影视
|
久久久精品日本一区二区三区
|
久久青草国产手机看片福利盒子
|
久久亚洲AV无码西西人体
|
韩国免费A级毛片久久
|
蜜臀久久99精品久久久久久
|
久久精品国产亚洲AV嫖农村妇女
|
中文字幕精品无码久久久久久3D日动漫
|
久久精品中文騷妇女内射
|
97久久久久人妻精品专区
|
伊人久久一区二区三区无码
|
狠狠精品久久久无码中文字幕
|
中文字幕成人精品久久不卡
|
人妻精品久久久久中文字幕69
|
看全色黄大色大片免费久久久
|
亚洲国产精品久久久久婷婷软件
|
久久精品一本到99热免费
|
精品国产乱码久久久久久呢
|
久久久久一级精品亚洲国产成人综合AV区
|
久久久噜噜噜久久熟女AA片
|
国产成人精品综合久久久
|
亚洲欧美国产精品专区久久
|
7国产欧美日韩综合天堂中文久久久久
|
国内高清久久久久久
|
伊人久久精品影院
|
免费无码国产欧美久久18
|
少妇被又大又粗又爽毛片久久黑人
|
99久久国产综合精品成人影院
|
久久www免费人成看国产片
|
91久久精一区二区三区大全
|
精品国产乱码久久久久久郑州公司
|
久久亚洲中文字幕精品有坂深雪
|
亚洲精品乱码久久久久久自慰
|