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.
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.
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.
hey thnx =)
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;
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.
just to clarify
i should have #include <algorithm> in
in my header file
and using namespace std;
right?
That's all fine, I guess. result should be a list<type>::iterator.
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
>> 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.
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
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"