Pointers as iterators; vector<string> representation...

Discussion in 'C++' started by barcaroller, Nov 7, 2008.

  1. barcaroller

    barcaroller Guest

    1. If I pass pointers (char*) as iterators to an STL algorithm and the
    return value is an iterator, can I convert that iterator to a pointer? If
    yes, how?


    2. What is the internal representation of vector<string>? Will the vector
    contain the string objects or will it contain pointers/references to the
    string objects? The reason I ask is that it is not clear to me how the
    v.reserve() and &v[0] operations would work for a vector<string>.
    barcaroller, Nov 7, 2008
    #1
    1. Advertising

  2. barcaroller

    Jim Langston Guest

    "Victor Bazarov" <> wrote in message
    news:gf2jh4$5cm$...
    > barcaroller wrote:
    >> 1. If I pass pointers (char*) as iterators to an STL algorithm and the
    >> return value is an iterator, can I convert that iterator to a pointer?
    >> If yes, how?

    >
    > For most of the standard algorithms, the return value type is the same as
    > the one you pass. It's already a pointer, no need to convert. Test it,
    > you'll see.


    Just an added note, the way to convert an iterator to a pointer is to take
    the address of the dereferenced iterator. I.E.

    &(*it) would be a pointer to the item if it is an iterator or a pointer.

    >> 2. What is the internal representation of vector<string>?

    >
    > Implementation-defined.
    >
    > > Will the vector
    >> contain the string objects or will it contain pointers/references to the
    >> string objects? The reason I ask is that it is not clear to me how the
    >> v.reserve() and &v[0] operations would work for a vector<string>.

    >
    > The vector will contain a dynamic array of 'string' objects. The
    > 'reserve' operation allocates the array capable of containing future
    > 'string' objects (which will be constructed using 'placement new', most
    > likely). Taking the address of v[0] (which returns the reference to the
    > very first object in the inner array) will give you the starting address
    > of the inner array. What's there to [not] understand? Now, you probably
    > need to understand how 'string' works, as well. Do you?


    As an added note you can find your implementation of std::string on your
    system. Try opening up your string. file and taking a look. It can get
    pretty esoteric but if you can understand that code you will be well on your
    way to understanding C++.
    Jim Langston, Nov 7, 2008
    #2
    1. Advertising

  3. barcaroller

    barcaroller Guest

    "Victor Bazarov" <> wrote in message
    news:gf2jh4$5cm$...

    > The vector will contain a dynamic array of 'string' objects. The
    > 'reserve' operation allocates the array capable of containing future
    > 'string' objects (which will be constructed using 'placement new', most
    > likely). Taking the address of v[0] (which returns the reference to the
    > very first object in the inner array) will give you the starting address
    > of the inner array. What's there to [not] understand? Now, you probably
    > need to understand how 'string' works, as well. Do you?


    If the vector "contains" the strings, then reserving ahead of time is not
    useful for optimization because the vector would still not know what the
    size of the strings are until they are actually inserted.

    If, however, the vector contains only pointers/references to the strings,
    reserve() would still not be useful.
    barcaroller, Nov 8, 2008
    #3
  4. barcaroller wrote:
    > If the vector "contains" the strings, then reserving ahead of time is not
    > useful for optimization because the vector would still not know what the
    > size of the strings are until they are actually inserted.


    Yes, we always do know the "size of the strings" in advance. The
    'std::vector' object in this case contains 'std::string' objects. And
    the size of the latter is always pre-determined and known at compile
    time. This is the only size 'std::vector' needs to know about.

    In general case it is the 'std::string' object itself that contains the
    pointer to the controlled sequence (the actual "string"). The length of
    the sequence can vary (it is allocated independently), but the size of
    the 'std::string' object itself is always fixed and does not depend in
    any way on the current length of the controlled sequence.

    In other words, the actual "strings" (character sequences) are stored in
    'std::vector<std::string>' by pointers, but it has absolutely nothing to
    to with the 'std::vector' itself. This is an internal issue of
    'std::string'. 'std::vector' doesn't know about it and doesn't care
    about it.

    Reserving 'std::vector<std::string>' ahead of time is, of course, useful
    - it prevents unnecessary reallocations of 'std::vector' when adding
    extra 'std::string' objects to the 'std::vector'.

    > If, however, the vector contains only pointers/references to the strings,
    > reserve() would still not be useful.


    You seem to be caught in a terminological mix-up with regards to what is
    a "string" and what the word "stores" means in this case.

    But in any case, 'reserve()' is still useful (as described above).
    Unless, again, you give the word "useful" some meaning that is not
    immediately clear to me.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 8, 2008
    #4
  5. barcaroller wrote:
    > 1. If I pass pointers (char*) as iterators to an STL algorithm and the
    > return value is an iterator, can I convert that iterator to a pointer? If
    > yes, how?


    Can you illustrate what you are talking about with an example? Normally
    the return value is an iterator of the same kind as the one (the ones)
    that was passed in. If you pass 'char*'s, you get back a 'char*'. No
    need to convert anything.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Nov 8, 2008
    #5
  6. barcaroller

    James Kanze Guest

    On Nov 8, 1:42 am, "barcaroller" <> wrote:
    > "Victor Bazarov" <> wrote in message


    > news:gf2jh4$5cm$...


    > > The vector will contain a dynamic array of 'string' objects.
    > >  The 'reserve' operation allocates the array capable of
    > > containing future 'string' objects (which will be
    > > constructed using 'placement new', most likely).  Taking the
    > > address of v[0] (which returns the reference to the very
    > > first object in the inner array) will give you the starting
    > > address of the inner array.  What's there to [not]
    > > understand?  Now, you probably need to understand how
    > > 'string' works, as well.  Do you?


    > If the vector "contains" the strings, then reserving ahead of
    > time is not useful for optimization because the vector would
    > still not know what the size of the strings are until they are
    > actually inserted.


    That depends on the implementation of std::string. The sizeof
    (as opposed to the size of) an object is a compile time
    constant; std::string needs an additional indirection to handle
    variable length, but this is hidden in std::string; std::vector
    doesn't no beans about it. And depending on the implementation,
    it may not always have to do a deep copy, or it may not need the
    extra indirection if the strings are short enough.

    > If, however, the vector contains only pointers/references to
    > the strings, reserve() would still not be useful.


    Oh yes it is. Perhaps even more so. Increasing the capacity of
    a vector means copying member objects. If std::string is
    systematically doing a deep copy (most do, at least if the
    strings are longer than a certain length), then copying will
    mean allocating a new string, copying the data, then deleting
    the old one. (It's things like this that motivated the entire
    movable business in the next version of the standard.)

    The most frequent reason for reserve(), however, is to ensure
    the validity of iterators.

    --
    James Kanze (GABI Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
    James Kanze, Nov 8, 2008
    #6
  7. barcaroller

    barcaroller Guest

    "Andrey Tarasevich" <> wrote in message
    news:gf30m1$v90$...

    Thanks for the clarification. I hadn't realized that the size of the object
    and object.size() were unrelated.
    barcaroller, Nov 8, 2008
    #7
  8. barcaroller

    barcaroller Guest

    "Andrey Tarasevich" <> wrote in message
    news:gf30qm$v90$...

    > Can you illustrate what you are talking about with an example? Normally
    > the return value is an iterator of the same kind as the one (the ones)
    > that was passed in. If you pass 'char*'s, you get back a 'char*'. No need
    > to convert anything.


    char* foo(char* a, char* b, char* c, char* d)
    {
    iterator i = search(a,b,c,d);

    return ???
    }

    If I understand you correctly, I can say:

    return i;
    barcaroller, Nov 8, 2008
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,786
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
  2. David Crawford

    Question on vector of vector iterators

    David Crawford, Dec 15, 2005, in forum: C++
    Replies:
    3
    Views:
    469
  3. Replies:
    8
    Views:
    1,895
    Csaba
    Feb 18, 2006
  4. Alex Vinokur
    Replies:
    5
    Views:
    373
    Ron Natalie
    Jul 12, 2006
  5. , India
    Replies:
    10
    Views:
    1,061
    James Kanze
    Aug 8, 2009
Loading...

Share This Page