简体   繁体   English

在类中声明向量大小

[英]Declaring vector size in a class

I am supposed to write a program that simulates a dice(6 faced) roll 6000 times and stores the results in a vector. 我应该编写一个程序,模拟骰子(6面)掷骰6000次并将结果存储在向量中。 For example if the dice roll returns 1, I would do something like frequency.at(0)++. 例如,如果掷骰子返回1,我将执行诸如frequency.at(0)++之类的操作。 Since the size of the vector is gonna be fixed, and I would also need to able to access each element freely, I was wondering if there was anyway to declare the size of the vector using a default constructor or something. 由于向量的大小将是固定的,并且我还需要能够自由访问每个元素,因此我想知道是否有使用默认构造函数或其他方法声明向量的大小的方法。 This is what I currently have but I get a "too many arguments in function call" and "expression must have class type" error. 这是我目前拥有的,但是出现“函数调用中的参数过多”和“表达式必须具有类类型”错误。 Maybe what I'm trying to do is not possible, I don't know, but just looking for some help. 我可能不知道我想做的事,只是寻求帮助。 Thanks. 谢谢。

My header file: 我的头文件:

#ifndef AHISTOGRAM_H
#define AHISTOGRAM_H


class aHistogram
{
public:
    aHistogram();
    ~aHistogram();
    void update(int face);
    void display(int maxLengthOfLine);
    void clear() const;
    int count(int face);
private:
    vector<int> numRolls();
    int numx, m, j;
};

#endif

aHistogram.cpp: aHistogram.cpp:

#include <iostream>
#include <cstdlib>
#include <vector>
#include "aHistogram.h"
using namespace std;

aHistogram::aHistogram()
{
    numRolls(6);
    numx, m, j = 0;
}

aHistogram::~aHistogram()
{
}

void aHistogram::update(int face)
{
    numRolls.at(face - 1)++;
    return;
}

This is what the constructor's initializer list is for: 这是构造函数的初始化列表的用途:

aHistogram::aHistogram(): numRolls(6), numx(0), m(0), j(0) // constructor parameters here
{
    // numRolls(6);
    // numx m, j = 0;
}

Also the declaration of your vector is wrong in your class definition: 同样,向量的声明在类定义中是错误的:

class aHistogram
{
public:
    aHistogram();
    ~aHistogram();
    void update(int face);
    void display(int maxLengthOfLine);
    void clear() const;
    int count(int face);
private:
    // vector<int> numRolls(); // this is declaring a function!
    vector<int> numRolls; // USE THIS!!
    int numx, m, j;
};

IF the size of the "vector" is fixed, then using std::array is most certainly a better option, unless of course, you are using an ancient compiler which doesn't support C++11. 如果“向量”的大小是固定的,那么使用std::array无疑是一个更好的选择,当然,除非您使用的是不支持C ++ 11的古老编译器。

Go through the above link on cppreference.com. 通过cppreference.com上的上述链接。 For most part, it is just like an "old fashioned" array, but it also comes with benefits such as bounds checking (if you use at() instead of operator[] ), ability to iterate through elements ( begin() , end() , etc.), and many other "benefits" of std::vector . 在大多数情况下,它就像一个“老式”数组,但是它还具有边界检查(如果使用at()而不是operator[] ),遍历元素的能力( begin()end()等),以及std::vector许多其他“好处”。

Have a look at this: 看看这个:

#include <iostream>
#include <array>
using namespace std;

class aHistogram {
public:
    aHistogram() : numRolls{0, 0, 0, 0, 0, 0} {}

    int count(int face) {
        return numRolls.at(face - 1);
    }

    void update(int face) {
        numRolls.at(face - 1)++;
    }

private:
    array<int, 7> numRolls;
};

int main() {
    aHistogram ah;
    ah.update(1);
    ah.update(2);
    ah.update(1);

    cout << "Count[1]: " << ah.count(1) << " Count[2]: " << ah.count(2) << endl;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM