1. 字符串
#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()
{
const char *src="2005,2006,2007,101,102,103,1101,1102,----";
const char *dst="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;
void my_output(const char *arg,...)
{
va_list valist;
char buf[10240]={};
va_start(valist, arg);
int nLen = vsprintf_s(buf,sizeof(buf),arg, valist);
va_end(valist);
cout << buf << endl;
#if 0
switch (g_log_type) {
case LOG_TO_FILE:
break;
case LOG_TO_TWO:
break;
case LOG_TO_STDOUT:
default:
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.