Page 2 of 2

Posted: Tue Apr 29, 2008 8:32 am
by Korona
You can access vector elements using a function like get(int index) or the [] operator. At the assembly level those functions will do a "mov dest, [array + index * scale]". A lookup table like the one you posted can be used to do method invocations, but it's not a general purpose data structure. This thread discusses the syntax of template declaration in c++ and not the details of the underlying implementation.

Posted: Wed Apr 30, 2008 12:28 am
by pcmattman
I'm pretty the STL vector class uses iterators instead of integers for it's internal referencing, but it's still the same concept.
At the assembly level those functions will do a "mov dest, [array + index * scale]"
But not just that, to clarify.

Posted: Wed Apr 30, 2008 4:39 am
by bluecode
pcmattman wrote:I'm pretty the STL vector class uses iterators instead of integers for it's internal referencing, but it's still the same concept.
The normal implementation for a std::vector would indeed be a C-like array. I think you mixed something up there. Iterators are a concept to _access_ a container, not a concept how to _store_ things in a container. In the case of a vector of T the iterator could be a T* (you could of course provide an entire iterator class, as long as it fullfills the requirements of a random-access iterator). The user of the std::vector class can then use begin() and end() (and of course rbegin() and rend()) to get the iterator that point to the beginning (ie. the first element in the internal C-like array) and to the end (ie. the (n-1)th element in the C-like array.
I'm pretty [...]
No, you are not :lol:

Posted: Wed Apr 30, 2008 9:41 am
by edfed
at assembly level, don't forget that everything is possible.

as you are all OS developpers, you shall know assembly as well as abstraction langages, otherwise you will be confused by pointer arythmetics in C or C++ that is really a boring concept.

in assembly, you have a lot of possibilities for addressing.

this is a level 2 addressing:

Code: Select all

mov eax,[esi+eax*4]
mov eax,[eax+eax*4]
call eax
how to do that in C?

and more, how to do that for multilevels addressing?

i don't post the specific cases i croos while coding, because they are to many.

vector of vector of vector of vector...
like file systems.....

:S

Posted: Wed Apr 30, 2008 1:33 pm
by bluecode
What has that to do with the topic _at_all_? :wink:

Posted: Wed Apr 30, 2008 3:39 pm
by pcmattman
bluecode wrote:
I'm pretty [...]
No, you are not :lol:
I was sure I put in "sure" :P. Shows why not to post late at night I guess.
The normal implementation for a std::vector would indeed be a C-like array.
I know that - my point was in reply to:
using a function like get(int index) or the [] operator

Posted: Thu May 01, 2008 12:22 am
by bluecode
Well the [] operator and the at() function really take integers as their argument. The argument type is size_type, but the C++ standard defines size_type as an "An unsigned integral type".

But I am not sure that you meant this either... Your sentence confuses me :lol:

Posted: Thu May 01, 2008 3:28 am
by pcmattman
I was talking about the get( int ); ;)

Posted: Fri May 02, 2008 1:33 am
by Solar
Either way is fine.

Code: Select all

std::vector< SomeClass > vec;
vec.push_back( SomeClass() );
std::vector< SomeClass >::const_iterator it = vec.begin(); // iterator
std::cout << vec[0]            // valid, access without bounds checking
          << vec.at( 0 )       // valid, throws exception if out-of-bounds
          << *it;              // valid, access through iterator