### Changing from arrays TO struct containing arrays

0

I am changing my code so it would fit me better .

``````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)

2012-04-03 19:41
by george mano
As to your edit, you'll need to define the function `bool operator==(const NewArray& n, const array<array<int,4>,4>& a) { return n.pinak == a); }`Robᵩ 2012-04-03 20:09
@Robᵩ This function works for me. Please make a post of it so I can accept this answer - george mano 2012-04-03 20:39
That was DRVic's answer, my comment just filled it in. Go ahead and accept his answer, since that is what solved your problem - Robᵩ 2012-04-03 20:41

0

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.

2012-04-03 19:45
by DRVic
How can I do that ? Is there an easier way - george mano 2012-04-03 19:52
Well part of the answer is to add an operator==( const NewArray & ); but the other part of the answer is you have to decide what makes two arrays equal. Are they the same array (pointer equality) or do they have the same elements (compare element by element) - DRVic 2012-04-03 19:57

2

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:

• Walks through every element of `visited`. For each element, it:
• Invokes the listed function, which
• Compares the element's `pinak` member to `myarray`

`find_if` then returns an iterator which points to the selected element, or `.end()` if none compare equal.

2012-04-03 19:53
by Robᵩ
Can you explain in your post what does these lines do - george mano 2012-04-03 19:55
I hope that helps - Robᵩ 2012-04-03 20:08