以下是引用zklhp在2014-11-27 12:44:40的发言:
楼主这是C++ 不是C
程序代码:
// g++ -Wall -fomit-frame-pointer -funroll-loops -Ofast -march=corei7-avx -msse4.2 -mavx -std=c++11 max_min.cpp -lm -o max_min
#include <iostream>
#include <vector>
#include <algorithm>
using std::cout;
using std::endl;
using std::distance;
using std::vector;
using std::max_element;
using std::min_element;
int main(void)
{
int tmp[] = {1,5,3,6,7,2,5,9,4,8};
vector<int> num(tmp, tmp + sizeof(tmp)/sizeof(int));
auto it_max = max_element(num.begin(), num.end());
auto pos_max = distance(num.begin(), it_max);
auto it_min = min_element(num.begin(), num.end());
auto pos_min = distance(num.begin(), it_min);
cout << "最大值: " << *it_max << endl;
cout << "所在位置" << pos_max << endl;
cout << "最小值: " << *it_min << endl;
cout << "所在位置" << pos_min << endl;
return 0;
}
泛型编程美醉了