看到C#的array自帶GetLength()很羨慕嗎?boost::array也自帶size()喔!!
由於boost::array自帶size(),所以當傳進function時,可以不需將array size當參數傳進function。
1
/**/
/*
2
(C) OOMusou 2007 http://oomusou.cnblogs.com
3![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
Filename : boostArraySize.cpp
5
Compiler : Visual C++ 8.0 / ISO C++ (boost)
6
Description : Demo how to use boost array pass to function
7
Release : 02/25/2007 1.0
8
*/
9
#include
<
iostream
>
10
#include
<
boost
/
array.hpp
>
11
#include
"
conio.h
"
12![None.gif](/Images/OutliningIndicators/None.gif)
13
using
namespace
std;
14
using
namespace
boost;
15![None.gif](/Images/OutliningIndicators/None.gif)
16
template
<
size_t N
>
17![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
void
func(array
<
int
,N
>
ia)
{
18![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i = 0; i != ia.size(); ++i)
{
19
cout << ia[i] << endl;
20
}
21
}
22![None.gif](/Images/OutliningIndicators/None.gif)
23![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
int
main()
{
24![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
array<int, 3> ia =
{0 , 1, 2};
25
func(ia);
26
27
getch();
28
}
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
2
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
3
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
4
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
5
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
6
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
7
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
8
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
9
![None.gif](/Images/OutliningIndicators/None.gif)
10
![None.gif](/Images/OutliningIndicators/None.gif)
11
![None.gif](/Images/OutliningIndicators/None.gif)
12
![None.gif](/Images/OutliningIndicators/None.gif)
13
![None.gif](/Images/OutliningIndicators/None.gif)
14
![None.gif](/Images/OutliningIndicators/None.gif)
15
![None.gif](/Images/OutliningIndicators/None.gif)
16
![None.gif](/Images/OutliningIndicators/None.gif)
17
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
18
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
19
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
20
![ExpandedSubBlockEnd.gif](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
21
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
22
![None.gif](/Images/OutliningIndicators/None.gif)
23
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
24
![ExpandedSubBlockStart.gif](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![ContractedSubBlock.gif](/Images/OutliningIndicators/ContractedSubBlock.gif)
![dot.gif](/Images/dot.gif)
25
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
26
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
27
![InBlock.gif](/Images/OutliningIndicators/InBlock.gif)
28
![ExpandedBlockEnd.gif](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
執行結果
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
![None.gif](/Images/OutliningIndicators/None.gif)
18行
![ExpandedBlockStart.gif](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![ContractedBlock.gif](/Images/OutliningIndicators/ContractedBlock.gif)
![dot.gif](/Images/dot.gif)
我們看到ia自帶size()
25行
![None.gif](/Images/OutliningIndicators/None.gif)
也沒有傳array size進去。
Conclusion
C/C++傳統須另外將array size當參數傳進function的缺點,現在有兩種方式解決,一種是使用function template,請參閱(原創) 如何使用function template傳遞array?(C/C++) (template),一種就是本文所使用的boost:array。
See Also
(原創) array傳進function該怎麼寫才好? (C/C++)
(原創) 如何使用function template傳遞array? (C/C++)