C++中sizeof返回对象的所占空间的字节数。
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string strArr1[] = {"Trend", "Micro", "soft"};
string *p = new string[2];
p[0]= "US";
p[1]= "CN";
cout << sizeof(strArr1) <<endl;
cout << sizeof(p) <<endl;
cout << sizeof(string) <<endl;
for(int i = 0; i < sizeof(strArr1)/sizeof(string); i++) {
cout << strArr1[i];
}
for(int i = 0; i < 2; i++) {
cout << *p++;
}
cout <<endl;
sizeof(string) == 4;
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
string strArr1[] = {"Trend", "Micro", "soft"};
string *p = new string[2];
p[0]= "US";
p[1]= "CN";
cout << sizeof(strArr1) <<endl; // 12
cout << sizeof(p) <<endl; // 4
cout << sizeof(string) <<endl; //4
int len = sizeof(strArr1) / sizeof(string);
for(int i = 0; i < len; i++) {
cout << strArr1[i];
}
for(int i = 0; i < 2; i++) {
cout << *p++;
}
cout <<endl;
}