I am changing my code so it would fit me better .
I had:
deque<array<array<int,4>,4>> visited;
Now I have :
deque<New_Array> visited;
where New_Array is:
struct New_Array {
array<array<int,4>,4> pinak;
int h;
}Jim;
my array is like this:
array<array<int,4>,4> myarray;
The problem is that I have a else-if function like this:
else if (find(visited.begin(), visited.end(), myarray)==visited.end())
This function checks if an array is in the visited stack-deque. If it is not then the else-function works. But now, the visited deque will have to contain structs ,not arrays . How can I transform this function to work with the new containers of the deque?
I made this change so each array could be connected with a number(h). I need to check the array , I dont care about the number.
EDIT:
error C2678: binary '==' : no operator found which takes a left-hand operand of type 'New_Array' (or there is no acceptable conversion)
If I read your question correctly, what you need to do is define an equality operator for your struct. Then the Find can use that operator in its search.
Perhaps you can use std::find_if
:
else if (find_if(visited.begin(), visited.end(),
[&myarray](const NewArray& newArray) {
return myarray == newArray.pinak;
}) == visited.end())
std::find_if(Iterator first, Iterator last, Predicate pred)
returns an iterator to the first element in the range [first,last) for which applying pred to it, is true.
The first and second parameters, visited.begin()
, and visited.end()
specifiy that we should examine the std::deque<NewArray>
named visited
.
So, we walk through the std::deque
called visited
, testing each element in turn.
For each element of the deque, we apply this predicate:
[&myarray](consts NewArray& newArray) { return myarray == newArray.pinak }
This syntax is a lambda expression. It creates an object which can be subsequently invoked by the ()
operator (which find_if
, in fact, does).
The first part of the lambda lists variable which will be made available to the body of the expression. In this case, [&myarray]
makes that variable available in the body, by const reference.
The next part is the parameter list of operator()
: (const NewArray& newArray)
. It has the same meaning as in any other function.
Finally, the body of the expression compares the passed-in element of visited
with the variable myarray
. To determine equality, we compare the array to the pinak
member of the passed-in structure.
In short, the find_if
function:
visited
. For each element, it:pinak
member to myarray
find_if
then returns an iterator which points to the selected element, or .end()
if none compare equal.
bool operator==(const NewArray& n, const array<array<int,4>,4>& a) { return n.pinak == a); }
Robᵩ 2012-04-03 20:09