0% found this document useful (0 votes)
48 views5 pages

Array of Vector Pointers

The document discusses how to create an array of vector pointers for the struct AdvancedButtonPress3 in C++. It explains that references cannot be used in arrays, and suggests using pointers or std::reference_wrapper instead. The document also clarifies the differences between pointers to vectors and vectors of pointers, providing correct code examples for both approaches.

Uploaded by

Bret Hart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views5 pages

Array of Vector Pointers

The document discusses how to create an array of vector pointers for the struct AdvancedButtonPress3 in C++. It explains that references cannot be used in arrays, and suggests using pointers or std::reference_wrapper instead. The document also clarifies the differences between pointers to vectors and vectors of pointers, providing correct code examples for both approaches.

Uploaded by

Bret Hart
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Array of Vector Pointers

struct AdvancedButtonPress3 {
std::vector<CGKeyCode> keys;
std::vector<CGKeyCode> modifierKeys;
int delayBefore; // wait time before pressing the key
int WaitAfterPressDuration;
int pressInterval; // if there are sequence of presses in a
single
// entry, this is the wait interval for each presses
};
static std::vector<AdvancedButtonPress3> seqMeepoGen =
{
{{9}, {}, 100, 20, 100}, // v (Select all)

};

static std::vector<AdvancedButtonPress3> seqMeepo1 = {


{{12, 48}, {}, 100, 20, 100}, // q, tab
};

static std::vector<AdvancedButtonPress3> seqMeepo2 = {


{{20, 13, 23, 13, 9}, {}, 100, 20, 90}, // 3,w,5,w

};

static std::vector<AdvancedButtonPress3> seqMeepo3 = {

{{20, 13, 23, 13, 22, 13, 9}, {}, 100, 20, 90}, // 3,w,5,w,6,w
};
static std::vector<AdvancedButtonPress3> seqMeepo4 = {

a{{20, 13, 23, 13, 22, 13, 26, 13, 9}, {}, 100, 20, 90}, //
3,w,5,w,6,w,7,w
};

static std::vector<AdvancedButtonPress3> seqMeepo5 = {

{{20, 13, 23, 13, 22, 13, 26, 13, 28, 13, 9}, {}, 100, 20, 90}, //
3,w,5,w,6,w,7,w,8,w
};

1/5
static std::vector<AdvancedButtonPress3> seqMeepo6 = {};

static &std::vector<AdvancedButtonPress3> sequences[] =


{&seqMeepo2, &seqMeepo3, &seqMeepo4, &seqMeepo5};

how to create array of vector of reference to


AdvancedButtonPress3 because the above is giving error

In C++, you cannot create an array of references because references are not objects
themselves and cannot be reseated. However, you can achieve your goal by using pointers to
the vectors or by storing std::reference_wrapper in a container like std::vector .

Here’s how you can fix your issue:

1. Use Pointers Instead of References


You can use an array of pointers to std::vector<AdvancedButtonPress3> to store references
to your vectors.

cpp

static std::vector<AdvancedButtonPress3>* sequences[] = {


&seqMeepo2,
&seqMeepo3,
&seqMeepo4,
&seqMeepo5
};

This resolves your issue because pointers can be stored in arrays, and you can dereference
them when needed.

2. Use std::vector of std::reference_wrapper


If you prefer to avoid raw pointers, you can use std::reference_wrapper , which is a
lightweight wrapper for references that makes them storable in containers.

cpp

#include <vector>
#include <functional> // for std::reference_wrapper

static std::vector<std::reference_wrapper<std::vector<AdvancedButtonPress3>>>
sequences = {

2/5
seqMeepo2,
seqMeepo3,
seqMeepo4,
seqMeepo5
};

Here, std::reference_wrapper ensures the references are properly handled, and you can
use them like the original objects.

Accessing Elements
For both approaches:

1. With pointers:

cpp

for (auto* seq : sequences) {


for (const auto& press : *seq) {
// Access elements of AdvancedButtonPress3 here
}
}

2. With std::reference_wrapper :

cpp

for (const auto& seq : sequences) {


for (const auto& press : seq.get()) {
// Access elements of AdvancedButtonPress3 here
}
}

Why Your Code Fails


The line:

cpp

static &std::vector<AdvancedButtonPress3> sequences[] = {&seqMeepo2, &seqMeepo3,


&seqMeepo4, &seqMeepo5};

is invalid because:

3/5
You cannot use & with the static keyword in this context.

Arrays of references are not allowed in C++.

By switching to pointers or std::reference_wrapper , you work within the language's


constraints while achieving the desired functionality.

static std::vector<AdvancedButtonPress3>* sequences[] = {


&seqMeepo2,
&seqMeepo3,
&seqMeepo4,
&seqMeepo5
};

static std::vector<AdvancedButtonPress3>*
why cant the * be inside like this static
std::vector<AdvancedButtonPress3*>

The reason you cannot write static std::vector<AdvancedButtonPress3*> in this case is


due to the semantic difference between std::vector<AdvancedButtonPress3>* and
std::vector<AdvancedButtonPress3*> .

Key Differences
1. std::vector<AdvancedButtonPress3>* :

This is a pointer to a std::vector of AdvancedButtonPress3 .

The std::vector itself stores instances of AdvancedButtonPress3 , not pointers.

The pointer ( * ) applies to the std::vector , meaning the variable holds the
memory address of a std::vector<AdvancedButtonPress3> .

Example:

cpp

static std::vector<AdvancedButtonPress3> vec;


static std::vector<AdvancedButtonPress3>* vecPtr = &vec; // Pointer to the
vector

2. std::vector<AdvancedButtonPress3*> :

This is a std::vector that stores pointers to AdvancedButtonPress3 objects.

4/5
The * applies to the elements of the std::vector , meaning the vector holds
pointers to individual AdvancedButtonPress3 instances, not the objects themselves.

Example:

cpp

static AdvancedButtonPress3 obj1, obj2;


static std::vector<AdvancedButtonPress3*> vec = {&obj1, &obj2}; // Vector of
pointers

Why This Matters in Your Case


In your code, you want to store pointers to std::vector<AdvancedButtonPress3> , which
means the * applies to the entire std::vector , not its elements. Therefore, you must write
it as std::vector<AdvancedButtonPress3>* .

If you were to write std::vector<AdvancedButtonPress3*> , the meaning would change:

You'd be storing pointers to individual AdvancedButtonPress3 objects inside the vector.

This would not align with your intended goal of pointing to multiple
std::vector<AdvancedButtonPress3> instances.

Correct Code
For your use case, stick with:

cpp

static std::vector<AdvancedButtonPress3>* sequences[] = {


&seqMeepo2,
&seqMeepo3,
&seqMeepo4,
&seqMeepo5
};

If you instead wanted a vector of pointers to AdvancedButtonPress3 objects (not relevant in


your example but valid in other contexts), you'd write:

cpp

static std::vector<AdvancedButtonPress3*> sequenceOfPointers;

5/5

You might also like