Re: [C++] Vector - guaranteed to be contiguous?

From: Jerry Coffin (jcoffin_at_taeus.com)
Date: 01/18/04


Date: Sun, 18 Jan 2004 05:49:31 GMT

In article <tAjOb.7357$xm.351468@nasal.pacific.net.au>,
no@email.provided says...

[ ... ]

> I'm trying to figure out how you haven't contradicted yourself. Earlier I
> asked, "Suppose I have a built-in float array and I want to use
> std::vector<float>::insert(iterator it, const_iterator first, const_iterator
> last) to insert it into a vector, by passing a const float * for the second
> and third parameters. Is this possible if an iterator is not a pointer?"
> Your reply was "Yes." So, can I or can't I?

An algorithm will use certain operations on an iterator. If the data is
such that those operations on a pointer will access the data correctly,
you can use pointers as iterators into that data.

Mostly that means you can use pointers to items in an array as random
access iterators into that array. Other than that, most bets are off --
for most other data structures, iterators will need to overload
operators to do the right things.

-- 
    Later,
    Jerry.
The universe is a figment of its own imagination.


Relevant Pages

  • Re: Sorting records using sort()
    ... We have an array of n*m bytes. ... >> The trouble with writing iterators for this problem is that he needs it ... We create a proxy class and let the iterator return proxy objects. ... that is used only inside the iterator, it sets the data pointer to point ...
    (comp.lang.cpp)
  • Re: C Stack allocation
    ... C++ iterators, but it's purely a C idea (though I don't think I've ... The use of sentinel pointers (e.g. one offset past the end of an array, ... the loop body. ... counter and pointer) to a single add/increment per iteration, ...
    (comp.lang.c)
  • operation signatures for a dictionary type
    ... Suppose I want to implement a dictionary data structure of some kind, ... // Insert a new key/data pair into the tree. ... returns a pointer to the data. ... STL solves these problems by using iterators. ...
    (comp.lang.cpp)
  • Re: strange crash after assertion with std::map::iterator
    ... What makes you think default-constructed pointer is NULL? ... int* p; ... Well, you didn't do anything like that to your iterators, you just ... Considering that a raw pointer is a ...
    (microsoft.public.vc.stl)
  • Re: C Stack allocation
    ... passing a pointer to just past the end of an array was inspired by ... C++ iterators, but it's purely a C idea (though I don't think I've ... the loop body. ...
    (comp.lang.c)