user3213700
user3213700

Reputation: 159

Accessing pointer to a vector in a struct

How can I access the value of a pointer to a vector in a struct? I've got the following code:

#include <iostream>  
#include <vector>

using namespace std;

struct item {
    int value;
    vector<bool> pb;
    vector<bool> *path = &pb;
};

int main(int argc, char* argv[]) {
    vector<item> dp(10);
    for (int n = 0; n < 10; n++)
        dp[n].pb = vector<bool>(10);

    if (dp[1].path[2] == true)
        cout << "true";
    else cout << "false";
}

Which results in the following compilation error:

Error   C2678   binary '==': no operator found which takes a left-hand operand of type 'std::vector<bool,std::allocator<_Ty>>' (or there is no acceptable conversion)

How can I access the value stored at dp[1].path[2]?

Upvotes: 0

Views: 95

Answers (2)

segevara
segevara

Reputation: 630

In addition, you can use at function which is also

checks whether n is within the bounds of valid elements in the vector

code example

if (dp[1].path->at(2) == true)

Upvotes: 1

nishantsingh
nishantsingh

Reputation: 4662

path is a pointer to a vector. You have to do the following to access the value in the vector that it points to

if ((*(dp[1].path))[2] == true)

or

if (dp[1].path->operator[](2) == true)

Upvotes: 2

Related Questions