container random access

This is a discussion on container random access within the C++ Programming forums, part of the General Programming Boards category; I'd like to store position in container for random access. Which data type should I use? Should I go for ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    container random access

    I'd like to store position in container for random access. Which data type should I use? Should I go for (unsigned int), container::size_type, or something else?

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    What container?

  3. #3

    Join Date
    May 2005
    Posts
    1,041
    You can, technically, get into trouble with signed/unsigned mis-matches when storing indexes, so using, 'container::size_type' would actually be the universally safest way to store it, but in general using an unsigned int is also safe...I'm trying to remember when I had a problem with this, I think I was using 'int' and not 'unsigned int' (an int is signed by default) and somehow I ran into a problem doing this.
    I'm not immature, I'm refined in the opposite direction.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    container::size_type would probably be best, but unsigned int should be ok also. I would create a typedef for the container if you think it is at all possible that you will be changing the container type. That way you won't have to update the places you refer to its size_type.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by l2u View Post
    I'd like to store position in container for random access. Which data type should I use? Should I go for (unsigned int), container::size_type, or something else?
    An iterator.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> An iterator.

    What if an operation on the container has invalidated its iterators? Storing the position will be safe in that case.

  7. #7
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by Daved View Post
    >> An iterator.

    What if an operation on the container has invalidated its iterators? Storing the position will be safe in that case.
    The subject of the post is container RANDOM access. Random access iterators remain valid when the container changes, unless they go out of range.

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    I don't think that's accurate. A vector's iterator is a random access iterator. But if you add items to the end causing a reallocation that iterator will become invalid even though the index is still in range.

  9. #9
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by brewbuck View Post
    The subject of the post is container RANDOM access. Random access iterators remain valid when the container changes, unless they go out of range.
    That's not true, any kind of iterator can be invalidated when the capacity of the container changes (for example), since the whole container can be relocated.

  10. #10
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by robatino View Post
    That's not true, any kind of iterator can be invalidated when the capacity of the container changes (for example), since the whole container can be relocated.
    You and Daved are right -- as usual, when I get tired I get stupid. Good thing I don't have to drive anywhere.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Counting number from a random file
    By kamisama in forum C Programming
    Replies: 42
    Last Post: 02-22-2005, 04:16 PM
  2. non repeating random number generation?
    By gencor45 in forum C# Programming
    Replies: 1
    Last Post: 02-08-2005, 04:23 PM
  3. random access function table design?
    By talz13 in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2005, 02:30 PM
  4. Best way to generate a random double?
    By The V. in forum C Programming
    Replies: 3
    Last Post: 10-16-2001, 04:11 PM
  5. Random Access Files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 08-29-2001, 08:06 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21