vector of vectors... Yes it sounds crazy

Programming, for all ages and all languages.
Korona
Member
Member
Posts: 1000
Joined: Thu May 17, 2007 1:27 pm
Contact:

Post 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.
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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.
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post 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:
User avatar
edfed
Member
Member
Posts: 42
Joined: Wed Apr 09, 2008 5:44 pm
Location: Mars

Post 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
welcome in my dream.
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post by bluecode »

What has that to do with the topic _at_all_? :wink:
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post 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
User avatar
bluecode
Member
Member
Posts: 202
Joined: Wed Nov 17, 2004 12:00 am
Location: Germany
Contact:

Post 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:
pcmattman
Member
Member
Posts: 2566
Joined: Sun Jan 14, 2007 9:15 pm
Libera.chat IRC: miselin
Location: Sydney, Australia (I come from a land down under!)
Contact:

Post by pcmattman »

I was talking about the get( int ); ;)
User avatar
Solar
Member
Member
Posts: 7615
Joined: Thu Nov 16, 2006 12:01 pm
Location: Germany
Contact:

Post 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
Every good solution is obvious once you've found it.
Post Reply