如何得到向量的最大值(或最小值)?

时间:2021-06-28 04:14:44

How can I get the max (or min) value in a vector in C++?

如何在c++中得到向量的最大值(或最小值)?

I have seen a few solutions for this on Google but none of them made sense to me :(

我在谷歌上看到了一些解决方案,但没有一个对我有意义。

Can someone explain in an easy straightforward noob way how to get the max or min value from a vector please? and am I wrong in assuming it would be more or less the same with an array?

有人能简单直接地解释一下如何从矢量中获取最大或最小值吗?假设一个数组或多或少都是相同的,我错了吗?

I need an iterator right? I tried it with max_element but kept getting an error?

我需要一个迭代器,对吗?我用max_element试过,但一直得到错误?

vector<int>::const_iterator it;
it = max_element(cloud.begin(), cloud.end());

error: request for member ‘begin’ in ‘cloud’, which is of non-class type ‘int [10]’

错误:请求“cloud”中的成员“begin”,它是非类类型的“int[10]”

EDIT: I was not able to answer my own ??? so I'll put it here...

编辑:我答不上来???所以我把它放在这里……

Wow, thanks for the fast replies! I ended up doing it this way, do think its ok?

哇,谢谢你的快速回复!最后我这么做了,你觉得可以吗?

for (unsigned int i = 0; i < cdf.size(); i++)
  if (cdf[i] < cdfMin)
    cdfMin = cdf[i];

where cdf is a vector.

cdf是一个向量。

9 个解决方案

#1


75  

Using c++11/c++0x compile flags, you can

可以使用c++11/c++0x编译标志

auto it = max_element(std::begin(cloud), std::end(cloud)); // c++11

Otherwise, write your own:

否则,编写自己的:

template <typename T, size_t N> const T* mybegin(const T (&a)[N]) { return a; }    
template <typename T, size_t N> const T* myend  (const T (&a)[N]) { return a+N; }

See it live at http://ideone.com/aDkhW:

请访问http://ideone.com/aDkhW:

#include <iostream>
#include <algorithm>

template <typename T, size_t N> const T* mybegin(const T (&a)[N]) { return a; }    
template <typename T, size_t N> const T* myend  (const T (&a)[N]) { return a+N; }

int main()
{
    const int cloud[] = { 1,2,3,4,-7,999,5,6 };

    std::cout << *std::max_element(mybegin(cloud), myend(cloud)) << '\n';
    std::cout << *std::min_element(mybegin(cloud), myend(cloud)) << '\n';
}

Oh, and use std::minmax_element(...) if you need both at once :/

噢,如果您同时需要两个:/,那么使用std::minmax_element(…)

#2


36  

If you want to use the function std::max_element(), the way you have to do it is:

如果您想使用std::max_element()函数,那么您必须这样做:

double max = *max_element(vector.begin(), vector.end());
cout<<"Max value: "<<max<<endl;

I hope this can help.

我希望这能有所帮助。

#3


7  

Assuming cloud is int cloud[10] you can do it like this: int *p = max_element(cloud, cloud + 10);

假设云是int cloud[10],你可以这样做:int *p = max_element(cloud, cloud + 10);

#4


4  

Let,

让,

 #include<vector>

 vector<int>v{1,2,3,-1,-2,-3};

If the vector is sorted in ascending or descending order then you can find it with complexity O(1).

如果这个向量按升序或降序排序,那么你就可以在复杂度O(1)中找到它。

For a vector of ascending order the first element is the smallest element, you can get it by v[0] (0 based indexing) and last element is the largest element, you can get it by v[sizeOfVector-1].

对于一个升序向量,第一个元素是最小的元素,你可以用v[0](基于0的索引)来表示,最后一个元素是最大的元素,你可以用v[sizeOfVector-1]来表示。

If the vector is sorted in descending order then the last element is the smallest element,you can get it by v[sizeOfVector-1] and first element is the largest element, you can get it by v[0].

如果向量按降序排序,那么最后一个元素就是最小的元素,你可以用v[sizeOfVector-1]来表示,第一个元素就是最大的元素,你可以用v[0]来表示。

If the vector is not sorted then you have to iterate over the vector to get the smallest/largest element.In this case time complexity is O(n), here n is the size of vector.

如果这个向量没有被排序,那么你必须对这个向量进行迭代以得到最小/最大的元素。在这种情况下,时间复杂度是O(n),这里n是向量的大小。

int smallest_element=v[0] //let, first element is the smallest one
int largest_element = v[0] //also let, first element is the biggest one
for(int i =1;i<sizeOfVector;i++)  //start iterating from the second element
{
if(v[i]<smallest_element)
    {
       smallest_element=arr[i];
    }
if(v[i]>largest_element)
    {
       largest_element=v[i];
    }
}

You can use iterator,

您可以使用迭代器,

for (vector<int>:: iterator it=v.begin(); it!=v.end(); it++)
{
if(*it<smallest_element) //used *it (with asterisk), because it's an iterator
    {
      smallest_element=*it;
    }
if(*it>largest_element)
    {
      largest_element=*it;
    }
}

You can calculate it in input section (when you have to find smallest or largest element from a given vector)

你可以在输入部分计算它(当你必须从给定的向量中找到最小或最大的元素)

int smallest_element,largest_element,value;
vector<int>v;
int n;//n is the number of elements to enter
cin>>n;
for(int i = 0;i<n;i++)
{
 cin>>value;
if(i==0)
{
smallest_element= value; //smallest_element=v[0];
largest_element= value; //also, largest_element = v[0]
}

if(value<smallest_element and i>0)
{
smallest_element = value;
}

if(value>largest_element and i>0)
{
largest_element = value;
}
v.push_back(value);
}

Also you can get smallest/largest element by built in functions

也可以通过内建函数得到最小/最大的元素

#include<algorithm>

int smallest_element = *min_element(v.begin(),v.end());

int largest_element  = *max_element(v.begin(),v.end());

You can get smallest/largest element of any range by using this functions. such as,

通过使用这些函数,可以得到任何范围内最小/最大的元素。例如,

vector<int>v {1,2,3,-1,-2,-3};

cout<<*min_element(v.begin(),v.begin()+3); //this will print 1,smallest element of first three elements

cout<<*max_element(v.begin(),v.begin()+3); //largest element of first three elements

cout<<*min_element(v.begin()+2,v.begin()+5); // -2, smallest element between third and fifth element (inclusive)

cout<<*max_element(v.begin()+2,v.begin()+5); //largest element between third and first element (inclusive)

I have used asterisk (*), before min_element()/max_element() functions. Because both of them return iterator. All codes are in c++.

在min_element()/max_element()函数之前,我使用了星号(*)。因为它们都返回迭代器。所有代码都是c++的。

#5


3  

In c++11, you can use some function like that:

在c++11中,可以使用如下函数:

int maxAt(std::vector<int>& vector_name) {
    int max = INT_MIN;
    for (auto val : vector_name) {
         if (max < val) max = val;
    }
    return max;
}

#6


3  

You can print it directly using max_element/min_element function. Eg:

可以使用max_element/min_element函数直接打印它。例如:

  cout<<*max_element(v.begin(),v.end());

  cout<<*min_element(v.begin(),v.end());

#7


1  

If you want to use an iterator, you can do a placement-new with an array.

如果您想使用迭代器,可以使用数组执行一个新位置。

std::array<int, 10> icloud = new (cloud) std::array<int,10>;

Note the lack of a () at the end, that is important. This creates an array class that uses that memory as its storage, and has STL features like iterators.

注意最后缺少a(),这一点很重要。这将创建一个数组类,该数组类使用该内存作为其存储,并具有诸如迭代器之类的STL特性。

(This is C++ TR1/C++11 by the way)

(顺便说一下,这是c++ TR1/ c++ 11)

#8


1  

You can use max_element to get the maximum value in vector. The max_element returns an iterator to largest value in the range, or last if the range is empty. As an iterator is like pointers (or you can say pointer is a form of iterator), you can use a * before it to get the value. So as per the problem you can get the maximum element in an vector as:

您可以使用max_element来获取向量中的最大值。max_element将迭代器返回到范围中的最大值,如果范围为空,则返回最后值。因为迭代器就像指针(或者您可以说指针是迭代器的一种形式),您可以在它获得值之前使用*。根据这个问题你可以得到向量的最大元素为:

int max=*max_element(cloud.begin(), cloud.end());

It will give you the maximum element in your vector "cloud". Hope it helps.

它将给你向量“云”中的最大元素。希望它可以帮助。

#9


-5  

#include <stdlib.h>
#include <stdio.h>

int main()
{

    int vector[500];

    vector[0] = 100;
    vector[1] = 2;
    vector[2] = 1239;
    vector[3] = 5;
    vector[4] = 10;
    vector[5] = 1;
    vector[6] = 123;
    vector[7] = 1000;
    vector[8] = 9;
    vector[9] = 123;
    vector[10] = 10;

    int i = 0;

    int winner = vector[0];

    for(i=0;i < 10; i++)
    {
        printf("vector = %d \n", vector[i]);

        if(winner > vector[i])
        {
            printf("winner was %d \n", winner);
            winner = vector[i];
            printf("but now is %d \n", winner);
        }
    }

    printf("the minimu is %d", winner);
}

The complet nooby way... in C

完整的nooby方式……在C语言中

#1


75  

Using c++11/c++0x compile flags, you can

可以使用c++11/c++0x编译标志

auto it = max_element(std::begin(cloud), std::end(cloud)); // c++11

Otherwise, write your own:

否则,编写自己的:

template <typename T, size_t N> const T* mybegin(const T (&a)[N]) { return a; }    
template <typename T, size_t N> const T* myend  (const T (&a)[N]) { return a+N; }

See it live at http://ideone.com/aDkhW:

请访问http://ideone.com/aDkhW:

#include <iostream>
#include <algorithm>

template <typename T, size_t N> const T* mybegin(const T (&a)[N]) { return a; }    
template <typename T, size_t N> const T* myend  (const T (&a)[N]) { return a+N; }

int main()
{
    const int cloud[] = { 1,2,3,4,-7,999,5,6 };

    std::cout << *std::max_element(mybegin(cloud), myend(cloud)) << '\n';
    std::cout << *std::min_element(mybegin(cloud), myend(cloud)) << '\n';
}

Oh, and use std::minmax_element(...) if you need both at once :/

噢,如果您同时需要两个:/,那么使用std::minmax_element(…)

#2


36  

If you want to use the function std::max_element(), the way you have to do it is:

如果您想使用std::max_element()函数,那么您必须这样做:

double max = *max_element(vector.begin(), vector.end());
cout<<"Max value: "<<max<<endl;

I hope this can help.

我希望这能有所帮助。

#3


7  

Assuming cloud is int cloud[10] you can do it like this: int *p = max_element(cloud, cloud + 10);

假设云是int cloud[10],你可以这样做:int *p = max_element(cloud, cloud + 10);

#4


4  

Let,

让,

 #include<vector>

 vector<int>v{1,2,3,-1,-2,-3};

If the vector is sorted in ascending or descending order then you can find it with complexity O(1).

如果这个向量按升序或降序排序,那么你就可以在复杂度O(1)中找到它。

For a vector of ascending order the first element is the smallest element, you can get it by v[0] (0 based indexing) and last element is the largest element, you can get it by v[sizeOfVector-1].

对于一个升序向量,第一个元素是最小的元素,你可以用v[0](基于0的索引)来表示,最后一个元素是最大的元素,你可以用v[sizeOfVector-1]来表示。

If the vector is sorted in descending order then the last element is the smallest element,you can get it by v[sizeOfVector-1] and first element is the largest element, you can get it by v[0].

如果向量按降序排序,那么最后一个元素就是最小的元素,你可以用v[sizeOfVector-1]来表示,第一个元素就是最大的元素,你可以用v[0]来表示。

If the vector is not sorted then you have to iterate over the vector to get the smallest/largest element.In this case time complexity is O(n), here n is the size of vector.

如果这个向量没有被排序,那么你必须对这个向量进行迭代以得到最小/最大的元素。在这种情况下,时间复杂度是O(n),这里n是向量的大小。

int smallest_element=v[0] //let, first element is the smallest one
int largest_element = v[0] //also let, first element is the biggest one
for(int i =1;i<sizeOfVector;i++)  //start iterating from the second element
{
if(v[i]<smallest_element)
    {
       smallest_element=arr[i];
    }
if(v[i]>largest_element)
    {
       largest_element=v[i];
    }
}

You can use iterator,

您可以使用迭代器,

for (vector<int>:: iterator it=v.begin(); it!=v.end(); it++)
{
if(*it<smallest_element) //used *it (with asterisk), because it's an iterator
    {
      smallest_element=*it;
    }
if(*it>largest_element)
    {
      largest_element=*it;
    }
}

You can calculate it in input section (when you have to find smallest or largest element from a given vector)

你可以在输入部分计算它(当你必须从给定的向量中找到最小或最大的元素)

int smallest_element,largest_element,value;
vector<int>v;
int n;//n is the number of elements to enter
cin>>n;
for(int i = 0;i<n;i++)
{
 cin>>value;
if(i==0)
{
smallest_element= value; //smallest_element=v[0];
largest_element= value; //also, largest_element = v[0]
}

if(value<smallest_element and i>0)
{
smallest_element = value;
}

if(value>largest_element and i>0)
{
largest_element = value;
}
v.push_back(value);
}

Also you can get smallest/largest element by built in functions

也可以通过内建函数得到最小/最大的元素

#include<algorithm>

int smallest_element = *min_element(v.begin(),v.end());

int largest_element  = *max_element(v.begin(),v.end());

You can get smallest/largest element of any range by using this functions. such as,

通过使用这些函数,可以得到任何范围内最小/最大的元素。例如,

vector<int>v {1,2,3,-1,-2,-3};

cout<<*min_element(v.begin(),v.begin()+3); //this will print 1,smallest element of first three elements

cout<<*max_element(v.begin(),v.begin()+3); //largest element of first three elements

cout<<*min_element(v.begin()+2,v.begin()+5); // -2, smallest element between third and fifth element (inclusive)

cout<<*max_element(v.begin()+2,v.begin()+5); //largest element between third and first element (inclusive)

I have used asterisk (*), before min_element()/max_element() functions. Because both of them return iterator. All codes are in c++.

在min_element()/max_element()函数之前,我使用了星号(*)。因为它们都返回迭代器。所有代码都是c++的。

#5


3  

In c++11, you can use some function like that:

在c++11中,可以使用如下函数:

int maxAt(std::vector<int>& vector_name) {
    int max = INT_MIN;
    for (auto val : vector_name) {
         if (max < val) max = val;
    }
    return max;
}

#6


3  

You can print it directly using max_element/min_element function. Eg:

可以使用max_element/min_element函数直接打印它。例如:

  cout<<*max_element(v.begin(),v.end());

  cout<<*min_element(v.begin(),v.end());

#7


1  

If you want to use an iterator, you can do a placement-new with an array.

如果您想使用迭代器,可以使用数组执行一个新位置。

std::array<int, 10> icloud = new (cloud) std::array<int,10>;

Note the lack of a () at the end, that is important. This creates an array class that uses that memory as its storage, and has STL features like iterators.

注意最后缺少a(),这一点很重要。这将创建一个数组类,该数组类使用该内存作为其存储,并具有诸如迭代器之类的STL特性。

(This is C++ TR1/C++11 by the way)

(顺便说一下,这是c++ TR1/ c++ 11)

#8


1  

You can use max_element to get the maximum value in vector. The max_element returns an iterator to largest value in the range, or last if the range is empty. As an iterator is like pointers (or you can say pointer is a form of iterator), you can use a * before it to get the value. So as per the problem you can get the maximum element in an vector as:

您可以使用max_element来获取向量中的最大值。max_element将迭代器返回到范围中的最大值,如果范围为空,则返回最后值。因为迭代器就像指针(或者您可以说指针是迭代器的一种形式),您可以在它获得值之前使用*。根据这个问题你可以得到向量的最大元素为:

int max=*max_element(cloud.begin(), cloud.end());

It will give you the maximum element in your vector "cloud". Hope it helps.

它将给你向量“云”中的最大元素。希望它可以帮助。

#9


-5  

#include <stdlib.h>
#include <stdio.h>

int main()
{

    int vector[500];

    vector[0] = 100;
    vector[1] = 2;
    vector[2] = 1239;
    vector[3] = 5;
    vector[4] = 10;
    vector[5] = 1;
    vector[6] = 123;
    vector[7] = 1000;
    vector[8] = 9;
    vector[9] = 123;
    vector[10] = 10;

    int i = 0;

    int winner = vector[0];

    for(i=0;i < 10; i++)
    {
        printf("vector = %d \n", vector[i]);

        if(winner > vector[i])
        {
            printf("winner was %d \n", winner);
            winner = vector[i];
            printf("but now is %d \n", winner);
        }
    }

    printf("the minimu is %d", winner);
}

The complet nooby way... in C

完整的nooby方式……在C语言中