CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2002
    Location
    NJ
    Posts
    3

    Searching a vector of objects for pairs

    Hello all,

    Say I have a vector of objects and I want to return an object based on a pair of strings. The strings can be in either order Ie; A B==B A.

    In general, what do you think is the best way to do this?

    Thanks,
    T.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Searching a vector of objects for pairs

    Quote Originally Posted by azeotrope View Post
    In general, what do you think is the best way to do this?
    There can be varying opinion on what is the "best way".

    http://www.cplusplus.com/reference/algorithm/find_if/

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Searching a vector of objects for pairs

    I think you have to provide more details to get a serious answer.

    Edit: Sorry Paul, was typing a response but changed my mind before posting so your response was not accounted for.
    Last edited by S_M_A; May 16th, 2013 at 02:46 PM.
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Searching a vector of objects for pairs

    Quote Originally Posted by S_M_A View Post
    I think you have to provide more details to get a serious answer.
    I agree, the question doesn't make much sense. Sounds to me like you may want a map.

  5. #5
    Join Date
    May 2013
    Posts
    1

    Re: Searching a vector of objects for pairs

    You may want to consider a sorted vector or a map as stated above.

  6. #6
    Join Date
    May 2009
    Posts
    2,413

    Re: Searching a vector of objects for pairs

    Quote Originally Posted by azeotrope View Post
    Say I have a vector of objects and I want to return an object based on a pair of strings. The strings can be in either order Ie; A B==B A.
    Well, if you search the vector from begin to end you'll have O(N) complexity.

    But if you introduce an extra unordered_map you can get O(1) accesses into the vector.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured