PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, November 7, 2022

[FIXED] What is the role of greater <int> in set?

 November 07, 2022     c++, set, stl     No comments   

Issue

While declaring a set,

set <int, greater <int> > gquiz1;

why do we use greater <int>? What purpose does it serve?


Solution

std::set is a container that contains an ordered set of objects.

The ordering of the objects is determined by the second argument. By default, it is std::less<Key>. See the definition of std::set for additional details. However, you can override the default argument by using your own Compare type as the second argument, as you have done in your posted code.

E.g.

std::set<int> set1; // Use default compare class, std::less<int>
set1.insert(10);
set1.insert(5);
set1.insert(7);

The order of the objects in the above container will be 5, 7, and 10. The objects in the container are sorted in increasing orer.

If you use

std::set<int, std::greater<int>> set2;
set2.insert(10);
set2.insert(5);
set2.insert(7);

The order of the objects in the above container will be 10, 7, and 5. The objects in the container are sorted in decreasing orer.



Answered By - R Sahu
Answer Checked By - Timothy Miller (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing