There is a certain multidimensional array, with ten arrays, in each array of N elements.

list<int>route; vector<list<int>>routes; route.push_back(.) routes.push_back(route) for(int start = 0 ; start = routes.size();start++) { for().. for().. } 

For example, in a multidimensional array, 3 arrays [1] = [10,20,30,40,1,20,14] [2] = [13,23,2,1,4] [3] = [3,2,1 5,13,2]

Task: It is necessary to output arrays using a loop and compare the elements of only the first array [10,20,30,40,1,20,14] with each other. Where to begin?

  • Can you clarify what the problem is? "How to build logic correctly?" This is too general a question. - yrHeTaTeJlb 8:25 pm
  • The problem is described, I cannot understand how to take elements from the first sheet, then take elements from the second sheet, considering the elements only in the necessary sheet - Misha Ostapchuk
  • in this example, the sheet is printed. You can process its contents in a similar way in a different way - yrHeTaTeJlb
  • Otherwise it's good =) - Misha Ostapchuk 8:40 pm

2 answers 2

 for(auto l: routes) { for(auto i: l) cout << i << " "; cout << endl; } 

This is to withdraw. Compare - what do you want to know? Are they all the same? Sort? What exactly - explain. If you check that everyone is the same, for example, then you need two iterators (because - see below - list very inefficient structure), something like this:

 auto i = l.begin(), j = i; ++j; for(;j != l.end(); ++j, ++i) { if (*i != *j) { /* Соседние элементы не равны */ } // Или if (*i == *j) { /* Соседние элементы равны */ } } 

If this is not needed - explain more clearly what is meant by

compare array elements with each other

That same note: a surprise awaits you, which you were told about - list extremely inefficient for all sorts, etc. - you can’t even address the element number in it just like that - it is an operation with the efficiency O(N) . It was necessary to take vector<vector<int>> , but, on the other hand, it is better to remember what you come to yourself by stuffing a couple of cones :)

By the way, in the vector<vector<int>> v you can refer to the m -th element of the n th vector simply as v[n][m] - it is clear that the indices are correct ... It is impossible to do with the list .

    If you need to compare to find out the same or not? If yes, then we take in the first and compare with the second. If the order in the second is not important. That, we take from the first and we look for in the second.