C++ 常用自定义函数

C++ 常用自定义函数

  • [1 字符串分割](#1 字符串分割)
  • [2 秒数转换成时间](#2 秒数转换成时间)
  • [3 子字符串出现的位置](#3 子字符串出现的位置)

1 字符串分割

输入 : 一个指定分隔符的字符串

输出 : 被分割符分割的字符串

coding

cpp 复制代码
void string_split(const string &str, const char splitChar, vector<string> &vecStr)
{
    int len = str.length();

    if (len == 0)
    {
        return;
    }

    int index = str[0] == splitChar ? 1 : 0;
    int startIndex = index;

    for (; index < len; index++)
    {
        if (str[index] == splitChar)
        {
            vecStr.push_back(str.substr(startIndex, index - startIndex));
            startIndex = index + 1;
        }

        if (index == len - 1 && str[index] != splitChar)
        {
            vecStr.push_back(str.substr(startIndex, index - startIndex));
        }
    }
}

void string_split_test()
{
    string str = "|abc|def|ghi|";
    vector<string> vecStr;
    string_split(str, '|', vecStr);

    vector<string>::const_iterator iter = vecStr.begin();
    while (iter != vecStr.end())
    {
        cout << *iter++ << endl;
    }
}

2 秒数转换成时间

输入 : 距离1970年1月1日的毫秒数

输出 : 年月日

cpp 复制代码
const int32 month_days[2][13] =
    {
        {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, // 平年每月天数
        {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}  // 闰年每月天数

};

static int32 getDate(int32 days)
{
    int32 year = 1970;
    if (days > 14610)
    {
        // 2010年距离1970年 14610天
        days -= 14610;
        year = 2010;
    }
    else if (days > 10957)
    {
        // 2000年距离 1970年 10957天
        days -= 10957;
        year = 2000;
    }

    // 年份分摊天数 够一年天数 则年份 +1 天数 - 一年天数
    while (year <= 2100)
    {
        if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) // 闰年
        {
            // 剩余天数超过 366 天
            if (days > 366)
            {
                days -= 366;
                year++;
            }
            else
            {
                break;
            }
        }
        else
        {
            if (days > 365)
            {
                days -= 365;
                year++;
            }
            else
            {
                break;
            }
        }
    }
    // 因为传进来的天数少一天 因此加 1
    //days += 1;
    
    int32 mon = 1;

    int flag = 0; // 平年标记
    if (year % 4 == 0 && year % 400 != 0 || year % 400 == 0)
    {
        flag = 1; // 闰年标记
    }
    
    // 计算月份数
    while (mon <= 12)
    {
        if (days > month_days[flag][mon])
        {
            days -= month_days[flag][mon];
            ++mon;
        }
        else
        {
           break; 
        }
    }

    cout << "mon" << mon << endl;
    return year * 10000 + mon * 100 + days;
}

static int64 timeToDateTime_int64(const string &strTime)
{
    double time_zone = 8;
    int64 time = atoll(strTime.c_str());
    int64 tmp_time = time + int32(time_zone * 3600);
    // 当前时间距离1970年有多少天
    int32 days = tmp_time / 86400;
    // 转换成日期
    int32 date = getDate(days + 1);
    int32 sec = tmp_time - days * 86400;
    int32 hour = sec / 3600;
    int32 min = (sec - hour * 3600) / 60;
    sec -= (hour * 3600 + min * 60);

    return date * 1000000LL + hour * 10000LL + min * 100LL + sec;
}

3 子字符串出现的位置

输入 : 字符串1,字符串2

输出 : 字符串2在字符串1中出现的位置,如果没有出现过 则输出 -1

暴力匹配

coding

cpp 复制代码
/**
 * @name:
 * @msg: 字符串s2 在字符串s1 中出现的位置 暴力匹配
 * @param {string&} s1
 * @param {string&} s2
 * @return {*}
 */
static int32 get_index(const string &s1, const string &s2)
{
    if (&s1 == NULL || &s2 == NULL || s2.length() < 1 && s1.length() < s2.length())
    {
        return -1;
    }

    int32 L1 = s1.length();
    int32 L2 = s2.length();
    int32 i1 = 0;
    int32 i2 = 0;
    int32 retIndex = 0; // 匹配s1开始的位置

    while (i1 < L1 && i2 < L2)
    {
        if (s1[i1] == s2[i2])
        {
            ++i1;
            ++i2;
        }
        else // 以retIndex开头是匹配不出来的 要从 retIndex的下一个开始
        {
            i1 = ++retIndex;
            i2 = 0;
        }
    }

    return i2 == L2 ? retIndex : -1;
}

KMP算法

coding

cpp 复制代码
/**
 * @name:
 * @msg: kmp算法获取前缀数组
 * @param {string&} str
 * @param {int} nextArr
 * @return {*}
 */
static void getNextArr(const string &str, int nextArr[])
{
    if (&str == NULL)
    {
        return;
    }

    int32 strLen = str.length();

    if (strLen == 0)
    {
        return;
    }

    if (strLen == 1)
    {
        nextArr[0] = -1;
        return;
    }

    nextArr[0] = -1;
    nextArr[1] = 0;

    int32 index = 2;
    int preIndex = 0;

    while (index < strLen)
    {
        if (str[index - 1] == str[preIndex])
        {
            nextArr[index++] = preIndex++;
        }
        else if (preIndex > 0)
        {
            preIndex = nextArr[preIndex];
        }
        else
        {
            nextArr[index++] = -1;
        }
    }
}

/**
 * @name: 
 * @msg: 返回字符串 s2 在 s1中出现的位置 如果没有s1没有包含s2 返回 -1
 * @param {string} &s1
 * @param {string} &s2
 * @return {*}
 */
static int32 getIndexKmp(const string &s1, const string &s2)
{
    if (&s1 == NULL || &s2 == NULL || s2.length() < 1 || s1.length() < s2.length())
    {
        return -1;
    }

    int32 *nextArr = new int[s2.length()];
    getNextArr(s2, nextArr);

    int32 i1 = 0;
    int32 i2 = 0;

    while(i1 < s1.length() && i2 < s2.length())
    {
        if(s1[i1] == s2[i2])
        {
            ++i1;
            ++i2;
        }
        else if (nextArr[i2] == -1)
        {
            i1++;
        }
        else
        {
            i2 = nextArr[i2];
        }
    }
    delete[] nextArr;
    return i2 == s2.length() ? i1 - s2.length() : -1;
}
相关推荐
YRr YRr22 分钟前
在Windows上安装WSL2和Ubuntu 20.04以搭建C++开发环境的详细指南
c++·windows·ubuntu·wsl2
Farewell_me36 分钟前
C++模拟实现list:list、list类的初始化和尾插、list的迭代器的基本实现、list的完整实现、测试、整个list类等的介绍
c++·list
hong1616881 小时前
VSCode中配置C/C++环境
c语言·c++·vscode
小羊在奋斗1 小时前
【C++】探秘二叉搜索树
c++·人工智能·神经网络·机器学习
白葵新2 小时前
PCL addLine可视化K近邻
c++·人工智能·算法·计算机视觉·3d
MustardJim2 小时前
Visual Studio 引入外部静态库与动态库
c++·visual studio
挽月0012 小时前
C++单例模式
开发语言·c++·单例模式
机器视觉知识推荐、就业指导2 小时前
Qt/C++ TCP调试助手V1.1 新增图像传输与接收功能(附发布版下载链接)
c++·qt·tcp/ip
wx200411023 小时前
Codeforces Round 973 (Div. 2) - D题
数据结构·c++·算法
DieSnowK3 小时前
[项目][WebServer][CGI机制 && 设计]详细讲解
linux·开发语言·c++·http·项目·webserver·cgi机制