Skip to main content
gamedev.net
🔒 Locked

Vector data access

Started by chbrules 4 replies 550+ views
Original Post
chbrules
chbrules
I've added structs to my vector using push_back(), and now I want to access the struct inside the vector. I've been trying: vector[0].struct.var; but that format doesn't let me access the struct inside. I'm guessing I'm going to have to assign the reference of the vector's element to a pointer of the same datatype to access it? Like:

struct stuff { yada; };

vector<stuff> vecStuff;

....

vecStuff.push_back(stuff_object);

stuff newStuff;

newStuff = vecStuff[0];
Just wondering if there was a way to access the members of the struct inside the vector.
-Conrad
snk_kid
snk_kid
Quote:
Original post by chbrules
Just wondering if there was a way to access the members of the struct inside the vector.


Just the same as you would for C-style array, std::vector overloads the subscript operator which returns either a constant or non constant reference (depending on the context) to its element. so you can do:

v[index].yada


or:

const stuff& s = v[index];


or:

stuff& s = v[index];


notice i used a reference to prevent redundant copies its not worth doing for small objects like primitive built-in types such as integer (unless you want to actually modify the original instance), in general a good estimate/heuristic is anything that is less than or equal to the size of an integer/pointer make a copy/pass-by-value.
chbrules
chbrules
Ok, MSVC++ isn't letting me do that. It keeps bringing up the members for the vector object like push_back() and such. When I compile like that it gives me errors that my struct members aren't members of the vector object. Really weird.

Well I have a question while I'm at it, say I created an struct pointer, then made it a 'new' struct array. I fill it with values, then I want to write it to my vector. I can write it as the value right? Like:

vector.push_back(*StructArray);

where the vector's datatype is the struct, and not a pointer. I don't know if it's working correctly, I don't think so, but in debug mode, I don't see an array from the vector.
-Conrad
snk_kid
snk_kid
Quote:
Original post by chbrules
Ok, MSVC++ isn't letting me do that. It keeps bringing up the members for the vector object like push_back() and such. When I compile like that it gives me errors that my struct members aren't members of the vector object. Really weird.


Can you show us the exact code for that.

Quote:
Original post by chbrules
Well I have a question while I'm at it, say I created an struct pointer, then made it a 'new' struct array. I fill it with values, then I want to write it to my vector. I can write it as the value right? Like:

vector.push_back(*StructArray);

where the vector's datatype is the struct, and not a pointer. I don't know if it's working correctly, I don't think so, but in debug mode, I don't see an array from the vector.


That wont work, if you want to copy a C-style dynamic array to std::vector there are a thew ways to do this

A. on construction of the vector:

foo* f = new foo[some_size];...std::vector<foo> v(f, f + some_size);...delete[] f;


B. via generic copy alogrithm:

#include <algorithm>#include <iterator>foo* f = new foo[some_size];std::vector<foo> v;v.reserve(some_size);std::copy(f, f + some_size, std::back_inserter(v));...delete[] f;


C. via vector's insertion method:

foo* f = new foo[some_size];...std::vector<foo> f;....f.insert(some_iterator_position, f, f + some_size);...delete[] f;

chbrules
chbrules
CBase::tMapVector *vecMapData; //Vector datatype.....I assign it a pointer value to the actual vector holding it.....vecMapData[y]. <-----


Right when I use the dot operator, I get all the vector library members of that object. My program doesn't crash, because I can access the members with at(), but that's about it.

Also, on the B. way to add a struct array, what does some_size represent?
-Conrad
snk_kid
snk_kid
Quote:
Original post by chbrules
CBase::tMapVector *vecMapData; //Vector datatype.....I assign it a pointer value to the actual vector holding it.....vecMapData[y]. <-----


Right when I use the dot operator, I get all the vector library members of that object. My program doesn't crash, because I can access the members with at(), but that's about it.


okay i think i know whats going on now, a pointer can be subscripted just as an array because a pointer could point to an array or a single instance, because you've used indexed access on a pointer and not on std::vector intellisense kicks in and thinks its an array of std::vector, either dereference the pointer first then use vector subscript e.g.:

CBase::tMapVector* vecMapData;....(*vecMapData)[y].some_member;


or use reference types instead:

CBase::tMapVector& vecMapData = foo_vec;vecMapData[y].some_member;


Note you cannot make reference type refer to another instance after its defined and must be assigned on first definition like above otherwise normal assignment will do assignement on the original instance (copy assignement), because a reference to an instance is the instance for all intents & purposes.

Quote:
Original post by chbrules
Also, on the B. way to add a struct array, what does some_size represent?


What ever size the C-style dynamic array is.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.