STL vector with iterators

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jan.Steinke@t-online.de

    #1

    STL vector with iterators

    Hi there,
    i've got a STL list like this:
    list<object *> myList;
    Now, i want to create a vector with iterators of this list like this:

    vector< list<object *>::iterator > myVector

    Where's the fault?

    thanks for reply,
    Jan

  • Gianni Mariani

    #2
    Re: STL vector with iterators

    Jan.Steinke@t-online.de wrote:[color=blue]
    > Hi there,
    > i've got a STL list like this:
    > list<object *> myList;
    > Now, i want to create a vector with iterators of this list like this:
    >
    > vector< list<object *>::iterator > myVector
    >
    > Where's the fault?[/color]

    That's ok. If you're using it in a template, it won't work. You'll
    need this:

    vector< typename list<object *>::iterator > myVector

    You may also need to worry about const-ness.

    vector< typename list<object *>::const_itera tor > myVector

    Comment

    • Stefan Näwe

      #3
      Re: STL vector with iterators

      Jan.Steinke@t-online.de schrieb:[color=blue]
      > Hi there,
      > i've got a STL list like this:
      > list<object *> myList;
      > Now, i want to create a vector with iterators of this list like this:
      >
      > vector< list<object *>::iterator > myVector
      >
      > Where's the fault?[/color]

      Where's the code ?

      S.
      --
      Stefan Naewe
      naewe.s_AT_atla s_DOT_de

      Comment

      • Stephane Wirtel

        #4
        Re: STL vector with iterators

        Jan.Steinke@t-online.de said the following on 20/01/2006 12:51:[color=blue]
        > Hi there,
        > i've got a STL list like this:
        > list<object *> myList;
        > Now, i want to create a vector with iterators of this list like this:
        >
        > vector< list<object *>::iterator > myVector[/color]

        Can you try this template class ?

        template< typename T > class MyVector
        : public std::vector< typename std::list <T *> :: iterator > {
        };


        [color=blue]
        >
        > Where's the fault?
        >
        > thanks for reply,
        > Jan
        >[/color]

        Comment

        • Axter

          #5
          Re: STL vector with iterators

          Jan.Steinke@t-online.de wrote:[color=blue]
          > Hi there,
          > i've got a STL list like this:
          > list<object *> myList;
          > Now, i want to create a vector with iterators of this list like this:
          >
          > vector< list<object *>::iterator > myVector
          >
          > Where's the fault?
          >
          > thanks for reply,
          > Jan[/color]

          I recommend not using raw pointers in your container, and instead use
          smart pointers like boost::shared_p tr or smart_ptr

          Other pointers:



          Are you sure you need the list container in the first place. It's
          rarely optimal to use std::list over std::vector/std::deque.
          If you're not doing a lot of insertions and deletions in the center of
          the container, you should consider just using std::vector in the first
          place.
          I recommend you do some performance test to see if you really need the
          std::list in the first place.

          Comment

          • roberts.noah@gmail.com

            #6
            Re: STL vector with iterators


            Gianni Mariani wrote:[color=blue]
            > Jan.Steinke@t-online.de wrote:[color=green]
            > > Hi there,
            > > i've got a STL list like this:
            > > list<object *> myList;
            > > Now, i want to create a vector with iterators of this list like this:
            > >
            > > vector< list<object *>::iterator > myVector
            > >
            > > Where's the fault?[/color]
            >
            > That's ok. If you're using it in a template, it won't work. You'll
            > need this:
            >
            > vector< typename list<object *>::iterator > myVector
            >
            > You may also need to worry about const-ness.
            >
            > vector< typename list<object *>::const_itera tor > myVector[/color]

            When and why you need 'typename' is explained pretty well in Thinking
            in C++ V2, which is available for download on the internet.

            Comment

            Working...