Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the largest int in a std::set<int>?

Tags:

c++

stl

stdset

I have a std::set<int>, what's the proper way to find the largest int in this set?

like image 710
leeeroy Avatar asked Aug 27 '09 16:08

leeeroy


People also ask

How do you find the largest element in a set in C++?

The max_element() function returns an iterator to the largest element in the range [start,end).

How do you find the max int in C++?

Values of INT_MAX and INT_MIN may vary from compiler to compiler. Following are typical values in a compiler where integers are stored using 32 bits. Value of INT_MAX is +2147483647. Value of INT_MIN is -2147483648.

How do you find the maximum element in a set?

begin() and set. end() methods Approach: Elements in a set are stored in sorted order. So the minimum element of the set will reside in the first element and the maximum element in the last element. Therefore, this first and last element can be fetched with the help of set.

How do you find Max Int?

To find the max value for the unsigned integer data type, we take 2 to the power of 16 and substract by 1, which would is 65,535 . We get the number 16 from taking the number of bytes that assigned to the unsigned short int data type (2) and multiple it by the number of bits assigned to each byte (8) and get 16.

What is the difference between int and bigint data types?

The int data type is the primary integer data type in SQL Server. The bigint data type is intended for use when integer values might exceed the range that is supported by the int data type.

How to find the maximum and minimum element of set in C++?

Given a Set, the task is to find the maximum and minimum element of this set in C++ STL. Approach: Elements in a set are stored in sorted order. So the minimum element of the set will reside in the first element and the maximum element in the last element.

How do you use the int function in Excel?

The INT function allows you to return the nearest integer less than or equal to a specified value. This will return 11. This will return -12. INT can be used in both TI’s and Rules. It works identically to the same function in Excel.

How to use set in C++ STL?

We use set in C++ STL. 1) Insert all elements into a set. 2) Traverse the set and print k-th element. Time complexity of above solution is O (n Log n). Note that set in STL uses a self-balancing BST internally and therefore time complexity of search and insert operations is O (log n).


1 Answers

What comparator are you using?

For the default this will work:

if(!myset.empty())     *myset.rbegin(); else     //the set is empty 

This will also be constant time instead of linear like the max_element solution.

like image 145
CTT Avatar answered Oct 05 '22 23:10

CTT