Search

C++ - Concepts and Examples about: std::less_equal, std::variant and std::visit

New episode about STL.


C++ - Concepts and Examples about: std::less_equal, std::variant and std::visit

std::less_equals

Is a function object class used to perform comparisons. It is defined as a function object class for less than equality comparison that returns a Boolean value depending on the condition.

That is, just use the same example to order in ascending order:

Example:

#include <iostream>
#include <algorithm>

int main(){
    int array[] = {11, 2, 9, 17, 12, 89, 13, 2, 52, 8, 4, 79},
          length = sizeof( array ) / sizeof( array[0] );

    std::sort( array, array + length, std::less_equal<int>() );

    for ( auto nums : array ) {
      std::cout << nums << ' ';
    }
    std::cout << '\n';

    return 0;
}

Output: 2 4 8 9 11 12 13 17 52 79 89

Simple to understand, right?! That is, put in order of less than or equal.


std::variant and std::visit

The std::variant is as its name assumes, the type can vary, it is not the same as using auto(needs initialization and cannot change type). That is, with std::variant you can say that the var variable can be: int, std::string, char *,…

Std::visit is the way we will handle this data delivered by std::variant, that is, when you create a function it has a return type, so std::visit is almost the same idea directly linked to polymosphism. It is often used in conjunction with lambda expressions.

Example: Create a variable and pass its value to a function, in this case: lambda.

#include <iostream>
#include <variant> // para usar o std::variant

int main(){
  std::variant<int, std::string, char *> var;

  var = "I am string!";

  std::visit( []( auto &e ) {
      std::cout << e << '\n';
      }, var );

  var = 936;

  visit( []( auto &e ) {
      std::cout << e << '\n';
      }, var );

  var = "And now i am char pointer.";

  visit( []( auto &e ) {
      std::cout << e << '\n';
      }, var );

  return 0;
}

Some things to look at in the code above:

  • Need to include variant lib: std::variant;
  • I chose 3 different types for my var variable: int, std::string and char *;
  • Scope resolution in std::visit can be optional: now I used: std::visit and once only: visit;
  • And finally, you will only be able to compile this code with C++17.

Well, for a quick and easy-to-understand analysis this is it! If you want to go deeper I suggest these links: std::less_equal, std::variant and std::visit .

That’s all for today, small daily doses that will always keep us in tune with C++!


cppdaily cpp


Share



Comments