Skip to content

mathfunction/DIYCXX

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

148 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

DIYCXX 專案概述

=================================================

開發階段中~函式名稱,結構隨時有可能改變,僅供參考 !!

=================================================

  • 作者: Plus & Minus

  • 更新日期: 2020.04.11

  • 這是嘗試讓 C++ 一些易用輔助開發的函式庫

  • headfile-only 都在 include/*

    #include "cxxuseful.hpp"
    using namespace cxxuseful;
    /* your codes */
  • 詳細用法可見 example/hello.cpp 實作或是各 .hpp 置頂註解

測試環境 (作業系統 / 終端機 / 編譯器)

函式庫需求(預先安裝)

  • 把以下的函式庫下載到 thirdparty/ ,完成後類似下圖

  • 第三方函式庫資訊:

    函式庫名 資料夾/檔名 用途
    boost boost_X_X_X/* 可視為 STL 擴展函式
    json json.hpp 處理 json 結構

編譯指令

直接透過 g++

g++ -std=c++XX -O3 -o {your.cpp} -I"{yourpath}/include/" -I"{yourpath}/thirdparty/{project_name}"

或是透過 python 腳本編譯+試跑,c++XX 代表 {c++11,c++14,c++17,c++2a}

python script.py --compile c++XX {your.cpp}
python script.py --run {your.cpp}

若要修改編譯器指令,可參考 script.py 的 cmds 如下圖:

include/*.hpp 檔案說明

hpp 檔名 實體物件名 說明
cxxuseful.hpp - 關於此專案所有 header 引用(包含 STL,Boost,DIY)
boost_includes.hpp - 關於此專案會需要用到的 boost 函式庫
timer.hpp - 程式區塊計時相關
printfunc.hpp print 列印結構相關
cmdcolor.hpp printXXX 關於在終端機上顯示彩色字串 XXX = { Red , Yellow , Blue , Green ...}
makedictfunc.hpp mkdict 結構轉 unordered_map 相關
jsonfunc.hpp jsonfc json 處理相關
randomfunc.hpp randfc 亂數產生模擬相關
strhandler.hpp shlr 字串輔助相關
utf8str.hpp utf8 , u8string utf8 中文字串處理相關
filepath.hpp - 檔案路徑分析
otherfunc.hpp otherfc 已實作但無法分類區

範例: hello.cpp

  • 終端機顯示彩色 string

    printRed("red",true);
    printGreen("green",true);
    printYellow("yellow",true);
    printBlueGreen("bluegreen",true);
    printBlue("blue",true);
    printPurple("purple",true);
  • 混雜型別轉字串 (尚未支援中文)

    string _str = shlr({1,0.123,"456"},/*分割元*/"_",/*head*/"X");
    cout << _str << endl;
  • 宣告 / 列印 print 結構

    vector<int> _vec({1,2,3,4,5,6,7});
    	unordered_map<string,int> _dict({
    		{"key1",45},
    		{"key2",46}
    	});
    	print(_vec);
    	print(_dict);
  • 陣列轉字典(位置=key)

    basic_string<int> _bas({2,3,4,5,6,7,8});
    auto _dictBastr = type<unordered_map<int,int> >(mkdict(_bas));
    print(_dictBastr);
    Windows cmd MAC TERMINAL
  • 程式區塊計時

    { // 計時開始
    		Timer timer("test");
    		for(int i=0;i<1000;i++){
    			cerr << i << "|";
    		}
    		cerr << endl;
    }//計時結束
    Windows CMD
    MAC TERMINAL
  • utf8 中文處理/顯示

    // 註: IDE/文字編輯器 , 觀看 hello.cpp 需要為 utf8編碼 
    string _cxxstr = "C++中文處理真的很麻煩,超級難⁉⁉最後是用 UTF-8 切中字串 u8len() 變成 vector<string> 😅 ";
    utf8 _cxxstr2 = s2u8(_cxxstr); // utf8 本質上為 vector<string>
    // 把字串裡的"中"字改成"英"
    for(int i=0;i<_cxxstr2.size();i++){
    		if(_cxxstr2[i] == ""){
    			_cxxstr2[i] = "";
    		}//endif
    }//endfor
    print_utf8(_cxxstr2);
    Windows cmd 預設 Big5(950) Mac terminal

    ===========================================

    Trick 筆記 : windows cmd 保險輸出中文(UTF8)方法

    ===========================================

    // 步驟1. 在 windows cmd 字串前面加入 "空格退格" , 實作在 utf8str.hpp
    string wintrick(const string& str){
    	return " \b"+str;
    }
    // 步驟2. 調整模式至 utf8 (65001),cout 後,再調回預設(950) , 實作在 printfunc.hpp
    void print(const &str){
        #if defined _WIN32 || defined _WIN64
            SetConsoleOutputCP(65001); 
        #endif
        cout << wintrick(str) << endl;
        #if defined _WIN32 || defined _WIN64
            SetConsoleOutputCP(950); 
        #endif
    }
  • u8string 實用物件,中文處理/顯示 !!

    // uft8str.hpp
    typedef vector<string> utf8;
    class u8string{
       utf8 v;   // 切割過後的 vector<string>  即 v = s2u8(str) 
       string str; // 原始字串  
    };
      // hello.cpp  
      typedef u8string u8;
    
        u8("_______這是 u8string_測試_________").print();
        u8 s1("中國");
        u8 s2("武漢");
    
        // concat 測試 !!
        (s1+s2).print();
        (s2+"肺炎").print();
        (s1+s2+"肺炎").print();
        (s1+""+s2).print();
    
        s1 = "1二3四5六";
        // 子字串測試 (-1 代表 s1.size()-1)
        s1(1,-1).print();
        s1(2,4).print();
        s1(3).print();
    
        // join 字串
        s1 = u8(",").join({u8("我正要去"),u8("空中大學"),u8("!!")});
        s1.print();
    
        // split 字串
        vector<u8> v;
        s1("!看我看看!知道什麼!到底要知道什麼!!知道什麼不知道!知道最好!!看!");
        s1.print();
        v = s1.ngram(3);
        print(v);
        v = s1.split("!");
        print(v);
        v = s1.split(u8("知道"));
        print(v);
        s1 = u8("測試").join(v);
        s1.print();
    
windows cmd Mac Terminal
  • 讀 json 檔 (./example/politics.json)

    json j;
    j = jsonfc.read("politics.json"); // 註:此路徑為寫死 !!
    // cout << j << endl;  // 有的 windows cmd 會有 bug !!
    unordered_map<string,string> cov = jsonfc.dict<string,string>(j["衛生組織"]); 
    vector<string> who = jsonfc.list<string>(j["肺炎"]);
    vector<int> sicks = jsonfc.list<int>(j["武漢起始數據"]["累積確診人數"]);
    print(cov);
    print(who);
    print(sicks);
    Windows cmd Mac terminal
  • 檔案路徑 FilePath()

    // filepath.hpp
    class FilePath{
        public:
        	FilePath(const string &_str,string delimiter="/")
    		vector<u8string> tokens();
    		string dirname();
        	string filename();
    		string type();
    };
    
    // hello.cpp
    FilePath f("/mnt/c/Users/XXX/Desktop/怕.jpg");
    f.print();
    print(f.tokens());
    FilePath("C:\\Program Files (x86)\\微軟\\HelloWorld.cpp","\\").print();	
    #if defined _WIN32 || defined _WIN64
    	// __FILE__ = C macro 取得此檔案的絕對路徑
    	FilePath(__FILE__,"\\").print();
    #else
    	FilePath(__FILE__).print();
    #endif
    
    

About

some common useful C++ routines

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published