如何在C ++中声明和初始化2d int向量? [英] How do I declare and initialize a 2d int vector in C++?

查看:110
本文介绍了如何在C ++中声明和初始化2d int向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要做的事情:

#include <iostream>
#include <vector>
#include <ctime>

class Clickomania
{
    public:
        Clickomania();
        std::vector<std::vector<int> > board;
        bool move(int, int);
        bool isSolved();
        void print();
        void pushDown();
};

Clickomania::Clickomania()
    : board(12, std::vector<int>(8,0))
{

    srand((unsigned)time(0));

    for(int i = 0; i < 12; i++)
    {
        for(int j = 0; j < 8; j++)
        {
            int color = (rand() % 6) + 1;
            board[i][j] = color;
        }
    }
}

如何创建2d向量类型的公共成员并正确初始化?

How can I create a public member of a 2d vector type and initialize it properly?

推荐答案

使用g ++编译代码,我得到的错误是 srand() code> rand()已声明。我必须添加 #include< cstdlib> 才能编译代码。但一旦我这样做,它的工作很好。所以,我会说,除了添加include语句,你的代码是好的。您正在正确初始化向量。

Compiling your code with g++, the error I get is that neither srand() nor rand() were declared. I had to add #include <cstdlib> for the code to compile. But once I did that, it worked just fine. So, I'd say that other than adding that include statement, your code is fine. You're initializing the vector correctly.

也许您拥有的代码与您发布的代码不完全匹配?我会假设,如果你的实际代码不包括cstdlib,你会很快就明白,这是问题,而不是与向量的东西。所以,如果你的代码不太匹配你发布,也许这是问题。如果没有,你使用什么编译器?

Perhaps the code you have doesn't quite match what you posted? I would assume that if your actual code didn't include cstdlib, that you would have quickly understood that that was the problem rather than something with vector. So, if your code doesn't quite match what you posted, maybe that's the problem. If not, what compiler are you using?

这篇关于如何在C ++中声明和初始化2d int向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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