如何初始化一个将结构体作为值的映射? [英] How do you initialize a map which takes a struct as value?

查看:368
本文介绍了如何初始化一个将结构体作为值的映射?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用地图作为ID - > value的关联数组,其中值是定义对象的结构:

I am using a map as an associative array of IDs -> value, where the value is a struct defining the object:

#include <map>

struct category {
        int id;
        std::string name;
};

std::map<int, category> categories;

int main() {
        categories[1] = {1, "First category"};
        categories[2] = {2, "Second category"};

}

上述代码使用g ++编译,但出现以下警告:

The above code compiles with g++, but with the following warning:

warning: extended initializer lists only available with -std=c++0x or -std=gnu++0x



我在这里阅读了关于结构初始化的各种问题/答案,但我还是有点困惑。我有一系列相关问题:

I have read various questions/answers here about struct initialization, but I'm still a bit confused. I have a series of related questions:


  1. 我可以添加编译选项-std = c ++ 0x,警告,但仍然没有更聪明的根本问题。如果我将一个方法添加到类别struct?不会中断事件。

  1. I could add the compiler option -std=c++0x and be done with the warning, but still be none the wiser about the underlying problem. Wouldn't things break if I add a method to the category struct?

最好的方法是在更多C ++ 03兼容的方式?

What would the best way be to initialize this POD struct (category) in a more C++03 compliant way?

基本上,我还不确定以一种方式而不是另一种方式做事情的后果。这种关联数组(其中键是对象的ID)使用PHP很容易,我仍然在学习在C ++中做正确的方法。在上述代码的上下文中,我应该注意什么?

Basically, I am not yet sure of the consequences of doing things one way rather than another way. This kind of associative array (where the key is the ID of an object) is easy with PHP, and I'm still learning about the proper way to do it in C++. Is there anything I should pay attention to in the context of the code above?

>

以下问题是相关的,但是我在第一次阅读时却无法理解答案:

C ++初始化匿名结构

c ++使用数组作为成员初始化结构

在C ++中初始化结构

推荐答案

14882:2003),可以使用括号括起的表达式列表来初始化定义它的声明中的 aggregate 类型的变量。

In C++ (ISO/IEC 14882:2003), a brace enclosed list of expressions can be used to initialize a variable of aggregate type in the declaration that defines it.

例如

struct S { int a; std::string b; };

S x = { 39, "Hello, World\n" };

一个聚集类型是一个数组或没有用户声明的类构造函数,没有私有或受保护的非静态数据成员,没有基类,没有虚函数。请注意,类聚合不一定是POD类,并且任何数组都是聚合,无论它是一个数组的类型是否是聚合。

An aggregate type is an array or a class with no user-declared constructors, no private or protected non-static data members, no base classes, and no virtual functions. Note that a class aggregate doesn't have to be a POD-class and any array is an aggregate whether or not the type that it is an array of is an aggregate.

但是,括号括起的表达式列表仅作为聚合的初始化器有效,在其他上下文中通常不允许使用,例如赋值或类构造函数的成员初始化列表。

However, a brace-enclosed list of expressions is only valid as an initializer for an aggregate, it is not generally allowed in other contexts such as assignment or a class constructor's member initialization list.

在下一个版本的C ++(C ++ 0x)的当前草案中,括号括起来的表达式列表允许在更多的上下文中,当从这样的初始化列表初始化对象时,它被称为列表初始化

In the current draft of the next version of C++ (C++0x), a brace enclosed list of expressions (brace-init-list) is allowed in more contexts and when an object is initialized from such an initializer list it is called list-initialization.

允许这种列表的新上下文包括函数调用中的参数,函数返回,构造函数的参数,成员和基本初始值设置以及作业右侧。

New contexts where such a list is allowed include arguments in a function call, function returns, arguments to constructors, member and base initializers and on the right hand side of an assignment.

这意味着这在C ++ 03中无效。

This means that this is not valid in C++03.

int main() {
        categories[1] = {1, "First category"};
        categories[2] = {2, "Second category"};
}

而是可以这样做。

int main() {
        category tmp1 = { 1, "First category" };
        category tmp2 = { 2, "Second category" };

        categories[1] = tmp1;
        categories[2] = tmp2;
}

或者。

int main() {
        category tmpinit[] = { { 1, "First category" },
                               { 2, "Second category" } };
        categories[1] = tmpinit[0];
        categories[2] = tmpinit[1];
}

或者,你可以考虑为你的类型做一个工厂函数。 (你可以为你的类型添加一个构造函数,但这会使你的类是非聚合的,并且会阻止你在其他地方使用聚合初始化。)

Or, you could consider making a factory function for your type. (You could add a constructor for your type but this would make your class a non-aggregate and would prevent you from using aggregate initialization in other places.)

category MakeCategory( int n, const char* s )
{
    category c = { n, s };
    return c;
}

int main()
{
    categories[1] = MakeCategory( 1, "First category" );
    categories[2] = MakeCategory( 2, "Second category" );
}

这篇关于如何初始化一个将结构体作为值的映射?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆