几个实用的C++函数接口

简介: 几个实用的C++函数接口
#include <codecvt>
#include <boost/filesystem.hpp>
#include <boost/filesystem/path.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
std::string time_to_string(const boost::posix_time::ptime& time)
{
  boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
  facet->format("%Y_%m_%d %H_%M_%S");
  std::stringstream stream;
  stream.imbue(std::locale(std::locale::classic(), facet));
  stream << time;
  return stream.str();
}
boost::filesystem::path executable_path()
{
  TCHAR path[MAX_PATH] = { 0 };
  GetModuleFileName(GetModuleHandle(NULL), path, MAX_PATH);
  return boost::filesystem::path(path).remove_filename();
}
std::string convertToUtf8(const std::wstring& str) {
  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
  return converter.to_bytes(str);
}
std::wstring convertToUtf16(const std::string& str) {
  std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>, wchar_t> converter;
  return converter.from_bytes(str);
}
bool isNum(std::string strInfo)
{
  try
  {
    unsigned long data = boost::lexical_cast<UINT64>(strInfo);
  }
  catch (boost::exception& e)
  {
    return false;
  }
  return true;
}
std::string GetUUID()
{
  boost::uuids::uuid uuid = boost::uuids::random_generator()();
  return boost::uuids::to_string(uuid);
}
UINT64 GetCurTime()
{
  return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now().time_since_epoch()).count();
}
std::string GetLocalFormatTime()
{
  char buf[512] = { 0 };
  using namespace std::chrono;
  const time_t& curTime = system_clock::to_time_t(system_clock::now());
  tm* localTime = localtime(&curTime);
  sprintf_s(buf, "%d-%d-%d %d:%d:%d", localTime->tm_year + 1900, localTime->tm_mon + 1,
    localTime->tm_mday, localTime->tm_hour, localTime->tm_min, localTime->tm_sec);
  return buf;
}
std::wstring Utf8ToUnicode(const std::string& utf8)
{
  LPCCH ptr = utf8.c_str();
  int size = MultiByteToWideChar(CP_UTF8, 0, ptr, -1, NULL, NULL);
  std::wstring wstrRet(size, 0);
  int len = MultiByteToWideChar(CP_UTF8, 0, ptr, -1, (LPWSTR)wstrRet.c_str(), size);
  return wstrRet;
}
std::string Utf8ToAnsi(const std::string& utf8)
{
  std::wstring wstrTemp = Utf8ToUnicode(utf8);
  LPCWCH ptr = wstrTemp.c_str();
  int size = WideCharToMultiByte(CP_ACP, 0, ptr, -1, NULL, 0, NULL, NULL);
  std::string strRet(size-1, 0);
  int len = WideCharToMultiByte(CP_ACP, 0, ptr, -1, (LPSTR)strRet.c_str(), size, NULL, NULL);
  return strRet;
}
void log(const std::string& str) 
{
  std::ofstream file((executable_path() / "error.log").c_str(), std::ios::app);
  file << time_to_string(boost::posix_time::second_clock::universal_time()) << ' ' << str << std::endl;
}
std::string ws2s(const std::wstring& str)
{
  int tm1 = GetCurTime();
  char*     pElementText;
  int    iTextLen;
  // wide char to multi char
  iTextLen = WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, NULL, 0, NULL, NULL);
  pElementText = new char[iTextLen + 1];
  memset((void*)pElementText, 0, sizeof(char)* (iTextLen + 1));
  ::WideCharToMultiByte(CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL);
  std::string strText;
  strText = pElementText;
  delete[] pElementText;
  int tm2 = GetCurTime() - tm1;
  return strText;
}
相关文章
|
2天前
|
存储 C++
c/c++宏定义(函数)
c/c++宏定义(函数)
|
4天前
|
编译器 C++
【C++进阶】引用 & 函数提高
【C++进阶】引用 & 函数提高
|
8天前
|
C++
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
C++从入门到精通:2.1.2函数和类——深入学习面向对象的编程基础
|
8天前
|
存储 C++
C++从入门到精通:2.1.1函数和类
C++从入门到精通:2.1.1函数和类
|
15天前
|
C语言 C++ 容器
探索c++:string常用接口 迷雾
探索c++:string常用接口 迷雾
39 0
|
15天前
|
缓存 编译器 API
NumPy与其他语言(如C/C++)的接口实践
【4月更文挑战第17天】本文介绍了NumPy与C/C++的接口实践,包括Python与C/C++交互基础、NumPy的C API和Cython的使用。通过案例展示了如何将C++函数与NumPy数组结合,强调了内存管理、类型匹配、错误处理和性能优化的最佳实践。掌握这些技能对于跨语言交互和集成至关重要。
|
16天前
|
机器学习/深度学习 定位技术 C++
c++中常用库函数
c++中常用库函数
38 0
|
18天前
|
算法 搜索推荐 C++
浅谈sort函数底层(一道c++面试的天坑题)
浅谈sort函数底层(一道c++面试的天坑题)
|
20天前
|
编译器 C++
C++ 解引用与函数基础:内存地址、调用方法及声明
C++ 中的解引用允许通过指针访问变量值。使用 `*` 运算符可解引用指针并修改原始变量。注意确保指针有效且不为空,以防止程序崩溃。函数是封装代码的单元,用于执行特定任务。理解函数的声明、定义、参数和返回值是关键。函数重载允许同一名称但不同参数列表的函数存在。关注公众号 `Let us Coding` 获取更多内容。
136 1
|
21天前
|
编译器 C语言 C++
【C++初阶(九)】C++模版(初阶)----函数模版与类模版
【C++初阶(九)】C++模版(初阶)----函数模版与类模版
20 0