std::vector::resizeメンバ関数は次のようである:64ビットプラットフォームでsize_typeが32ビット整数になるのはなぜですか?
ボイドサイズ変更(size_type n)が、私の理解パー
、size_type
は64-bit
プラットフォーム上64-bit long
タイプでなければなりません。
#include <iostream>
#include <climits>
#include <vector>
using namespace std;
vector<char> v;
int main() {
// your code goes here
v.resize(INT_MAX +1);
for (auto i = 0; i < v.size(); i++) {
cout << i << endl;
}
return 0;
}
次の警告が生成される:
g++ -std=c++11 hello.cpp
hello.cpp: In function ‘int main()’:
hello.cpp:11:19: warning: integer overflow in expression [-Woverflow]
v.resize(INT_MAX +1);
のでsize_type
が32-bit int
我々は64-bit
プラットフォーム上で作業しているにもかかわらず、まだですが、次のプログラムをコンパイルしますか?
ベクトルの 'size_type'は、符号なしである必要はありません。 – StoryTeller
[this](https://stackoverflow.com/questions/4849632/vectorintsize-type-in-c)と[this](http://en.cppreference.com/w/cpp/types/)をご覧ください。 size_t)。 – hnefatl
ここでINT_MAX +1がUBを呼び出しました。 – rsp