c++ 常见函数

1. 字符串

//例1: src是否包含dst字符串
#include <iostream>
using namespace std;

bool contains(const char* src, const char*dst) {
    const char *p = src;
    while (*p != '\0' && (0 != strncmp(dst, p, strlen(dst) ) ) )
        p++;

    return (*p == '\0') ? false : true;
}

void test_contains()
{	// 场景测试: src 可以看做数据库某项功能id 的集合
    const char *src="2005,2006,2007,101,102,103,1101,1102,----";
    const char *dst="2020";  // 需要查找是否具备2020 这项功能.
    cout << "src:" << src << endl;
    cout << "dst:" << dst << endl;
    //
    bool ret = contains(src,dst);
    cout<<"[" << dst << "] ";
    if(false==ret)
        cout<<"不";
    cout << "在 [" << src << "]" << endl;
}
int main()
{
    test_contains();
    
    return 0;
}

// ----- 输出------------
src:2005,2006,2007,101,102,103,1101,1102,----
dst:2020
[2020] 不在 [2005,2006,2007,101,102,103,1101,1102,----]

2. 可变参数

#include <iostream>
#include <stdarg.h>

#define LOG_TO_STDOUT  0
#define LOG_TO_FILE    1
#define LOG_TO_TWO     2

unsigned int g_log_type=1;

// 可变参数 arg...
void my_output(const char *arg,...)
{
    va_list   valist;
    char  buf[10240]={};

    va_start(valist, arg);
    //组包到 buf
    int  nLen = vsprintf_s(buf,sizeof(buf),arg, valist);
    va_end(valist);
    cout << buf << endl;

    #if 0  // Todo: 增加log输出地
    switch (g_log_type) {
    case LOG_TO_FILE:
        // write_logfile(buf,nLen);
        break;
    case LOG_TO_TWO:
        // write_logfile(buf,nLen);
        // mydebug(buf);
        break;
    case LOG_TO_STDOUT:
    default:
        // mydebug(buf);
        break;
    }
    #endif
}

void test_output()
{
    my_output("[%s] This is log test. [%u]'s file[%s] process finnished.","2020-05-20 11:00:23 381568", 310081001,"abc.txt");
}

int main()
{
    test_output();
    
    return 0;
}

// ----- 输出------------
[2020-05-20 11:00:23 381568] This is log test. [310081001]'s file[abc.txt] process finnished.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值