SampleCode CPlusPlus HighPerformance - Dieptranivsr/DroneIVSR GitHub Wiki
- Replace switch case if there are many case by using std::map and typedef function pointer
- Check whether file exists in given folder
- Compare two vectors
- Error Code (File system)
- Define Macro
- Others
#include <iostream>
#include <cmath>
auto main () -> int {
int key = 1, value = 1;
int a = 0;
while (key < 3) {
switch (key) {
case 0:
case 1:
a = value;
std::cerr << "a: " << a << std::endl;
break;
case 2:
a = std::pow(value, 10);
std::cerr << "a: " << a << std::endl;
break;
default:
// do something
break;
}
key++;
value++;
}
return 0;
}
#include <iostream>
#include <map>
#include <cstdint>
#include <cmath>
// typedef int (*FunctionGroup)(const int& value);
using FunctionGroup = std::add_pointer_t<int(const int&)>;
enum class KeyFunction : std::uint32_t {
One = 1,
Two,
Three,
Four,
Five
};
int ElementFunction1(const int& value) {
int a = value;
std::cerr << "a: " << a << std::endl;
return 0;
}
int ElementFunction2(const int& value) {
int a = value;
a = std::pow(a, 10);
std::cerr << "a: " << a << std::endl;
return 0;
}
std::map<std::uint32_t, FunctionGroup> FuntionMapping = {
{static_cast<std::uint32_t>(KeyFunction::One), ElementFunction1},
{static_cast<std::uint32_t>(KeyFunction::Two), ElementFunction2}
};
auto main () -> int {
int key = 1, value = 1;
while (key < 3) {
FuntionMapping[key](value);
key++;
value++;
}
return 0;
}
#include <iostream>
#include <string>
#include <regex>
#include <fstream>
std::string checkFileExist() {
static const std::filesystem::path fullFolderPath{"/abc/def"};
static const std::regex pattern("^(/abc/def/ghi.)([0-9]{3}[A-Z]{3})(.txt)$");
// | ^ (/abc/def/ghi.) ([0-9]{3}[A-Z]{3}) (.txt) $ |
// | Start of line /abc/def/ghi. XXX AAA .txt End of line |
// | ___ ___ |
// | X = 0,9 A = A,Z |
std::string searchFile{};
for (const auto& entry : std::filesystem::directory_iterator{fullFolderPath}) {
auto tempPath = entry.path().string();
if ((true == entry.is_regular_file()) &&
(true == std::regex_match(tempPath.begin(), tempPath.end(), pattern))){
searchFile = tempPath;
break;
}
}
}
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
constexpr auto MAX_SIZE = 1024;
auto function () -> int {
std::array<int, MAX_SIZE + 1> array_1{1, 2, 3, ...};
std::array<int, MAX_SIZE + 1> array_2{1, 2, 3, ...};
if (false == std::equal(array_1.begin(), array_1.end(), array_2.data())) {
std::cout << "The contents of both sequences differ.\n";
std::copy(array_1.begin(), array_1.end(), array_2.data());
std::cout << "Updating value for array_2.";
} else {
std::cout << "The contents of both sequences are equal.\n";
}
...
return 0;
}
mọi thứ trong linux đều là file, ngay cả các standard input, output và error cũng đều là file.
Mỗi file đều có một cái định danh gì đó để có thể cầm nắm hay thao tác được đó là các file descriptor.
Một file descriptor được diễn đạt như một số nguyên dương, các chuẩn input, output
và error cũng không phải ngoại lệ,
Còn `/dev/null' hoá ra là một device hay cũng là một file đặc biệt trong linux/unix
thường được dùng để chứa các dữ liệu rác từ các input stream khi chúng ta không muốn xử lý
hay muốn hiển thị nó, nói dễ hiểu hơn `/dev/null' giống như một Hố Đen có thể chứa tất cả
các dữ liệu được redirect tới nó.
Các toán tử `>' là các toán tử redirect từ luồng stream này sang luống stream khác.
như vậy `echo hello >/dev/null 2>&1' có ý nghĩa là
- `>/dev/null': redirect tất cả các standard output sang /dev/null . Tương đương cách viết `1>/dev/null'
- `2>&1': redirect tất cả các standard error sang standard output. Nhưng thời điểm này standard output
đang trỏ tới `/dev/null' nên standard error sẽ redirect tới `/dev/null'
#include
#include <system_error>
bool std::filesystem::remove ( const std::filesystem::path& p, std::error_code& ec ) noexcept;
bool std::filesystem::create_directory ( const std::filesystem::path& p, std::error_code& ec ) noexcept;
void std::filesystem::copy( const std::filesystem::path& from, const std::filesystem::path& to, std::filesystem::copy_options options, std::error_code& ec );
Let’s explore some of the key features provided by the library:
- Path Manipulation: The file system library introduces the std::filesystem::path class to represent file system paths. This class encapsulates the platform-specific path representation and provides an easy way to manipulate and inspect paths.
- File and Directory Operations: The file system library includes functions to perform common file and directory operations such as creating, removing, renaming, and checking for the existence of files and directories.
- Error Handling: The library provides exceptions to handle errors during file system operations. You can catch exceptions like std::filesystem::filesystem_error to gracefully handle failures.
- Portable Code: One of the main advantages of using is the portability it brings to your code. Since it abstracts platform-specific details, you can write code that works consistently across different operating systems.
#if !defined(INTERNATIONAL) && !defined(DEBUG)
// neither defined - setup Crittercism
#else
// one or both defined
#endif
#if defined(INTERNATIONAL) || !defined(DEBUG)
// one or both defined
#else
// neither defined - setup Crittercism
#endif