[英]Using sizeof() in an initialized, unknown size array - C++
我是C ++編程的新手,我正在嘗試獲得數組的大小。 誰能解釋我為什么會這樣? 我已經嘗試在runnable.com中運行代碼,結果相同。
我確信這不是正確的做法。 如果可能的話,你能建議任何簡單的方法來獲得那種陣列的大小嗎?
#include <iostream>
using namespace std;
int main ()
{
int set1[] = {1, 9, 3, 50, 31, 65};
int set234[] = {3, 5, 5};
cout << sizeof(set1) << endl;
cout << sizeof(set234) << endl;
cout << "When sizeof() return value is divided by 4"<< endl;
cout << sizeof(set1) / 4 << endl;
cout << sizeof(set234) / 4 << endl;
return 0;
}
****************************************************************
Output:
24
12
When sizeof() return value is divided by 4
6
3
****************************************************************
**編輯:謝謝你的回復。 飛走了 :D
數組的大小等於其所有元素的大小總和。 在您的示例中,您處理int類型的數組,然后系統中的sizeof(int)似乎等於4,因此第一個數組有6個元素,那么它的大小等於6 * sizeof( int )
即24。第二個數組的大小等於3 * sizeof( int )
,即12.如果你的系統中的sizeof(int)等於8,那么第一個數組的大小將等於48(6 * sizeof) * int))和第二個數組的大小將等於18(3 * sizeof(int))。
因此,如果您想要知道數組中有多少元素,您可以通過以下方式計算它
sizeof( YouArray ) / sizeof( YourArray[0] )
要么
sizeof( YouArray ) / sizeof( ElementType )
sizeof
返回字節數,而不是元素數。 大多數情況下你應該避免陣列; 使用std::vector
或std::array
,兩者都提供了一種獲取元素數量的簡便方法。
sizeof以字節為單位返回數組的大小,而不是元素。 看起來你正在運行32位系統(或LP64,這意味着只有long和指針是64位,但是int仍然是32),所以整數數組中的每個元素都是4個字節。 在你的set1中,它是6個元素,即6x4 = 24個字節。
要獲取元素的大小,請執行sizeof(set1)/ sizeof(set1 [0])。 但是,它不適用於您在編譯時列出元素的數組。 但是,如果只有int *,則sizeof運算符將返回指針的大小。
sizeof()為您提供以字節為單位的總大小。 例如,如果int為4,則數組中有6個元素。 作為答案,它將返回4 * 6 = 24。 因此,以下代碼將為您提供數組的大小。
#include <iostream>
using namespace std;
int main ()
{
int set1[] = {1, 9, 3, 50, 31, 65};
int set234[] = {3, 5, 5};
cout << (sizeof(set1))/4 << endl;
cout << (sizeof(set234))/4 << endl;
return 0;
}
你也可以用這個:
#include <iostream>
#include <array>
int main ()
{
array<int,5> a;
cout << "size is" << a.size();
return 0;
}
要獲取在堆棧上分配的數組中元素的計數 ,通常的技術是獲取整個數組的總大小(以字節為單位),然后將其除以單個數組項的大小(以字節為單位):
<item count> = <total array size in bytes> / <single array item size in bytes>
但是,此公式只能應用於在堆棧上分配的數組, 而不能應用於在堆上動態分配的數組(例如,使用new[]
):在后一種情況下,您需要將數組項目計數作為單獨的信息保留。
Microsoft Visual C ++提供了一個方便的_countof()
宏,它為您執行上述計算,並且如果參數是一個指針(如動態分配的數組),則安全地中斷編譯過程(因為在這種情況下,上面的公式給出了不正確的結果,即指針的大小 - 固定的,例如32位系統上的4個字節,與指向的數組大小無關 - 除以第一個指向項的大小)。
#include <cstddef>
template<class T, std::size_t N>
std::size_t length_of( T(&)[N] ) {
return N;
}
是一個解決您的問題的模板。
C ++ 11中更具工業級的版本是:
#include <array>
template<class T, std::size_t N>
constexpr std::size_t size( T(&)[N] ) {
return N;
}
template<class T, std::size_t N>
constexpr std::size_t size( std::array<T,N> const& ) {
return N;
}
template<class C,class=void>
struct has_size : std::false_type {};
template<class>using void_t=void;
template<class C, void_t< decltype( std::declval<C&>().size() ) > >
struct has_size : std::true_type {};
template<bool b, class T=void>using enable_if_t=typename std::enable_if<b,T>::type;
template<class C, class=enable_if_t< has_size<C>::value, bool[1] >>
constexpr std::size_t size( C const& c ) {
return c.size();
}
template<class C, class=enable_if_t< !has_size<C>::value, bool[2] >>
constexpr std::size_t size( C const& c ) {
using std::begin; using std::end;
return end(c)-begin(c);
}
這可能太過分了。 請注意,缺少大小的容器中的非隨機訪問迭代器可能無法編譯(因為-
通常沒有定義),這類似於purpoes(我們不想意外地執行O(n)工作)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.