89

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");
4

5 回答 5

110

创建向量 push_back 元素,然后将其修改为:

struct subject {
    string name;
    int marks;
    int credits;
};


int main() {
    vector<subject> sub;

    //Push back new subject created with default constructor.
    sub.push_back(subject());

    //Vector now has 1 element @ index 0, so modify it.
    sub[0].name = "english";

    //Add a new element if you want another:
    sub.push_back(subject());

    //Modify its name and marks.
    sub[1].name = "math";
    sub[1].marks = 90;
}

在该索引处的向量中存在元素之前,您无法使用 [#] 访问向量。此示例填充 [#],然后对其进行修改。

于 2011-11-09T15:39:15.327 回答
61

If you want to use the new current standard, you can do so:

sub.emplace_back ("Math", 70, 0);

or

sub.push_back ({"Math", 70, 0});

These don't require default construction of subject.

于 2011-11-09T18:53:42.707 回答
18

对于此类情况,您还可以使用括号初始化列表中的聚合初始化。

#include <vector>
using namespace std;

struct subject {
    string name;
    int    marks;
    int    credits;
};

int main() {
    vector<subject> sub {
      {"english", 10, 0},
      {"math"   , 20, 5}
    };
}

然而,有时结构的成员可能并不那么简单,因此您必须让编译器帮助推断其类型。

所以延伸到上面。

#include <vector>
using namespace std;

struct assessment {
    int   points;
    int   total;
    float percentage;
};

struct subject {
    string name;
    int    marks;
    int    credits;
    vector<assessment> assessments;
};

int main() {
    vector<subject> sub {
      {"english", 10, 0, {
                             assessment{1,3,0.33f},
                             assessment{2,3,0.66f},
                             assessment{3,3,1.00f}
                         }},
      {"math"   , 20, 5, {
                             assessment{2,4,0.50f}
                         }}
    };
}

如果没有大assessment括号初始化程序中的 ,编译器在尝试推断类型时将失败。

以上已经用c++17中的gcc编译测试过了。但是,它应该从 c++11 及更高版本开始工作。在 c++20 中,我们可能会看到指示符语法,我希望它允许以下内容

  {"english", 10, 0, .assessments{
                         {1,3,0.33f},
                         {2,3,0.66f},
                         {3,3,1.00f}
                     }},

来源:http ://en.cppreference.com/w/cpp/language/aggregate_initialization

于 2018-04-29T10:00:36.150 回答
13

您不能通过下标访问空向量的元素。在使用运算符时,
请始终检查向量是否为空且索引是否有效。如果不存在元素,则不会添加元素,但如果索引无效,则会导致未定义行为。[]std::vector
[]

您应该创建结构的临时对象,将其填充,然后将其添加到向量中,使用vector::push_back()

subject subObj;
subObj.name = s1;
sub.push_back(subObj);
于 2011-11-09T15:34:48.147 回答
3

查看接受的答案后,我意识到如果知道所需向量的大小,那么我们必须使用循环来初始化每个元素

但我发现新的使用 default_structure_element 来做到这一点,如下所示......

#include <bits/stdc++.h>
typedef long long ll;
using namespace std;

typedef struct subject {
  string name;
  int marks;
  int credits;
}subject;

int main(){
  subject default_subject;
  default_subject.name="NONE";
  default_subject.marks = 0;
  default_subject.credits = 0;

  vector <subject> sub(10,default_subject);         // default_subject to initialize

  //to check is it initialised
  for(ll i=0;i<sub.size();i++) {
    cout << sub[i].name << " " << sub[i].marks << " " << sub[i].credits << endl;
  } 
}

然后我认为它是初始化结构向量的好方法,不是吗?

于 2018-07-14T06:22:25.117 回答