我想知道如何使用该push_back
方法将值添加到结构向量中
struct subject
{
string name;
int marks;
int credits;
};
vector<subject> sub;
那么,现在如何向其中添加元素?
我有初始化字符串名称的函数(以它的主题名称为准)
void setName(string s1, string s2, ...... string s6)
{
// how can i set name too sub[0].name= "english", sub[1].name = "math" etc
sub[0].name = s1 // gives segmentation fault; so how do I use push_back method?
sub.name.push_back(s1);
sub.name.push_back(s2);
sub.name.push_back(s3);
sub.name.push_back(s4);
sub.name.push_back(s6);
}
函数调用
setName("english", "math", "physics" ... "economics");