也许您使用 sscanf 是有充分理由的,但至少我认为可以使用流将信息加载到结构中是一件好事。
在这种情况下,我建议您使用 istringstream 类,它可以让您从字符串中读取值作为值,并根据需要进行转换。所以,你的代码,我想我可以把它改成这样:
std::string one = "v 100.32 12321.232 3232.6542";
struct Face {float x,y,z;};
std::vector<struct Face>obj;
char space[3];
// As mentioned previously, create a temporal Face variable to load the info
struct Face tmp; // The "struct" maybe can be omited, I prefer to place it.
// Create istringstream, giving it the "one" variable as buffer for read.
istringstream iss ( one );
// Replace this line...
//sscanf(one.c_str(), "%s %f %f %f",space,&obj[1].x1,&obj[1].y1,&obj[1].z1);
// With this:
iss >> space >> tmp.x >> tmp.y >> tmp.z;
// Add the temporal Face into the vector
obj.push_back ( tmp );
// As mentioned above, the first element in a vector is zero, not one
std::cout << obj[0].x1 << std::endl;
istringstream 类(您需要包含“sstream”)在这种情况下很有用,当您有要从字符串加载的值时。
我希望我的回答能以任何方式帮助你。