【发布时间】:2021-07-24 08:09:59
【问题描述】:
我有一个名为 Person 的结构:
Person
{
int id;
int age;
int salary;
};
所以我想在以下形式的一行中使用标准输入输入任意数量的 Person:
[id, age, salary] [id, age, salary] ...
所以我确实写了这段代码,但它没有给我我想要的东西:
std::vector<Person> person;
int y = scanf("%i", &num);
for (int i = 0; i < num; i++)
{
Person p;
int r = scanf("[%i,%i,%i]", &p.id, &p.age, &p.salary);
person.push_back(p);
}
如何更正我的代码以满足我的需要?
【问题讨论】:
-
r的值是多少,为什么要使用scanf? -
谷歌
std::cin -
因为我想要以下格式 [ , , ]
-
嗯,有很多方法:stackoverflow.com/questions/51977400/…stackoverflow.com/questions/38672532/…stackoverflow.com/questions/24276482/…,从检查
scanf返回的值开始,即成功转换和分配的字段数。