Tuesday, August 29, 2017

C++11: Time: Durations

Durations can only be set with whole numbers. For example, to specify a 1.5 second duration, you need to specify 1500 milliseconds.


C++11: Time: Durations

C++11 has new duration types that can be used with the new time functions.

#include <chrono>
#include <iostream>

int main()
{
  std::chrono::nanoseconds  ns(9'000'000);
  std::chrono::microseconds us(60'000);
  std::chrono::milliseconds ms(300);
  std::chrono::seconds       s(1);

  std::chrono::high_resolution_clock::duration hrc_duration = s + ms + us + ns;

  double hrc_seconds = double(hrc_duration.count())                    *
                       std::chrono::high_resolution_clock::period::num /
                       std::chrono::high_resolution_clock::period::den;  

  std::cout << hrc_seconds << std::endl;

  return 0;
}
// Output: 1.369
Reference: http://en.cppreference.com/w/cpp/chrono/duration

C++11: Time

Three time systems were added. Here is an example:

#include <chrono>
#include <ctime>
#include <iostream>

int main()
{
////
  std::chrono::system_clock::time_point syc_now      = 
                                         std::chrono::system_clock::now();
  std::chrono::system_clock::time_point syc_default  ;
  std::chrono::system_clock::duration   syc_duration = syc_now -
                                                              syc_default;

  double syc_seconds = double(syc_duration.count())           *
                       std::chrono::system_clock::period::num /
                       std::chrono::system_clock::period::den; 

  std::cout << syc_seconds << std::endl;
////
  std::chrono::steady_clock::time_point stc_now     =                                             std::chrono::steady_clock::now();
  std::chrono::steady_clock::time_point stc_default ;
  std::chrono::steady_clock::duration   stc_duration = stc_now -
                                                            stc_default;

  double std_seconds = double(stc_duration.count())           *
                       std::chrono::steady_clock::period::num /
                       std::chrono::steady_clock::period::den;  

  std::cout << std_seconds << std::endl;
////
  std::chrono::high_resolution_clock::time_point hrc_now      =                                   std::chrono::high_resolution_clock::now();
  std::chrono::high_resolution_clock::time_point hrc_default  ;
  std::chrono::high_resolution_clock::duration   hrc_duration = 
                                                   hrc_now - hrc_default;

  double hrc_seconds = double(hrc_duration.count())                    *
                       std::chrono::high_resolution_clock::period::num /
                       std::chrono::high_resolution_clock::period::den;  

  std::cout << hrc_seconds << std::endl;
////
  std::time_t tt_now = std::time(nullptr);

  std::cout << tt_now << std::endl;
  return 0;
}
/* Output:
     1.50396e+09
     33785.6
     33785.6
     1503960804
*/
Reference: http://www.cplusplus.com/reference/chrono/

C++11: Regex: Replacements

You can do regex replacements. Here is an example:

#include <iostream>
#include <regex>
#include <string>

int main(int argc, char * argv[])
{
  std::string   myString("abcdef");
  std::smatch   myMatches;
  std::regex    myRegex("(a)(b)(c)(d)(e)(f)");

  std::cout << std::regex_replace(myString, myRegex, "$6-$5-$4-$3-$2-$1")
            << std::endl;
  return 0;
}
// Output: f-e-d-c-b-a
Reference: http://www.cplusplus.com/reference/regex/regex_replace/

C++11: Regex: Grep

You can use the regex facilities like grep. Here is an example:

#include <fstream>
#include <iostream>
#include <regex>
#include <string>

int main(int argc, char * argv[])
{
  if (argc != 3)
  {
    std::cerr << "Usage: grep <regex> <filename>" << std::endl;
    exit(1);
  }

  std::string   myString;
  std::smatch   myMatches;
  std::regex    myRegex(argv[1]);
  std::ifstream ifs(argv[2]);

  if (ifs.is_open())
  {
    while (std::getline(ifs, myString))
    {
      std::regex_search(myString, myMatches, myRegex);
      if (myMatches.size() == 1)
      {
        std::cout << myString << std::endl;
      }
    }
  }
  else
  {
    std::cout << "Unable to open file." << std::endl;
  }
  return 0;
}
/* Input:
     grep abc test.txt

   test.txt:
     abc
     def
     defabc
     hij
   
   Output:
    abc
    defabc
*/
Reference: http://www.cplusplus.com/reference/regex/regex_search/

C++11: Regex: Global

You can globally search a string as if using the Perl ‘g’ mode switch. Here is an example.

#include <iostream>
#include <regex>
#include <string>
 
int main()
{
  std::string myString("aeiouaeiouaeiou");
  std::regex  myRegex ("aeiou");
  std::smatch myMatches;
  std::string mySuffix;
  bool        keepLooping = true;
 
  keepLooping = true    ;
  mySuffix    = myString;
 
  while (keepLooping)
  {
    keepLooping = std::regex_search(mySuffix, myMatches, myRegex);
    if (myMatches.size() > 0)
    {
      std::cout << myMatches.size() << " ";
      std::cout << myMatches[0] << std::endl;
    }
    mySuffix = myMatches.suffix();
  }
  return 0;
}
/* Output:
 1 aeiou
 1 aeiou
 1 aeiou
*/
Reference: http://www.cplusplus.com/reference/regex/regex_search/

C++11: Regex

Regular Expressions (Regex) were added to C++11. Here is an example:

#include <iostream>
#include <regex>
#include <string>

int main()
{
  std::string myString("abcdefg");
  std::regex  myRegex ("cde"    );
  std::smatch myMatches;

  std::regex_search(myString, myMatches, myRegex);

  if (myMatches.size() > 0) std::cout << myMatches[0] << std::endl;
  
  return 0;
}
// Output: cde
Reference: http://www.cplusplus.com/reference/regex/regex_search/