Thread: getting offset from iterator for use in another container

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

    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
    937
    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,149
    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
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    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.

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, 09: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, 07:12 PM
  5. Replies: 4
    Last Post: 03-21-2004, 03:34 PM