c++ - Derived vector: Perform operations -


i have derived std::vector<int> (i know shouldn't, wanted test it). can instantiate , assign values:

myvector v(5); v[0]=3; 

i can return value:

cout << v[0]; 

but how can access value(s) if want operations within class? like:

int func(int a){    return this->[0] + a; // example } 

as stated in comments under question:

return (*this)[0] + a; should work. – didierc 5 hours ago

additionally, since vector lays out memory in linear fashion (like array) can access memory holds values through pointer, so:

int *ptr = &(*this)[0]; // read integer console 3rd element of vector scanf("%d", ptr + 2); 

this can useful if have vector of chars , need pass char* string function, example.

be warned however, vector<bool> not behave in same way (the boolean values stored internally in bitfields, not array of bools, see http://isocpp.org/blog/2012/11/on-vectorbool).


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -