comparing vector elements

This is a discussion on comparing vector elements within the C++ Programming forums, part of the General Programming Boards category; Hello I have 2 vectors which I want to compare if they contain the same element(s) (not all elements, just ...

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

    comparing vector elements

    Hello

    I have 2 vectors which I want to compare if they contain the same element(s) (not all elements, just some specific). They hold 'some class' objects.

    What would be the best way to compare those vector elements?

    I've been thinking about reloading operator== in the class they contain, and the iterate through both vectors, something like:

    Code:
    std::vector<some>::iterator it = v1.begin(), it2;
    for (; it != v1.end(); ++it) {
    
    	for (it2 = v2.begin(); it2 != v2.end(); ++it2) {
    		if (*it == *it2) std::cout << "vectors contain the same element\n";
    	}
    }
    Is there any better/faster way of doing this?
    Maybe some other container would be more suitable? Please note that I need random access iterator.

    Many thanks in advance!

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could use "find" instead of yourself iterating through the vector. If the vector you are searching in is sorted, you could use a binary search to locate a particular element.

    If the lists are signficantly different size, you should iterate through the shorter one.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Quote Originally Posted by matsp View Post
    You could use "find" instead of yourself iterating through the vector. If the vector you are searching in is sorted, you could use a binary search to locate a particular element.

    If the lists are signficantly different size, you should iterate through the shorter one.

    --
    Mats
    I looked at vector reference but I couldnt find 'find' member.

    Are you sure it exists?

    Thanks!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I think it's in "algorithm" rather than in vector, because it works on all sorts of iteratable objects (including standard arrays).

    Edit: http://www.cplusplus.com/reference/algorithm/find.html

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    EOP
    EOP is offline
    Registered User
    Join Date
    Jan 2008
    Posts
    45
    if the vectors are sorted:

    set_intersection

    http://www.cppreference.com/wiki/stl...t_intersection

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    find might be slow - your loop would be O(m*n) where m is the number of objects in the first vector you want to search for and n is the number of objects in the second vector.

    Hopefully your vectors are sorted (or at least the second one) so you can use some of the other tools. Perhaps making a third vector that contains only the elements from the first vector that you want to search for, and then using set_intersection or something similar to verify that it is a subset of the second vector.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Problem with comparing strings!
    By adrian2009 in forum C Programming
    Replies: 2
    Last Post: 02-28-2009, 09:44 PM
  3. comparing bitmaps
    By bigSteve in forum C++ Programming
    Replies: 1
    Last Post: 11-08-2002, 09:40 AM
  4. comparing problem
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 03-05-2002, 05:19 AM
  5. comparing struct members
    By breed in forum C Programming
    Replies: 4
    Last Post: 11-22-2001, 11:27 AM

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