by 1100++ » 30 Dec 2019, 18:05
I'm trying to write code where I point to a vector rather than directly include it in a struct. How would I access the elements of the vector pointed to in this way? Here's some code to give an idea of what I'm trying to do.
Code: Select all
#include <iostream>
#include <vector>
typedef struct {
__int64 size;
vector<__int64>* numbers;
} num_array;
int main() {
num_array array;
// I know how to do this.
array.numbers->resize(5);
// I don't know how to do this. These both return errors.
array.numbers[0] = 5;
*(array.numbers)[0] = 5;
cout << array.numbers[0];
return 0;
}
I'm trying to write code where I point to a vector rather than directly include it in a struct. How would I access the elements of the vector pointed to in this way? Here's some code to give an idea of what I'm trying to do.
[Codebox=cpp file=Point to Vector.cpp]#include <iostream>
#include <vector>
typedef struct {
__int64 size;
vector<__int64>* numbers;
} num_array;
int main() {
num_array array;
// I know how to do this.
array.numbers->resize(5);
// I don't know how to do this. These both return errors.
array.numbers[0] = 5;
*(array.numbers)[0] = 5;
cout << array.numbers[0];
return 0;
}
[/Codebox]