c++ stl 获取最小值_如何在C ++ STL中找到向量的最小/最小元素?

c++ stl 获取最小值

Given a vector and we have to minimum/smallest element using C++ STL program.

给定一个向量,我们必须使用C ++ STL程序最小/最小元素。

寻找向量的最小元素 (Finding smallest element of a vector)

To find a smallest or minimum element of a vector, we can use *min_element() function which is defined in <algorithm> header. It accepts a range of iterators from which we have to find the minimum / smallest element and returns the iterator pointing the minimum element between the given range.

找到向量的最小或最小元素 ,我们可以使用在<algorithm>标头中定义的* min_element()函数 。 它接受一定范围的迭代器,我们必须从中查找最小/最小元素,然后返回指向给定范围之间的最小元素的迭代器。

Note: To use vector – include <vector> header, and to use *min_element() function – include <algorithm> header or we can simply use <bits/stdc++.h> header file.

注意:要使用vector –包含<vector>头文件,并使用* min_element()函数 –包含<algorithm>头文件,或者我们可以简单地使用<bits / stdc ++。h>头文件。

Syntax:

句法:

    *min_element(iterator start, iterator end);

Here, iterator start, iterator end are the iterator positions in the vector between them we have to find the minimum value.

在这里, 迭代器开始,迭代器结束是它们之间向量中的迭代器位置,我们必须找到最小值。

Example:

例:

    Input:
    vector<int> v1{ 10, 20, 30, 40, 50, 25, 15 };

    cout << *min_element(v1.begin(), v1.end()) << endl;

    Output:
    10

C ++ STL程序查找向量的最小或最小元素 (C++ STL program to find minimum or smallest element of a vector )

//C++ STL program to find minimum or smallest 
//element of a vector 
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    //vector
    vector<int> v1{ 10, 20, 30, 40, 50 };

    //printing elements
    cout << "vector elements..." << endl;
    for (int x : v1)
        cout << x << " ";
    cout << endl;

    //finding the minimum element
    int min = *min_element(v1.begin(), v1.end());

    cout << "minimum/smallest element is: " << min << endl;

    return 0;
}

Output

输出量

vector elements...
10 20 30 40 50
minimum/smallest element is: 10


翻译自: https://www.includehelp.com/stl/find-the-minimum-smallest-element-of-a-vector.aspx

c++ stl 获取最小值

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C++数组模板封装,主要成员包括: public: Array(); Array(int size); Array(int size, T value); Array(Array<T> & other); Array(T * arr, int size); Array(int size, T* arr, int arrSize); template <typename T2> Array(Array<T2> & other); template <typename T2> Array(T2 * arr, int size); template <typename T2> Array(int size, T2 * arr, int arrSize); ~Array(void); inline int Size(); // 返回元素的个数 inline int MemorySize(); // 返回实际占用内存的字节数 inline void ReSize(int size); // 重新设定大小,清除所有的数据 inline T * Buffer(); // 获取内部数组的首地址 inline void SetValue(int index, T val); // 设置第index个数据的值 inline T GetValue(int index); // 获得第index个数据 T SquareOfNorm2(); // 2范式的平方 T SquareOfDistance(Array<T> & another); // 距离另一个数组的距离的平方 T Distance(Array<T> & another); // 计算两个数组的距离 Array<T> Cut(int start, int len); // 从向量截取一部分成为新的向量 Array<T> Cut(int start); // 将数组剪切一部分,返回新的数组,从start开始,一直到结束 T Norm0(); // 0范式,即求非0元素个数 T Norm1(); // 1范式,即求所有元素的和 T Norm2(); // 2范式,即求所有元素的平方和开根号 T Norm(int p = 2); // p范式 T Sum(); // 所有元素的和 void SetSmallValueToZero(); // 将绝对值小于eps的数值设置为0 T AbsMin(); // 返回绝对值的最小值 T Min(); // 返回最小值 T AbsMax(); // 返回绝对值的最大值 T Max(); // 返回最大值 void Offset(T value); // 所有数据偏移value T Average(); // 获得所有数据的平均值 void AverageTo(T value = 0); // 将数据平均化到某个数值 T Variance(); // 返回方差 void GaussianWhiten(); // 高斯白化,使得数组平均值为0,方差为1 void AbsValues(); // 所有的元素取绝对值 bool InfinityCheck(T maxValue = 1E50); // 数据检查,看数组是否有数据为无穷大 bool IsZero(); // 返回数据是否全部为0 void Clear(); // 将数据全部清0 void Normalization(); // 正规化,每个元素平方和为1 void SumAsOne(); // 是每个元素的和为1 void SetMinTo(T value); // 将小于value的元素全部设置为value void SetMaxTo(T value); // 将大于value的元素全部设置为value virtual void WriteToTextFile(string filename, bool append = false); virtual void WriteToTextFile(ofstream & o); virtual void ReadFromTextFile(string filename); virtual void ReadFromTextFile(ifstream & i); protected: inline void Init(int size); public: Array<T> & operator = (Array<T> & v); Array<T> & operator = (T value); T & operator[] (int index); template<typename T2> Array<T> & operator = (Array<T2> & v); template<typename T> friend ostream & operator << (ostream & out, Array<T> & vector); template<typename T> friend istream & operator >> (istream & in, Array<T> & vector);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值