## Is it possible to pass a variable length array as a parameter in C++? - c++

I do not know the value of V before. It is found within a file I open in the program. It cannot be defined as such #DEFINE V __. It does not work as a global variable. The input file changes V based on the contents. Expected the parameters to pass and use the djisktra's algorithm found on Geeks for Geeks.
I have tried declaring V globally, but I am given an error saying "variable must have constant value."
void dijkstra(int graph[V][V], int src, int V)
//array function being pasted, error is the V in graph[V]
//V is defined at beginning of main as
int V;
//where V is changed
while(std::getline(file2,newstr))
{
if(newstr.find(check) != std::string::npos)
{
V++;
}
}
//where it is passed in main
for(int i = 0; i < V; i++)
{
size = V;
dijkstra(array[size][size], i, V);
}

Don't use C-style arrays. Use std::vector and friends from the Standard Library where you can ask for the size if you want to know.
Converted:
void dijkstra(const std::vector<std::vector<int>>& graph, int src) {
auto v = graph.size();
// ... Other code.
}
For inserting you can use push_back:
std::vector<std::vector<int>> graph;
while(std::getline(file2,newstr)) {
if(newstr.find(check) != std::string::npos) {
std::vector<int> row;
row.push_back(...);
graph.push_back(row);
}
}
Then pass it in like a regular variable:
dijkstra(graph, src);
If all that vector stuff looks really ugly, typedef it to something more friendly looking.

For c style arrays, you need to know the size at compile time. A variable like int N; is a runtime value. A variable like constexpr int N = 9; is usable at compile time and cannot be mutated.
If you need an array sizeable at runtime, you need some sort of dynamic array. The most common one is std::vector.
void dijkstra(std::vector<int> graph, int src, int V)
std::vector<int> graph;
graph.resize(V * V); // vector are resizable
for(int i = 0; i < V; i++)
{
size = V;
dijkstra(graph, i, V);
}

Is it possible to pass a variable length array as a parameter in C++.
No.
Variable length arrays are not supported in std C++, But read on, they have an alternative that is surprisingly better.
I do not know the value of V before it is found within a file I open
in the program.
A 1d vector is trivial to create, after your code has found V, no compile time constant required.
Early in the startup in one of my programs, the gBoard vector is built using argv[3] and argv[4]. Here is a snippet:
aTermPFN += argv[1]; // ouput tty, /dev/pts/<argv[1]>
fillPatternChoiceLetter = argv[2][0];
aMaxRow = stoi(argv[3]);
aMaxCol = stoi(argv[4]);
userDim = true;
Clearly, the program has already started ... and V size is easily computed from (aMaxRow * aMaxCol).
I find it easy to access a 1d vector (or 1d array), in row major order, as if it is a 2d matrix, with the following function:
// game-board-index: computes index into the single dimension vector
// from 2d (row, col) matrix coordinates
size_t gbIndx(int r, int c) { return static_cast<size_t>((r * maxCol) + c); }
// a 2d game board of cells
// 2d access (row major order) implemented using 1d access
Cell_t* getCell( int r, int c ) { return (gBoard [gbIndx(r,c)]); }
// 1d access is surprisingly convenient for many functions
Cell_t* getCell( uint gbIndex ) { return (gBoard [gbIndex]); }
Sample initialization usage:
// vvvvvvvvvvvvvvvvvvv_-- 2d matrix access
gBoard [ gbIndx((midRow+1), midCol) ] -> setOptionX();
// ^^^^^^--1d row-major order index
A randomized gGoard is trivial in 1d:
void GOLUtil_t::setRandom()
{
CellVec_t myVec(gBoard); // copy cell vector
random_device rd;
mt19937_64 gen(rd());
shuffle (myVec.begin(), myVec.end(), gen); // shuffle order
int count = 1;
for ( auto it : myVec ) // randomly mark half the cells
{
if(count++ & 1)
it->setAlive(); // every odd cell
}
}
Note from https://en.cppreference.com/w/cpp/container/vector:
"The elements are stored contiguously, which means that elements can be accessed not only through iterators, but also using offsets to regular pointers to elements. This means that a pointer to an element of a vector may be passed to any function that expects a pointer to an element of an array."
I was surprised how often the 1d access enabled simpler code.
for (auto it : gBoard)
it->init(); // command each cell to init
Summary:
Despite variable-length-arrays (vla) not being supported in std C++, I believe you will find std::vector a better alternative. And you will find that passing the vector within your code works.

## Related

### How to create an auxiliary data structure to keep track of heap indices in a minheap for the decrease_key operation in c++

I think this is probably a trivial problem to solve but I have been struggling with this for past few days. I have the following vector: v = [7,3,16,4,2,1]. I was able to implement with some help from google simple minheap algorithm to get the smallest element in each iteration. After extraction of the minimum element, I need to decrease the values of some of the elements and then bubble them up. The issue I am having is that I want find the elements whose value has to be reduced in the heap in constant time, then reduce that value and then bubble it up. After the heapify operation, the heap_vector v_h looks like this: v_h = [1,2,7,4,3,16]. When I remove the min element 1, then the heap vector becomes, [2,3,7,4,16]. But before we do the swap and bubble up, say I want to change the values of 7 to 4, 16 to 4 and 4 to 3.5 . But I am not sure where they will be in the heap. The indices of values of the elements that have to be decreased will be given with respect to the original vector v. I figured out that I need to have an auxiliary data structure that can keep track of the heap indices in relation to the original order of the elements (the heap index vector should look like h_iv = [2,4,5,3,1,0] after all the elements have been inserted into the minheap. And whenever an element is deleted from the minheap, the heap_index should be -1. I created a vector to try to update the heap indices whenever there is a change but I am unable to do it. I am pasting my work here and also at https://onlinegdb.com/SJR4LqQO4 Some of the work I had tried is commented out. I am unable to map the heap indices when there is a swap in the bubble up or bubble down operations. I will be very grateful to anyone who can lead me in a direction to solve my problem. Please also let me know if I have to rethink some of my logic. The .hpp file #ifndef minheap_hpp #define minheap_hpp #include <stdio.h> // #include "helper.h" #include <vector> class minheap { public: std::vector<int> vect; std::vector<int> heap_index; void bubble_down(int index); void bubble_up(int index); void Heapify(); public: minheap(const std::vector<int>& input_vector); minheap(); void insert(int value); int get_min(); void delete_min(); void print_heap_vector(); }; #endif /* minheap_hpp */ The .cpp file #include "minheap.hpp" minheap::minheap(const std::vector<int>& input_vector) : vect(input_vector) { Heapify(); } void minheap::Heapify() { int length = static_cast<int>(vect.size()); // auto start = 0; // for (auto i = 0; i < vect.size(); i++){ // heap_index.push_back(start); // start++; // } for(int i=length/2-1; i>=0; --i) { bubble_down(i); } } void minheap::bubble_down(int index) { int length = static_cast<int>(vect.size()); int leftChildIndex = 2*index + 1; int rightChildIndex = 2*index + 2; if(leftChildIndex >= length){ return; } int minIndex = index; if(vect[index] > vect[leftChildIndex]) { minIndex = leftChildIndex; } if((rightChildIndex < length) && (vect[minIndex] > vect[rightChildIndex])) { minIndex = rightChildIndex; } if(minIndex != index) { std::swap(vect[index], vect[minIndex]); // std::cout << "swap " << index << " - " << minIndex << "\n"; // auto a = heap_index[heap_index[index]]; // auto b = heap_index[heap_index[minIndex]]; // heap_index[a] = b; // heap_index[b] = a; // print_vector(heap_index); bubble_down(minIndex); } } void minheap::bubble_up(int index) { if(index == 0) return; int par_index = (index-1)/2; if(vect[par_index] > vect[index]) { std::swap(vect[index], vect[par_index]); bubble_up(par_index); } } void minheap::insert(int value) { int length = static_cast<int>(vect.size()); vect.push_back(value); bubble_up(length); } int minheap::get_min() { return vect[0]; } void minheap::delete_min() { int length = static_cast<int>(vect.size()); if(length == 0) { return; } vect[0] = vect[length-1]; vect.pop_back(); bubble_down(0); } void minheap::print_heap_vector(){ // print_vector(vect); } and the main file #include <iostream> #include <iostream> #include "minheap.hpp" int main(int argc, const char * argv[]) { std::vector<int> vec {7, 3, 16, 4, 2, 1}; minheap mh(vec); // mh.print_heap_vector(); for(int i=0; i<3; ++i) { auto a = mh.get_min(); mh.delete_min(); // mh.print_heap_vector(); std::cout << a << "\n"; } // std::cout << "\n"; return 0; }

"I want to change the values of 7 to 4, 16 to 4 and 4 to 3.5 . But I am not sure where they will be in the heap. The indices of values of the elements that have to be decreased will be given with respect to the original vector v. ... Please also let me know if I have to rethink some of my logic." Rather than manipulate the values inside the heap, I would suggest keeping the values that need changing inside a vector (possibly v itself). The heap could be based on elements that are a struct (or class) that holds an index into the corresponding position in the vector with the values, rather than hold the (changing) value itself. The struct (or class) would implement an operator< function that compares the values retrieved from the two vector locations for the respective index values. So, instead of storing the comparison value in the heap elements and comparing a < b, you would store index positions i and j and so on and compare v[i] < v[j] for the purpose of heap ordering. In this way, the positions of the numerical values you need to update will never change from their original positions. The position information will never go stale (as I understand it from your description). Of course, when you make changes to those stored values in the vector, that could easily invalidate any ordering that might have existed in the heap itself. As I understand your description, that much was necessarily true in any case. Therefore, depending on how you change the values, you might need to do a fresh make_heap to restore proper heap ordering. (That isn't clear, since it depends on whether your intended changes violate heap assumptions, but it would be a safe thing to assume unless there are strong assurances otherwise.) I think the rest is pretty straight forward. You can still operate the heap as you intended before. For ease you might even give the struct (or class) a lookup function to return the current value at it's corresponding position in the vector, if you need that (rather than the index) as you pop out minimum values. p.s. Here is a variation on the same idea. In the original version above, one would likely need to also store a pointer to the location of the vector that held the vector of values, possibly as a shared static pointer of that struct (or class) so that all the members could dereference the pointer to that vector in combination with the index values to look up the particular member associated with that element. If you prefer, instead of storing that shared vector pointer and an index in each member, each struct (or class) instance could more simply store a pointer (or iterator) directly to the corresponding value's location. If the values are integers, the heap element struct's member value could be int pointer. While each pointer might be larger than an index value, this does have the advantage that it eliminates any assumption about the data structure that holds the compared values and it is even simpler/faster to dereference vs. lookup with an index into the vector. (Both are constant time.) One caution: In this alternate approach, the pointer values would be invalidated if you were to cause the vector's storage positions to change, e.g. by pushing in new values and expanding it in a way that forces it to reallocate it's space. I'm assuming you only need to change values, not expand the number of values after you've begun to use the heap. But if you did need to do that, that would be one reason to prefer index values, since they remain valid after expanding the vector (unlike pointers). p.p.s. This technique is also valuable when the objects that you want to compare in the heap are large. Rather than have the heap perform many copy operations on large objects as it reorders the positions of the heap elements, by storing only pointers (or index values) the copying is much more efficient. In fact, this makes it possible to use heaps on objects that you might not want to copy at all. Here is a quick idea of one version of the comparison function (with some class context now added). class YourHeapElementClassName { public: // constructor explicit YourHeapElementClassName(theTypeOfYourComparableValueOrObject & val) : m_valPointer(&val) { } bool operator<(const YourHeapElementClassName & other) const { return *m_valPointer < *(other.m_valPointer); } ... private: theTypeOfYourComparableValueOrObject * m_valPointer; }; // YourHeapElementClassName // and later instead of making a heap of int or double, // you make a heap of YourHeapElementClassName objects // that you initialize so each points to a value in v // by using the constructor above with each v member. // If you (probably) don't need to change the v values // through these heap objects, the member value could be // a pointer to a const value and the constructor could // have a const reference argument for the original value. If you had need to do this with different types of values or objects, the pointer approach could be implemented with a template that generalizes on the type of value or object and holds a pointer to that general type.

### can someone help me translate this c++ code to c?

this is a magic square generator, but do not know C++, I have some difficulties to convert this code: #include <vector> #include <iostream> using namespace std; //There two series will be on even in case of magic square // One of even order will be for multiple of 4 void BuildDoublyEvenMagicSquare(vector<vector<int> > &mat, int Order); //Other of even order will be for multiple of 2 void SinglyEvenMagicSquare(vector<vector<int> > &mat, int order); // For odd order void BuildOddMagicSquare(vector<vector<int> > &mat, int Order); // For odd order void BuildOddMagicSquare(vector<vector<int> > &mat, int Order) { int SqrOfOrder = Order * Order; int start=0, mid=Order/2; // start position for (int loop=1; loop<=SqrOfOrder; ++loop) { mat[start--][mid++] = loop; if (loop % Order == 0) { start += 2; --mid; } else { if (mid==Order) mid -= Order; else if (start<0) start += Order; } } } void BuildDoublyEvenMagicSquare(vector<vector<int> > &mat, int Order) { vector<vector<int> > A(Order, vector<int> (Order, 0)); vector<vector<int> > B(Order, vector<int> (Order, 0)); int i, j; //Building of matrixes I and J int index=1; for (i=0; i<Order; i++) for (j=0; j<Order; j++) { A[i][j]=((i+1)%4)/2; B[j][i]=((i+1)%4)/2; mat[i][j]=index; index++; } for (i=0; i<Order; i++) for (j=0; j<Order; j++) { if (A[i][j]==B[i][j]) mat[i][j]=Order*Order+1-mat[i][j]; } } void BuildSinglyEvenMagicSquare(vector<vector<int> > &mat, int order) { int ho=order/2; vector<vector<int> > C(ho, vector<int> (ho, 0)); // For Order is Odd if (order%2==1) BuildOddMagicSquare(C, order); // For Order is Even else { //For Order is Doubly Even Order if (order % 4==0) BuildDoublyEvenMagicSquare(C, order); //For Order is Singly Even Order else BuildSinglyEvenMagicSquare(C, order); } int i, j, k; for (i=0; i<ho; i++) for (j=0; j<ho; j++) { mat[i][j]=C[i][j]; mat[i+ho][j]=C[i][j]+3*ho*ho; mat[i][j+ho]=C[i][j]+2*ho*ho; mat[i+ho][j+ho]=C[i][j]+ho*ho; } if (order==2) return; vector<int> A(ho, 0); vector<int> B; for (i=0; i<ho; i++) A[i]=i+1; k=(order-2)/4; for (i=1; i<=k; i++) B.push_back(i); for (i=order-k+2; i<=order; i++) B.push_back(i); int temp; for (i=1; i<=ho; i++) for (j=1; j<=B.size(); j++) { temp=mat[i-1][B[j-1]-1]; mat[i-1][B[j-1]-1]=mat[i+ho-1][B[j-1]-1]; mat[i+ho-1][B[j-1]-1]=temp; } i=k; j=0; temp=mat[i][j]; mat[i][j]=mat[i+ho][j]; mat[i+ho][j]=temp; j=i; temp=mat[i+ho][j]; mat[i+ho][j]=mat[i][j]; mat[i][j]=temp; } int main() { int Order; cout<<"Enter the order of square which you wanna: "; cin>>Order; vector<vector<int> > mat(Order, vector<int> (Order, 0)); // For order less than 3 is meaningless so printing error if (Order<3) { cout<<" Order Of Square must be greater than 2"; return -1; } // For Order is Odd if (Order%2==1) BuildOddMagicSquare(mat, Order); // For Order is Even else { //For Order is Doubly Even Order if (Order % 4==0) BuildDoublyEvenMagicSquare(mat, Order); //For Order is Singly Even Order else BuildSinglyEvenMagicSquare(mat, Order); } // Display Results for (int i=0; i<Order; i++) { for (int j=0; j<Order; j++) { cout<< mat[i][j]<<" " ; } cout<<endl; } return 0; } for example, how can I write this function call in C? void BuildDoublyEvenMagicSquare(vector<vector<int> > &mat, int Order); and what vector<vector<int> > &mat means? #Omnifarious can i use something like this? int **mat: *mat = (int **)malloc(sizeof(int*)*Order); for (int i=0;i<Order;i++) mat[i] = (int *)malloc(sizeof(int)*Order);

For the last part of the question, in C that function prototype would look like this if you follow the rest of my advice: void BuildDoublyEvenMagicSquare(int *mat, int Order); There are actually several ways you could do it. There are some things being done here that simply can't be done in C, so you'll have to sort of go for a slightly different approach. The biggest thing is the C++ vector's. A C++ vector is like a C array, but it does all the memory management for you. This means, for example, that it's fairly convenient to have an array of arrays where in C it would just add to your resource management headache. The C++ declaration: vector<int> varname(5); is roughly equivalent to the C declaration: int varname[5]; But in C++ you can do this: int randominteger = 7; vector<int> varname(randominteger); and in C this is illegal unless you have a C99 compliant compiler (-std=c99 in gcc): int randominteger = 7; int varname[randominteger]; You can't have arrays with variable numbers of elements in C, so you have to resort to calloc or malloc and do your own memory management, like this: /* Not that this is not necessary and shouldn't be done (as it's * * prone to memory leaks) if you have a C99 compliant compiler. */ int randominteger = 7; int *varname = calloc(randominteger, sizeof(int)); if (varname == NULL) { /* Die horribly of running out of memory. */ } In this case, I'm assuming that you're going to unfold your array of arrays into a single long C array of integers large enough to hold the answer so you can reduce the number of bits of memory you have to manage. To accomplish this, I would use a call like mat = calloc(order * order, sizeof(int)); in main, which also means you'll have to call free(mat) when you're finished with it at the end of main. I'm also assuming that you're unfolding the array so that you no longer have an array of arrays. That means you'll have to be doing some math to turn a row,column index into a linear index into the array. Something like row * order + column. You'll have to repeat the procedure I suggested for main in each of the functions that build a magic square because they each create temporary arrays to hold stuff in that go away at the end of the function.

Are you restricted to building the project as a C project? If you're writing good C code (and it isn't C99), you can probably compile it as C++ with no difficulty. If you can then build it as a C++ program, you can use the function as is. In that case, all you really need to know is that you've got vector<vector<int> > mat, and when you call your function it's got your result. Then you can put the preprocessor directive #include <vector> in your files that use it, and follow it with using std::vector, and everything will just work. In particular, you can read off the values with mat[i][j], just as you would with an array of array of int in C. One thing to watch is that you write vector<vector<int> > rather than vector<vector<int>>, since in the latter the >> will be treated as a right-shift operator rather than angle bracket delimiters. This will be fixed in C++0x, when it comes out (the x digit is now strictly hex), and may be fixed in particular compilers. Alternatively, write a wrapper function that takes the vector and changes it into an array of array of int. For convenience, you can find the number of elements in a vector with mat.size() or mat[i].size().

I'll just answer the last part of the question. vector is a container in the C++ standard library. It's like an array that can automatically resize itself when it gets full. A vector<vector<int> > is a vector containing vector objects, and the latter holds int. A vector<vector<int> >& is a reference to same. A reference is like a pointer, except that you do not use * to access the actual contents. So you treat mat "as if" it's a vector object directly, except that it's really aliased to another instance, so any changes you make to it will "reflect back" and affect what the caller can see. Simple example of references: void add1(int& n) { ++n; } int main() { int num = 5; add1(num); // num is 6 here }

A C++ vector is like a C array. It adds some nice features, like optional bounds checking, automatic reallocation when it needs its size increased, and so on. A vector<int> is roughly analogous to an int[]. A vector<vector<int> > is like an int*[], where each int* points to an array. It's not like a two-dimensional array - each of the inner vectors can have different sizes. Prefixing a variable with an & makes that variable a reference. A reference is like a pointer that you don't have to explicitly dereference. Passing parameters by reference is a common C++ idiom, which is used in many of the same situations as passing by pointer in C.

vector is an array which is resized automatically. So vector<vector<int>> would be an array of int-arrays, equivalent to the C int*[]. &mat is a reference to a mat, similiar to pointers (in fact I think C99 supports references). However, in this case since the value being passed in is already a pointer, it is not really needed. So the equivalent in C would be void BuildDoublyEvenMagicSquare(int*[] mat, int Order);

You can get rid of the #includes and the 'using namespace std' line. The only difficult bit now is your vector. What's being passed here is a two dimensional array of ints which is easy enough in C. The difficult bit is resizing it if you don't know the bounds at the outset. That's why vector is so nice - you don't need to care. For more general C++ to C connversion I'd suggest you get a book like "C++ for C programmers" and work from the index back. Even better, work from the beginning to the end and learn C++. You'll probably find that if the program is in any way complicated, there's going to be some things which are pretty tricky to do in C from C++. Good luck!!

Vector is pretty much C++ for an array. There are ways to dynamicly resize vectors (without resorting to realloc()), but otherwise that is pretty much what you are looking at. If you see & in a parameter list, it means "pass this parameter by reference". In C parameters inside a routine are a copy of what was passed in, so if you modify them that modificaion doesn't go outside the function. However, if you modify a C++ reference parameter, you are also modifying the variable the caller used for that parameter. So to get the equivalent of <vector<vector<int>> & mat in C, you'd probably pass that parameter as something like int ** mat[], with the assumption that the user is passing in a pointer to an array of int arrays that they want you to work on. The difference is that inside the routine your C code would have to be doing a *mat to get at the array of int arrays, whereas in the C++ code they can just use mat directly.

### Sorting large std::vector of custom objects

I am creating a sparse matrix in CSR format, for which I start with a vector of matrix element structures. It needs to be std::vector at the beginning because I don't know ahead of time how many non-zeros my matrix is going to have. Then, to fill up the appropriate arrays for the CSR matrix, I need to first sort this array of non-zeros, in the order they appear in the matrix if one goes through it line-by-line. But above a certain matrix size (roughly 1 500 000 non-zeros), the sorted vector does not start from the beginning of the matrix. It is still sorted, but starts around row 44000. // Matrix element struct: struct mel { int Ncols; int row,col; MKL_Complex16 val; void print(); }; // Custom function for sorting: struct less_than_MElem { inline bool operator() (const mel& ME1, const mel& ME2) { return ( ( ME1.row*ME1.Ncols+ME1.col ) < ( ME2.row*ME2.Ncols+ME2.col ) ); } }; int main() { std::vector<mel> mevec; /* long piece of code that fills up mevec */ std::sort( mevec.begin(), mevec.end(), less_than_MElem() ); return 0; } I thought maybe as the vector was grown dynamically it wound up in separate blocks in the memory and the iterator wasn't pointing at the genuine beginning/end anymore. So I have tried creating a new vector and started with resizing it to the size that is known by that time. Then copied the elements one-by-one into this new vector and sorted it, but the result was the same. Nelements = mevec.size(); std::vector<mel> nzeros; nzeros.resize(Nelements); for( int i = 0; i < Nelements; i++ ) { nzeros[i].Ncols = mevec[i].Ncols; nzeros[i].row = mevec[i].row; nzeros[i].col = mevec[i].col; nzeros[i].val = mevec[i].val; } std::sort( nzeros.begin(), nzeros.end(), less_than_MElem() ); Can anyone think of a solution?

### Error while passing 2-D array of variable size to a function

I am getting this error while running the code: [Error] array bound is not an integer constant before ']' token Here is a segment of the code: using namespace std; int R,C; bool isSafer(int grid[][C],int row,int col, bool visited[][C]) { if(row<R && row>=0 && col<C && col>=0 && grid[row][col] && visited[row][col]) { return true; } return false; } int main() { .... .... cin>>R>>C; int grid[R][C]; .... } In int main() I ask user for the input for R and C. I have also declared the array in the main function and called in in the above mentioned function. Please suggest me how should I pass my array to the function with the parameter as variable taken by the user.

There is no variable-length arrays in C++. It means, following code int w, h; std::cin >> w >> h; int a[w][h]; is illegal. If it works for you, it's because your compiler supports it as an extension (as an additional feature that is not part of standard C++). Other compilers may not support it. Even if it works for you, there is no way you can pass it to function. There are following soltions: Use nested std::vector. It's easy, but it may be slightly slow and/or memory-expensive for 2D arrays. Convert 2D array to 1D array of ints and pass width separately. Best solution in my opinion. Use 1D array of pointers to new-allocated 1D arrays of ints. And pass it as int **param. If you only need to change one dimension of the array, you can do something like constexpr int w = 5; int *a[w] = new int[h][w]; This won't work if you need to change both dimensions.

### Printing the First Array in a Deque of Structs

I have a Deque that contains this kind of stucts. struct New_Array { array<array<int,4>,4> mytable; int h; }; In this stuct 2 different arrays may have same value of h. deque<New_Array> Mydeque; I also know how many different h are in the deque(the value of steps). And how many stucts are in the deque(Mydeque.size()). I need to print one array for each h. Starting from h=0 till h=steps (steps is a known int value). Each array that is going to be printed must be the closer to the end of the deque. I tried something like this: void foo(deque<New_Array> Mydeque, int steps) for(int i=0; i<steps; i++) { deque<New_Array>::iterator it; it = find(Mydeque.begin(),Mydeque.end(),i); PrintBoard(*it); // This if a function where you enter the New_Array struct // and it prints the array } } The above gives me : error C2679: binary '==' : no operator found which takes a right-hand operand of type 'const bool' (or there is no acceptable conversion) Or something like this: void foo(deque<New_Array> Mydeque, int steps) for(int i=0; i<steps; i++) { deque<New_Array>::iterator it; for(unsigned int j=0;j<Mydeque.size();j++) { it = find_if(Mydeque.begin(),Mydeque.end(),Mydeque[j].h==i); PrintBoard(*it); break; } } The above gives me: error C2064: term does not evaluate to a function taking 1 arguments EDIT: The deque is not sorted. For each h an array should be printed. This array should be the one that is at this moment closer to the end of the deque.

Remember the last value and skip: assert(!Mydeque.empty()); int old_h = Mydeque[0].h + 1; // make sure it's different! for (std::size_t i = 0, end != Mydeque.size(); i != end; ++i) { if (Mydeque[i].h == old_h) continue; print(Mydeque[i]); old_h = Mydeque[i].h; }

Firstly, note that you declare your std::array on the stack, so the storage will also be on the stack. This means that iterating over the structure involves loading a (4*4+1)*int for each comparison. If this is performance-sensitive, I would suggest using std::vector since the load will be only of the outer vector pointer and the h when only comparing h. struct New_Array { vector<vector<int,4>,4> mytable; int h; }; Secondly, if you need to access these tables through their h values, or access all the tables with a given h at once, make it easier for everyone and store them as vectors in a map, or a sorted vector of vectors: std::map<int,std::vector<New_Array> > rolodex; rolodex[someNewArray.h].push_back(someNewArray); If you construct this in-order, then the first item in each vector will be the one to print: for(auto it : rolodex) { vector<New_Array> tablesForThisH = it->second; if(tablesForThisH.begin() != tablesForThisH.end()) PrintBoard(it->second[0]); } Since std:map stores (and iterates) its keys in ascending (I think) order, this will run over the different h values in ascending order. Again it will only need to load the stack-stored struct, which is just the h int and the vector header (probably 12 bytes, as mentioned in this question). Forgive me if the code is wrong, my stl is a little rusty.

Loop through the deque, and insert all elements into a map, using h as the key. Since your set of h values seems to be sequential, you can use a vector instead, but testing whether an element has already been found will be more difficult.

The solution is : void Find_Solution_Path(deque<New_Array> Mydeque, int steps) { for(int i=0; i<steps+1; i++) { for(int j=Mydeque.size()-1;j>-1;j--) { if (Mydeque[j].h==i) { PrintBoard(Mydeque[j]); cout<<endl; break; } } } }