如何初始化用于数组大小的常量int? [英] How to initialize a constant int to use for array size?

查看:100
本文介绍了如何初始化用于数组大小的常量int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个静态整数变量 Game :: numPlayers ,它是用户输入的内容.然后,我将以下类定义为:

I have a static integer variable Game::numPlayers, which is read in as an input from user. I then have the following class defined as such:

class GridNode
{
private:
    /* Members */
    static const int nPlayers = Game::numPlayers;
    std::vector<Unit> units[nPlayers];

    //...
};

这将不会编译(静态数据成员的类初始化器不是常量表达式").

This won't compile ("in-class initializer for static data member is not a constant expression").

我显然不能只分配 Game :: numPlayers 的数组大小,而且我还尝试不初始化它并让构造函数来工作,但这也不起作用.

I obviously cant just assign the array size of Game::numPlayers, and I also tried not initializing it and letting a constructor do the work, but that didn't work either.

我不明白我在这里做错了什么,还有我可能做些什么才能使它按预期工作.

我只是复制一个值,它与 static const int nPlayers = 8 (将值8复制到 nPlayers 并正常工作)有什么不同?

I'm just copying a value, how is that any different from static const int nPlayers = 8 which copies the value 8 into nPlayers and works?

为了明确起见,我选择有一个向量数组,因为我希望每个节点都有一个快速访问单位的容器,但是每个用户/玩家一个容器,以便区分哪些单位属于每个节点内的哪个玩家(例如,数组的索引0 =播放器1,索引1 =播放器2,索引2 =播放器3,依此类推),否则我将只有一个向量或向量的向量.我以为地图可以工作,但我想以向量为单位的数组可以更快地访问和推入.

To clarify, I choose to have an array of vectors because I want each node to have a quick-access container of units, but one container for each user/player so as to distinguish which units belong to which player within each node (e.g. index 0 of the array = player 1, index 1 = player 2, index 2 = player 3, and so on), otherwise I would just have one vector or a vector of vectors. I thought a map might work, but I thought an array of vectors would be faster to access and push into.

此外, Game :: numPlayers 是作为用户输入读取的,但是在一个游戏循环内只能读取和分配一次,但是如果我关闭/重新启动/玩新游戏,则需要再次读入用户输入并分配一次.

Also, Game::numPlayers is read in as a user input, but only read and assigned once within one game loop, but if I close/restart/play a new game, it needs to read in the user input again and assign it once.

推荐答案

我不明白为什么要在 std :: vector 数组中获取元素的数量,为什么运行时.

I don't see why you need to use an array of std::vector if the number of elements will be obtained at runtime.

相反,创建一个 std :: vector< std :: vector< Units>> 并在构造时适当调整其大小.如果需要重置大小,请使用成员函数调整向量大小.

Instead, create a std::vector<std::vector<Units>> and size it appropriately on construction. if you need to reset the size, have a member function resize the vector.

示例:

class GridNode
{
    private:
        /* Members */
        std::vector<std::vector<Unit>> units;

    public:
        GridNode(int nPlayers=10) : units(nPlayers) {}

        std::vector<Unit>& get_units(size_t player) 
        { return units[player]; }  // gets the units for player n

        void set_num_players(int nPlayers) 
        {  units.resize(nPlayers); }  // set the number of players

        int get_num_players() const { return units.size(); }
 };

 int main()
 {
     int numPlayers;
     cin >> numPlayers;
     //...
     GridNode myGrid(numPlayers); // creates a GridNode with 
                                    // numPlayers vectors.  
     //...
     Unit myUnit;
     myGrid.get_units(0).push_back(myUnit); // places a Unit in the 
                                            // first player
 }

此外,让无关的变量告诉您​​向量的大小也不是一个好主意.通过调用 vector :: size()函数, vector 已经知道其自身的大小.携带不必要的变量(可能会为您提供此信息)打开自己的bug.

Also, it isn't a good idea to have extraneous variables tell you the vector's size. The vector knows its own size already by calling the vector::size() function. Carrying around unnecessary variables that supposedly gives you this information opens yourself up for bugs.

这篇关于如何初始化用于数组大小的常量int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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