C++常用函数

C++常用函数


替换字符串中指定字符


char* replace_str(char *text, char sp_ch, char re_ch)//参数依次是 要替换的字符串指针 锁定字符 替换字符
	{
		int len = strlen(text);

		// 动态创建copy之后的字符串
		char *copy = (char *)malloc(len + 1);

		for (int i = 0; i < len; i++)
		{
			// 获取当前的char
			char ch = text[i];
			if (ch == sp_ch)
				copy[i] = re_ch;
			else
				copy[i] = ch;
		}
		// 结束
		copy[len] = 0;
		// 赋值给传进来的字符串
		strcpy(text, copy);
		// 释放动态创建的内存
		free(copy);
		return text;
	}


获得当前时间


char *get_time()
	{

		time_t now = time(0);
		char *dt = ctime(&now);
		cout << dt << endl;
		return dt;
	}

输出结果:

Sat Jan  8 20:07:41 2011

分割字符串实例

vector<string> split(const string &str, const string &pattern)
	{
		vector<string> ret;
		if (pattern.empty())
			return ret;
		size_t start = 0, index = str.find_first_of(pattern, 0);
		while (index != str.npos)
		{
			if (start != index)
				ret.push_back(str.substr(start, index - start));
			start = index + 1;
			index = str.find_first_of(pattern, start);
		}
		if (!str.substr(start).empty())
			ret.push_back(str.substr(start));
		return ret;
	}

部分内容转载自:
https://www.runoob.com/cplusplus/cpp-date-time.html
https://www.cnblogs.com/dingxiaoqiang/p/8228390.html
https://blog.csdn.net/jirryzhang/article/details/80473032

posted @ 2021-02-25 11:13  Evan_Lai  阅读(95)  评论(0编辑  收藏  举报