array of vectors

Mar 22, 2021 at 1:53pm
I want to create an array of vectors, in each vector there are stored integers. Here in the code the hashBucket and table are defined. But i dont know how to write the constructor and initialize the array with empty vectors. Could someone help me out?

thank you!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;


class myHashTable {
    static const uint32_t hashBucket = 4294967295;
    static std::vector<int> table[hashBucket];
    
    public:
    myHashTable() {
        for (uint32_t i=0; i< hashBucket; i++) {
            table[i] = vector<int>();
        }
    }
};


int main()
{
    myHashTable mh;
  
    return 0;
}
Mar 22, 2021 at 2:39pm
You need to define table outside the class:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <cmath>
#include <vector>

using namespace std;


class myHashTable {
    static const uint32_t hashBucket = 4294967295;
    static std::vector<int> table[hashBucket];
    
    public:
    myHashTable() {
        for (uint32_t i=0; i< hashBucket; i++) {
            table[i] = vector<int>();
        }
    }
};

std::vector<int> myHashTable::table[hashBucket];

int main()
{
    myHashTable mh;
  
    return 0;
}
Note that the array may be too large to comile into an executable.
Mar 22, 2021 at 2:42pm
A vector will already be initialized by default to be empty, so your for loop is not necessary.
The bigger issues are:
(1) non-const static variables need to be initialized outside of the class itself (or with the inline keyword starting with C++17)
(2) You are trying to create static, contiguous memory for 4,294,967,295 ints. This is HUGE, like 16 GB of contiguous memory needed. It doesn't even build for me because there isn't room in bss (zero-init'd memory spot).
Mar 22, 2021 at 3:01pm
Compiling with VS2019 as 64bit, get "error C2148: total size of array must not exceed 0x7fffffff bytes"

Mar 22, 2021 at 3:10pm
Visual Studio 2019's Intellisense flags an error without even trying to do a compile. The error is (for MS) rather simple and understandable:

E0095 array is too large
Topic archived. No new replies allowed.