Get the file name out of a path

Concise code to extract everything after the slash (if there is one) without checking std::string::npos. The +1 rounds a “not found” value up to zero if there’s no slash and then substr returns the original string.

#include <iostream>

int main() {

  const std::string full_path = "one/two.jpg";
  const std::string just_the_file_name = full_path.substr(full_path.find_last_of('/') + 1);

  std::cout << "\"" << just_the_file_name << "\"\n";
}

There can only be one – call a routine only once

Using IIFE. See also std::call_once.

#include <iostream>

void say_hi(){
    static const bool said_hi = [](){ std::cout << "hi!\n"; return true; }();
}

int main() {

    say_hi();
    say_hi();
    say_hi();
}

Easy timestamp string

const auto now = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::string time = ctime(&now);

// Remove the inexplicable newline
if (!time.empty())
    time.pop_back();

Further reading