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

C++ for 循環(huán) - size_type 與 size_t

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

問題描述

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

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

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

謝謝.

解決方案

C++ 標(biāo)準(zhǔn)說,

<塊引用>

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

然后補充,

<塊引用>

容器的實現(xiàn)在本國際中描述的標(biāo)準(zhǔn)允許假設(shè)他們的分配器模板參數(shù)滿足以下兩個附加條件超出表 32 中的要求.

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

所以很可能,size_typesize_t 的 typedef.

標(biāo)準(zhǔn)真正將其定義為,

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

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

  • size_typeunsigned 整數(shù),而 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.

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

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

相關(guān)文檔推薦

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;(比較運算符的數(shù)學(xué)式鏈接-如“if((5<j<=1)))
Difference between quot;if constexpr()quot; Vs quot;if()quot;(“if constexpr()之間的區(qū)別與“if())
C++, variable declaration in #39;if#39; expression(C++,if 表達(dá)式中的變量聲明)
主站蜘蛛池模板: 影视一区| 国产最新视频在线 | 日韩三级免费观看 | 一区二区三区久久 | 亚洲成人在线免费 | 黄色免费av| 中文字幕日韩欧美一区二区三区 | 欧美区日韩区 | 97免费在线观看视频 | 国产精品一区二区三区久久久 | 国产一区二区三区四区 | 免费在线观看成年人视频 | 91就要激情| 蜜桃综合在线 | 中文字幕第7页 | 一级毛片网 | 99精品视频一区二区三区 | 欧美日韩在线精品 | 亚洲国产中文字幕 | 亚洲欧洲精品一区 | 羞羞视频网页 | 亚洲成人av在线播放 | 操皮视频 | 中文字幕一区在线 | 亚州精品成人 | 一区日韩| 韩日精品视频 | 五月综合激情在线 | 久久伊人免费视频 | 亚洲一区二区视频在线播放 | 亚洲精品2 | 欧美在线观看黄色 | 欧美九九九 | 亚洲成人自拍 | 免费亚洲视频 | 亚洲欧美成人影院 | 久久久久国产 | 日日操夜夜干 | 99精品久久久久久 | 亚洲九色 | 成人性视频免费网站 |