结构初始化向量
我想知道如何使用 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");
I want know how I can add values to my vector of structs using the push_back
method
struct subject
{
string name;
int marks;
int credits;
};
vector<subject> sub;
So now how can I add elements to it?
I have function that initializes string name(subject name to it)
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);
}
Function call
setName("english", "math", "physics" ... "economics");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于此类情况,您还可以使用支撑初始化列表中的聚合初始化。
然而,有时,结构体的成员可能并不那么简单,因此您必须帮助编译器推断其类型。
所以在上面的基础上进行扩展。
如果没有花括号初始化器中的
评估
,编译器在尝试推导类型时将会失败。以上已在c++17中用gcc编译并测试。然而,它应该从 c++11 及更高版本开始工作。在 c++20 中,我们可能会看到指示符语法,我希望它将允许以下
源: http://en.cppreference.com/w/cpp/language/aggregate_initialization
You may also which to use aggregate initialization from a braced initialization list for situations like these.
Sometimes however, the members of a struct may not be so simple, so you must give the compiler a hand in deducing its types.
So extending on the above.
Without the
assessment
in the braced initializer the compiler will fail when attempting to deduce the type.The above has been compiled and tested with gcc in c++17. It should however work from c++11 and onward. In c++20 we may see the designator syntax, my hope is that it will allow for for the following
source: http://en.cppreference.com/w/cpp/language/aggregate_initialization
您无法通过下标访问空向量的元素。
始终检查向量是否为空&在
std::vector
上使用[]
运算符时,索引有效。如果不存在元素,
[]
不会添加元素,但如果索引无效,则会导致未定义行为。您应该创建一个结构的临时对象,填充它,然后使用
vector::push_back()
将其添加到向量中You cannot access elements of an empty vector by subscript.
Always check that the vector is not empty & the index is valid while using the
[]
operator onstd::vector
.[]
does not add elements if none exists, but it causes an Undefined Behavior if the index is invalid.You should create a temporary object of your structure, fill it up and then add it to the vector, using
vector::push_back()
在查看了接受的答案之后,我意识到,如果知道所需向量的大小,那么我们必须使用循环来初始化每个元素,
但我发现新的方法可以使用 default_struct_element 来执行此操作,如下所示...
然后我认为初始化一个元素是很好的方法结构体的向量,不是吗?
After looking on the accepted answer I realized that if know size of required vector then we have to use a loop to initialize every element
But I found new to do this using default_structure_element like following...
Then I think its good to way to initialize a vector of the struct, isn't it?
创建向量、push_back 元素,然后按如下方式修改它:
在该索引处的向量中存在元素之前,您无法使用 [#] 访问向量。此示例填充 [#],然后对其进行修改。
Create vector, push_back element, then modify it as so:
You cant access a vector with [#] until an element exists in the vector at that index. This example populates the [#] and then modifies it afterward.
如果您想使用新的现行标准,您可以这样做:
或
。
If you want to use the new current standard, you can do so:
or
.