久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

C++ for 循環 - size_type 與 size_t

C++ for-loop - size_type vs. size_t(C++ for 循環 - size_type 與 size_t)
本文介紹了C++ for 循環 - size_type 與 size_t的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

C++ Primer 一書的第 (3) 章中,有以下 for 循環將向量中的元素重置為零.

for (vector::size_type ix = 0; ix ! = ivec.size(); ++ix)ivec[ix] = 0;

為什么使用 vector::size_type ix = 0?我們不能說 int ix = 0 嗎?在第二種形式上使用第一種形式有什么好處?

謝謝.

解決方案

C++ 標準說,

<塊引用>

 size_type |無符號整數類型 |可以表示最大對象的大小的類型分配模型

然后補充,

<塊引用>

容器的實現在本國際中描述的標準允許假設他們的分配器模板參數滿足以下兩個附加條件超出表 32 中的要求.

  • typedef 成員指針、const_pointer、size_type 和差異類型是需要分別為 T*、T const*、size_t 和 ptrdiff_t

所以很可能,size_typesize_t 的 typedef.

標準真正將其定義為,

template 類分配器{上市:typedef size_t size_type;//…………};

所以需要注意的最重要的幾點是:

  • size_typeunsigned 整數,而 int 不是 必然 未簽名.:-)
  • 它可以表示最大的索引,因為它是無符號的.

In the C++ Primer book, Chapter (3), there is the following for-loop that resets the elements in the vector to zero.

for (vector<int>::size_type ix = 0; ix ! = ivec.size(); ++ix)
ivec[ix] = 0;

Why is it using vector<int>::size_type ix = 0? Cannot we say int ix = 0? What is the benefit of using the first form on the the second?

Thanks.

解決方案

The C++ Standard says,

 size_type  |  unsigned integral type  |  a type that can represent the size of the largest object in the
allocation model

Then it adds,

Implementations of containers described in this International Standard are permitted to assume that their Allocator template parameter meets the following two additional requirements beyond those in Table 32.

  • The typedef members pointer, const_pointer, size_type, and difference_type are required to be T*,T const*, size_t, and ptrdiff_t, respectively

So most likely, size_type is a typedef of size_t.

And the Standard really defines it as,

template <class T> 
class allocator 
{
   public:
       typedef size_t size_type;
       //.......
};

So the most important points to be noted are :

  • size_type is unsigned integral, while int is not necessarily unsigned. :-)
  • it can represent the largest index, because it's unsigned.

這篇關于C++ for 循環 - size_type 與 size_t的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

What do compilers do with compile-time branching?(編譯器如何處理編譯時分支?)
Can I use if (pointer) instead of if (pointer != NULL)?(我可以使用 if (pointer) 而不是 if (pointer != NULL) 嗎?)
Checking for NULL pointer in C/C++(在 C/C++ 中檢查空指針)
Math-like chaining of the comparison operator - as in, quot;if ( (5lt;jlt;=1) )quot;(比較運算符的數學式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達式中的變量聲明)
主站蜘蛛池模板: 黄色av网站在线观看 | 91看片在线观看 | 黄片毛片在线观看 | 国产亚洲欧美在线 | www.九色| 小视频黄色 | 免费看黄色av | 99视频在线精品免费观看2 | 国产精品三 | 欧美精品黄色 | 青草网| 在线观看黄色小视频 | 91av视频在线 | 欧美视频在线一区 | 一区二区三区在线观看免费 | 欧洲av在线 | 亚洲精品1| 婷婷久久综合 | 免费av网站在线观看 | 欧美视频在线一区 | av免费观看网址 | 久久久黄色片 | 日韩福利视频 | 一区在线播放 | 国产不卡在线视频 | av网站免费看 | 国产一级视频在线观看 | 国产精品欧美精品 | 国产黄av| 国产精品久久久久久久久久久久久 | 日韩中文视频 | 欧美日韩性| 中文字幕第三页 | 欧美一区二区免费 | 美日韩精品 | 一级黄色片视频 | 日韩在线不卡视频 | 日韩色在线 | 中文字幕日韩欧美 | 视频一区在线播放 | 久久久国|