以下C++程式已在Windows Visual Studio 2017試驗過

 

字串轉整數(string to int)


#include <stdlib.h>
#include <string>

using namespace std;

int str2int(string str)
{
    return atoi(str.c_str());
}


 

整數轉字串(int to string)


#include <sstream>
#include <string>

using namespace std;

string int2str(int num)
{
    string s;
    stringstream ss(s);
    ss << num;

    return ss.str();
}


 

字串轉字元陣列(string to char array)


#include <string>

using namespace std;

char* str2chArray(string str)
{
    char* tmp;
    int size = str.length();
    tmp = new char[sizeof(char) * (size + 1)];
    for (int i = 0; i < str.length(); i++) tmp[i] = str[i];
    tmp[size] = '\0';

    return tmp;
}


 

字元陣列轉字串(char array to string)


#include <string>

using namespace std;

string chArray2str(char* ch)
{
    string str(ch);
    return str;
}


 

字串切割(Split)


#include <vector>
#include <string>

using namespace std;

vector<string> Split(string str, string delimiter)
{
    vector<string> data = vector<string>();

    char* pch;
    pch = strtok(str2chArray(str), str2chArray(delimiter));
    while (pch != NULL)
    {
        data.push_back(chArray2str(pch));
        pch = strtok(NULL, str2chArray(delimiter));
    }
    return data;
}


 

判斷檔案是否存在(is file exist)


#include <fstream>

using namespace std;

bool isFileExist(string filename)
{
    FILE* fin = fopen(str2chArray(filename), "r");
    if (!fin) return false;
    else return true;
}


 

取得資料夾下的所有檔案(包含子資料夾內的檔案)

需使用dirent.h


#include "dirent.h"
#include <vector>
#include <string>

#include <stdlib.h>

#define Platform_Windows
//#define Platform_Linux

 

using namespace std;

void GetFiles(char* path, vector<string>& files)
{
#ifdef Platform_Windows
    char glue = '\\';//Windows的分隔符號
#elif Platform_Linux
    //char glue='/';//Linux 的分隔符號
#else
    cout << "***[ERROR]Platform error\n"
#endif

    DIR* dp = opendir(path);
    if (!dp)
    {
        // 不是目錄
        files.push_back(chArray2str(path));
        return;
    }

    struct dirent* filename;
    while ((filename = readdir(dp)))
    {
        //跳過當前及母目錄
        if (!strcmp(filename->d_name, "..") || !strcmp(filename->d_name, ".")) continue;

        //計算新的路徑字串所需的長度
        int pathLength = strlen(path) + strlen(filename->d_name) + 2;
        
        //產生新的陣列空間
        char* pathStr = new char[sizeof(char) * pathLength];
        
        //複製當前目錄路徑至新的陣列空間
        strcpy(pathStr, path);

        //檢查目錄分隔符號
        int i = strlen(pathStr);
        if (pathStr[i - 1] != glue)
        {
            pathStr[i] = glue;
            pathStr[i + 1] = '\0';
        }
        
        //串接次目錄名稱或檔案名稱至新的陣列空間
        strcat(pathStr, filename->d_name);

        //遞迴呼叫目錄掃瞄
        GetFiles(pathStr, files);
    }
}


 

從txt檔一次讀取一行資料(getline)


#include <string>
#include <fstream>
#include <vector>

using namespace std;

vector<string> GetData(char* path)
{
    vector<string> dataVec;
    string data;
    fstream file;
    file.open(path);
    while (getline(file, data))
    {
        dataVec.push_back(data);
    }
    return dataVec;
}


 

找出vector中的最大值和最小值


#include <vector>
using namespace std;

int main()
{
    vector<int> data = {1, 2, 5, -1, 0, 4};
    int max = *std::max_element(std::begin(data), std::end(data));
    int min = *std::min_element(std::begin(data), std::end(data));
}


 

arrow
arrow
    全站熱搜

    Yang 發表在 痞客邦 留言(0) 人氣()