Thread: Vector contains?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    103

    Vector contains?

    Hello, I was just wondering how I would check to see if a vector contains an element. I've been searching but nothing ever comes up when I google vector contains for C++. Was wondering if someone could let me know. Thanks.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    hey thnx =)

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    so i implemented the vectorFind method like so but then i get this error

    Code:
    Severity and Description	Path	Resource	Location	Creation Time	Id
    /usr/include/c++/4.2/bits/stl_algo.h no match for ‘operator==’ in ‘__first.__gnu_cxx::__normal_iterator<_Iterator, _Container>::operator* [with _Iterator = Dependency*, _Container = std::vector<Dependency, std::allocator<Dependency> >]() == __val’		phase1	line 216	1223652736055	499

    here is a chunk of my code

    Code:
    result = find( dependencyList.begin(), dependencyList.end(), d);
    
                if(result == dependencyList.end()){
    			dependencyList.push_back(d);
    			isTrue = true;
                }
                else
                 isTrue = true;

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So your object doesn't have a "operator==" I suppose?

    -
    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.

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    just to clarify

    i should have #include <algorithm> in

    in my header file
    and using namespace std;

    right?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's all fine, I guess. result should be a list<type>::iterator.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    You should never have using namespace std; in a header file. But the find algorithm is in the std namespace.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> result should be a list<type>::iterator. <<
    You mean std::vector<Dependency>::iterator or std::vector<Dependency>::const_iterator, right?

    Also, TaiL, do your items have to be in the vector in a certain order? It might make more sense to use a different container if the order of the items doesn't matter. For example, a set<Dependency> might be more appropriate. It is built to allow you to add items and look them up to see if they exist already. In fact, you can insert an item and it will just fail if it already exists.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The order of dependencies does matter, since apparently he's building a work queue.

    Still, I think in this case I'd use an ADT like a linked hash set. Or build a real graph and do a topological sort.

    Either way, work queues are problematic for parallelization.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    103
    Quote Originally Posted by Daved View Post
    >> result should be a list<type>::iterator. <<
    You mean std::vector<Dependency>::iterator or std::vector<Dependency>::const_iterator, right?
    yeah thats what i was asking about. Sorry.

    Its actually recommended to use a vector =\ so should i be using std::vector<Dependency>:;const_iterator or std::vector<Dependency>iterator??

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Daved View Post
    >> result should be a list<type>::iterator. <<
    You mean std::vector<Dependency>::iterator or std::vector<Dependency>::const_iterator, right?
    Yes, you're right. Thank you.

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    I think the name 'isTrue' for a boolean variable is about the worst name you can get. How meaningful is it for itTrue to be true? What can we learn from isTrue being false?
    What exactly is it that is, or is not, true? Now call it that instead!

    Also, if you're doing many of these find operations on a vector then it's likely you'd be better off switching to another container instead such as a std::set.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed