getting offset from iterator for use in another container

This is a discussion on getting offset from iterator for use in another container within the C++ Programming forums, part of the General Programming Boards category; Hello, I have two vectors (of different types) whose elements correspond to each other. I'm going through vector<foo> A using ...

  1. #1
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905

    getting offset from iterator for use in another container

    Hello,
    I have two vectors (of different types) whose elements correspond to each other. I'm going through vector<foo> A using find_if and removing some elements. I want to remove the corresponding elements in vector<bar> B. What is the cleanest way? I don't think that
    Code:
    iter - A.begin()
    is a good idea. What do you think?
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  2. #2
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    905
    You know what, I'm better off just going through manually beginning to end. This way I don't check the early elements multiple times DUH. But I'm still curious about the answer to my OP.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    2,047
    Well the desire to do so points to a design error. If vector B's element's depend on vector A's elements, then you should instead have a list of foo bar pairs, or perhaps one of the object types should hold the other.

    But if you want to force a way to do it, then yeah iter-A.begin is as good a way as any. Or you can forget about find_if, and use an indexed for loop.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  4. #4
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    I have two vectors (of different types) whose elements correspond to each other
    also, what you said, indicated that the order doesn't matter. If so, std::map would be a better and cleaner option.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Container - Iterator ++it
    By thescratchy in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2010, 08:37 AM
  2. iterator to unspecified container?
    By StainedBlue in forum C++ Programming
    Replies: 16
    Last Post: 06-04-2010, 05:02 AM
  3. Access via iterator to pointer container
    By atlmallu in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2009, 01:13 PM
  4. Replies: 1
    Last Post: 01-23-2006, 06:12 PM
  5. Replies: 4
    Last Post: 03-21-2004, 02:34 PM

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