Skip to content

Commit 8a0ad09

Browse files
authored
add function to max_size_index
Function to return the element index number of the vector with the largest length in a 2d array.
1 parent cf0efb9 commit 8a0ad09

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

max2darrayidx.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <algorithm>
4+
5+
using namespace std;
6+
7+
//Function to return the element index number of the vector with the largest length in a 2d array.
8+
size_t max_size_index(vector<vector<int>> v) {
9+
decltype(v)::iterator m = max_element(v.begin(), v.end(), [](vector<int> a, vector<int> b) {
10+
return a.size() < b.size(); });
11+
size_t index = std::distance(v.begin(), m);
12+
return index;
13+
14+
}
15+
16+
17+
18+
19+
20+
int main() {
21+
vector<vector<int>> array2d = { {1,2,3}, {4,5,6}, {7,8,9,10} };
22+
23+
auto i = max_size_index(array2d);
24+
//return 2
25+
cout << i << endl;
26+
return 0;
27+
}

0 commit comments

Comments
 (0)