C++ tricks/idioms

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. [Read More]