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;
}
相关推荐
·薯条大王3 小时前
经济实惠玩云服务器|一台云服务器多人共用,子账号配置教程
java·linux·运维·服务器·汇编·c++·python
hansang_IR7 小时前
【题解】LC:倍增 / 区间并查集(Range Parallel Unionfind)
c++·算法·并查集
东东最爱敲键盘7 小时前
day7.c++继承
c++
沫璃染墨8 小时前
《从零入门Linux系统篇(十五):系统工具篇·六——GDB调试器详解:从程序执行控制到高级调试技巧》
linux·运维·服务器·c语言·c++
吃着火锅x唱着歌8 小时前
Effective C++ 学习笔记 条款40 明智而审慎地使用多重继承
c++·笔记·学习
醉城夜风~9 小时前
(超详细)C++ STL list容器详解:从使用方法到双向链表底层原理全面解析
c++·windows
烟花落o9 小时前
C++ 从 C 到进阶 ②:引用、inline 、nullptr
c++·引用·inline·nullptr·c++入门
liulilittle10 小时前
MOE路由:路由(logits: top-k/8)
c++·人工智能·算法·机器学习·llm
旖旎夜光10 小时前
LeetCode 11:盛最多水的容器(双指针问题) —— 题解
数据结构·c++·算法·leetcode·双指针
我不会插花弄玉12 小时前
15.map,set下:AVL树,红黑树和map,set的封装【由浅入深-C++】
c++·stl