Newbie c++: find and save the max, min Index of vector

Dear Experts,
I am a regular Matlab User and just switched to Cpp for a small project, thing are quite different and difficult than what I think as compared to Matlab coding.

Description: as an example I am creating a gain_table vector that for the moment stores incremental values from x to y range. I would like to find the max or min values and their Index from this vector and store it as I need the Index number of Max to get value from the same index in another vector. A friend of mine gave me his max function but he made it for an array, how can I modify it for vectors and call back in the code.

Questions
1: usually which approach is better: Vectors or Arrays, as I understand Arrays can be matrices also.
2: I have used the max_element command that display the correct values and Indexes, but how can I store this Index number for future use.
below is my basic code, that can be modified accordingly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;
#include <unistd.h>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <stdlib.h>
#include <vector>
#include <algorithm>    // std::min_element, std::max_element

int main()
{
	double gain_start=0;
	double gain_stop= 15;

	std::vector<int> gain_table (0);
	std::vector<int>::iterator itr;
	for (int gain=gain_start; gain<gain_stop; gain++)
	{ 		gain_table.push_back(gain); 	}
	std::cout << "gain_table size: "<< int(gain_table.size())  << " elements\n"<<endl;

	std::cout << "gain_table Elements are : \n";
	for (itr=gain_table.begin(); itr<gain_table.end(); itr++)
	std::cout << ' ' << *itr<<endl;

	std::vector<int>::iterator result;
	result = max_element(gain_table.begin(), gain_table.end());
	std::cout << "max element value is: " << *result<<endl;
	std::cout << "max element index is: " << std::distance(gain_table.begin(), result) << '\n';

    return 0;
}

/////////////////////MAX//////////////////////////
float max(float arr[],int &index,int length)
{
	float temp = arr[0];
	for (int i = 0;i<length;i++)
	{
		if (arr[i]>temp)
		{
			temp=arr[i];
			index = i;

		};

	};

	return temp;
};



any comments or suggestion would be greatly appreciated.

Best Regards
Ali
1. Vectors can be resized dynamically. That is usually a significant advantage over arrays.

2. You already have the index number on line 29. Store in variable.
> usually which approach is better: Vectors or Arrays,

If the size is known at compile-time, array or preferably std::array<>
If not, std::vector<>

When in doubt, use std::vector<>; you won't go wrong.

> as I understand Arrays can be matrices also.

Vectors too (logically): std::vector< std::vector<int> >


> but how can I store this Index number for future use.

For the duration of the program, it can be saved in a variable. (To persist across multiple runs, write to a file).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    // std::srand elided

    std::vector<int> seq { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 9, 4, 4, 4 } ;
    std::random_shuffle( seq.begin(), seq.end() ) ;
    for( int v : seq ) std::cout << v << ' ' ;
    std::cout << '\n' ;

    // http://en.cppreference.com/w/cpp/algorithm/minmax_element
    const auto pair = minmax_element( seq.begin(), seq.end() ) ;

    const int min_element = *pair.first ;
    const std::size_t min_element_pos = pair.first - seq.begin() ;

    const int max_element = *pair.second ;
    const std::size_t max_element_pos = pair.second - seq.begin() ;

    std::cout << "min element " << min_element
              << " first occurrence at " << min_element_pos << '\n'

              << "max element " << max_element
              << " last occurrence at " << max_element_pos << '\n' ;
}

http://coliru.stacked-crooked.com/a/76bbe69e0c8bb40d
Hallo JLBorges,
Thanks alot for your response, I have tried to run your code as a standalone just for test purpose and I am getting these errors.

warning: ‘auto’ changes meaning in C++11; please remove it [-Wc++0x-compat]
const auto pair = minmax_element( seq.begin(), seq.end() ) ;

error: invalid use of template-name ‘std::pair’ without an argument list

const int min_element = *pair.first ;
error: missing template arguments before ‘.’ token

am I missing something here??

That code requires C++11.

Either compile with -std=c++0x

Or try this legacy C++ code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <iostream>
#include <vector>
#include <algorithm>

int main()
{
    // std::srand elided
    const int a[15] =  { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 } ;
    std::vector<int> seq( a, a+15 ) ;
    std::random_shuffle( seq.begin(), seq.end() ) ;
    for( std::size_t i = 0 ; i < seq.size() ; ++i ) std::cout << seq[i] << ' ' ;
    std::cout << '\n' ;

    const std::vector<int>::iterator iter_min = std::min_element( seq.begin(), seq.end() ) ;
    const int min_element = *iter_min ;
    const std::size_t min_element_pos = iter_min - seq.begin() ;

    const std::vector<int>::iterator iter_max = std::max_element( seq.begin(), seq.end() ) ;
    const int max_element = *iter_max ;
    const std::size_t max_element_pos = iter_max - seq.begin() ;

    std::cout << "min element " << min_element
              << " occurrence at " << min_element_pos << '\n'

              << "max element " << max_element
              << " occurrence at " << max_element_pos << '\n' ;
}

http://coliru.stacked-crooked.com/a/50a237622979e24a
Topic archived. No new replies allowed.