C++语言中怎么通过字符串中的struct name获取到struct大小
可以使用sizeof运算符来获取结构体的大小,例如:
#include <iostream>
#include <string>
using namespace std;
struct Student {
string name;
int age;
};
int main() {
string structName = "Student";
if (structName == "Student") {
cout << "Size of Student struct is: " << sizeof(Student) << endl;
}
return 0;
}
在上面的例子中,我们定义了一个名为Student的结构体,它包含一个字符串类型的成员变量name和一个整型的成员变量age。然后,我们通过字符串structName来判断需要获取哪个结构体的大小,如果是"Student",则使用sizeof运算符获取Student结构体的大小并输出。
原文地址: https://www.cveoy.top/t/topic/cJWR 著作权归作者所有。请勿转载和采集!