C ++でのsizeofについて考える

テスト手順は次のとおりです。

#include <iostream>
#include <vector>
#include <string>
#include <cstddef>
using namespace std;

int main()
{
    
    
	string word1 = "Teddy";
	string word2 = "Bear";
	vector<int> vec1{
    
     0,1,2,3,4 };
	vector<int> vec2{
    
     0,1,2 };
	vector<string> vec3 = {
    
     "Teddy","Bear" };
	cout << "size of int is " << sizeof(int) << ".\n";
	cout << "size of long is " << sizeof(long) << ".\n";
	cout << "size of long long is " << sizeof(long long) << ".\n";
	cout << "size of short is " << sizeof(short) << ".\n";
	cout << "size of char is " << sizeof(char) << ".\n";
	cout << "size of 'word1' is " << sizeof word1 << ".\n";
	cout << "size of 'word2' is " << sizeof word2 << ".\n";
	cout << "size of 'vec1' is " << sizeof vec1 << ".\n";
	cout << "size of 'vec2' is " << sizeof vec2 << ".\n";
	cout << "size of 'vec3' is " << sizeof vec3 << ".\n";
	return 0;
}

私のコンピューターの出力は次のとおりです
出力
(各コンピューターは必ずしも同じである必要はありません)
既知の法則によると、sizeof文字列の値は、格納されているコンテンツとは関係ありません
配列のsizeofは、このプログラムにはリストされていません。配列の各要素のサイズに数値が乗算されます。


【C ++入門(第5版)演習】演習プログラム-第4章(第4章)

おすすめ

転載: blog.csdn.net/weixin_50012998/article/details/108257791