user1031204
user1031204

Reputation: 701

Linux C++ ._MyFirst ._MyLast vector equivilent

On Windows using VS2013 I am using ._MyFirst and ._MyLast to get the pointers to the first and last elements of the vector for a fast manipulation through pointer incrementing.

vec._MyFirst->element = anotherElement;
vec._MyFrist++;

On Linux though this does not seem to there but I assume there must be an equivalent of it. Would anyone know what it is?

I know this should not be done, but in debug mode using them has almost no overheads thus allowing me to do tests quickly on massive vectors.

_MyFirst is the direct pointer that is used by the vector itself to the first element and _MyLast points tot he last element. Instead of using an iterator it's much faster to go through the vector my incrementing the pointer memory address thus is why I am using them. Also if the vector ever gets resized the ._MyFirst and ._MyLast will always point to first and last elements. On linux their not present under that name.

Thank you

Upvotes: 1

Views: 2267

Answers (3)

Gerhard Stein
Gerhard Stein

Reputation: 1563

You can also get the pointers with:

auto sPtr = &vec.front();
auto lastPtr = &vec.back();

The end pointer is one position after the last element:

auto ePtr = lastPtr + 1;

Note: For the data() method you need c++11 or later. VS 2013 suports it partly, but under Linux you might need to tell him to use that standard.

Upvotes: 1

Richard Hodges
Richard Hodges

Reputation: 69912

OK, I see that you're looking for performance with debug info. One way (as you are doing) is to compile a debug build with no optimisations and then seek to 'optimise source code by hand' by writing nonstandard code.

Another of course is to write standards-conforming code and simply turn on optimisations and debug output.

I did an illustration of the costs of writing correct code some time back when answering this question

TL;DR - there is zero overhead.

Upvotes: 1

TartanLlama
TartanLlama

Reputation: 65730

You could just get a pointer to the start of the internal container and increment it to get the end.

auto fPtr = vec.data();
auto ePtr = fPtr + vec.size();

Upvotes: 4

Related Questions