c++片段

本文展示了C++中创建线程的方法,使用互斥锁实现线程安全,以及字符串分割、查找、比较的操作。同时,介绍了如何获取文件夹内文件和目录的路径,包括GBK和UTF8编码转换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

c++ 创建线程,并运行

#include <iostream>
#include <windows.h>
#include <thread> 

HANDLE Thread1 = NULL;
int Thread1_bool = 0;
DWORD WINAPI Process_thread(LPVOID lpParameters)
{
	int i = 0;
	while (Thread1_bool)
	{
		i++;
		if (i == 100)
			Thread1_bool = false;
		std::cout << "子线程" << std::endl;
		std::this_thread::sleep_for(std::chrono::milliseconds(20));
	}
	return 0;
}

int main()
{
	//创建线程
	Thread1 = CreateThread(NULL, 0, Process_thread, NULL, 0, NULL);
	Thread1_bool = true; 
	while (Thread1_bool)
	{
		std::cout << "主线程" << std::endl;
		Sleep(20);
	}
	std::cout << "结束" << std::endl;
   
	//等待函数,等待子线程 1秒
	DWORD dwRet = WaitForSingleObject(Thread1, 1000);
	//关闭线程
	CloseHandle(Thread1);
	return 1;
}

C++ 用函数 加锁解锁

void proc(int& aa,int& bb,int cc)
{
	{
		//通过使用{}来调整作用域范围,可使得在合适的地方被解锁
		lock_guard<mutex> g2(mutex_return);
		cout << "作用域内 的内容" << endl;
		aa += 2;
		bb += cc;
	}
	cout << "作用域外 的内容" << endl;
}

c++ 字符串分割

//字符串分割
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;
} 
std::string str = "abcd_efg";
std::string sp = "_";
vector<string> whd = split(str , sp ); 

c++ 判断string 是否 包含 string

//判断第一个参数 是否 包含 第二个参数  1:包含   0:不包含
bool find_str(std::string input_str, std::string input_find_str)
{
	const char* s = input_str.c_str();
	int count = input_find_str.size();
	const char* s1 = input_find_str.c_str();

	for (int i = 0; i < input_str.size(); i++)
	{
		if (s[i] == s1[0])
		{
			int del = 0;
			for (int j = 1; j < count; j++)
			{
				if (s[i + j] == s1[j]) { del++; }
				else { 	break; }
			}
			if (del == count - 1) { return 1; }
		}
	}
	return 0;
}

c++ 判断两个字符串相等

//判断两个字符串相等
bool is_equal(std::string input_str1, std::string input_str2)
{
	const char* s1 = input_str1.c_str();
	const char* s2 = input_str2.c_str();
	int count1 = input_str1.size();
	int count2 = input_str2.size();
	int n = 0;
	if (count1 == count2)
	{
		for (int i = 0; i < count1; i++)
		{
			if (s1[i] != s2[i]) { return 0; }
			n = 1;
		}
	}
	return n;
}

c++ //获取文件夹内所有 文件夹 路径

//获取文件夹内所有 文件夹 路径
void getNowDirs(std::string path, std::vector<std::string>& files, std::string format)
{
	intptr_t hFile = 0;			//文件句柄
	struct _finddata_t fileinfo; //文件信息
	string p;
	string aaa = p.assign(path).append("\\*" + format).c_str();
	if ((hFile = _findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1) //文件存在
	{
		do
		{
			if ((fileinfo.attrib & _A_SUBDIR)) //判断是否为文件夹
			{
				int a11 = strcmp(fileinfo.name, ".");
				int a22 = strcmp(fileinfo.name, "..");
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0) //文件夹名中不含"."和".."
				{
					files.push_back(p.assign(path).append("\\").append(fileinfo.name)); //保存文件夹名
				}
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

c++ 获取文件夹内所有 文件夹、文件 路径

//获取文件夹内所有 文件夹、文件 路径
void getAllFiles(std::string path, std::vector<std::string>& files, std::string format)
{
	intptr_t hFile = 0;
	struct _finddata_t fileinfo;
	std::string p;
	if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)//文件夹存在
	{
		do
		{
			if ((fileinfo.attrib & _A_SUBDIR))  //判断是否为文件夹
			{
				if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)//文件夹名中不含"."和".."
				{
					getAllFiles(p.assign(path).append("\\").append(fileinfo.name), files, format);
				}
			}
			else
			{
				if ((_findfirst(p.assign(path).append("\\*" + format).c_str(), &fileinfo)) != -1)//文件存在
				{
					files.push_back(p.assign(path).append("\\").append(fileinfo.name));
				}
			}
		} while (_findnext(hFile, &fileinfo) == 0);
		_findclose(hFile);
	}
}

c++ utf8转国标

//utf8转国标
string UTF8ToGB(const char* str)
{
	string result;
	WCHAR* strSrc;
	LPSTR szRes;

	//获得临时变量的大小
	int i = MultiByteToWideChar(CP_UTF8, 0, str, -1, NULL, 0);
	strSrc = new WCHAR[i + 1];
	MultiByteToWideChar(CP_UTF8, 0, str, -1, strSrc, i);

	//获得临时变量的大小
	i = WideCharToMultiByte(CP_ACP, 0, strSrc, -1, NULL, 0, NULL, NULL);
	szRes = new CHAR[i + 1];
	WideCharToMultiByte(CP_ACP, 0, strSrc, -1, szRes, i, NULL, NULL);

	result = szRes;
	delete[]strSrc;
	delete[]szRes;

	return result;
}

c++ 国标转utf8

//国标转utf8
char* GBToUTF8(const char* gb2312)
{
	int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
	wchar_t* wstr = new wchar_t[len + 1];
	memset(wstr, 0, len + 1);
	MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
	len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
	char* str = new char[len + 1];
	memset(str, 0, len + 1);
	WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
	if (wstr) delete[] wstr;
	return str;
}

c++ 计时 clock_t

    double clock_dur;
    clock_t clock_start, clock_end;
    
    clock_start = clock();
    int ret = find_line(img, rotrect, fparam);
    std::cout << ret << std::endl;
    
    clock_end = clock();
    
    std::cout << " find time	" << (double)(clock_end - clock_start) / CLOCKS_PER_SEC * 1000 << "ms	" << std::endl;

c++ 判断文件是否存在

#include "io.h"
	bool tf = _access(fname.c_str(), 0) != -1 ? true : false;
	cout << tf << endl;
	struct stat buffer;
	tf = stat(fname.c_str(), &buffer) == 0 ? true : false;
	cout << tf << endl;

c++ 获取文件夹里所有文件 路径

	void getFiles(string path, vector<string>& files)
	{
		long long  hFile = 0;
		struct _finddata_t fileinfo;
		string p;
		if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
		{
			do
			{
				if ((fileinfo.attrib & _A_SUBDIR))
				{
					if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
						getFiles(p.assign(path).append("\\").append(fileinfo.name), files);
				}
				else
				{
					files.push_back(p.assign(path).append("\\").append(fileinfo.name));
				}
			} while (_findnext(hFile, &fileinfo) == 0);
	
			_findclose(hFile);
		}
	}

c++ 判断字符串是 国标、utf8

bool is_str_gbk(const char* str)
{
	unsigned int nBytes = 0;//GBK可用1-2个字节编码,中文两个 ,英文一个
	unsigned char chr = *str;
	bool bAllAscii = true; //如果全部都是ASCII,
	for (unsigned int i = 0; str[i] != '\0'; ++i) {
		chr = *(str + i);
		if ((chr & 0x80) != 0 && nBytes == 0) {// 判断是否ASCII编码,如果不是,说明有可能是GBK
			bAllAscii = false;
		}
		if (nBytes == 0) {
			if (chr >= 0x80) {
				if (chr >= 0x81 && chr <= 0xFE) {
					nBytes = +2;
				}
				else {
					return false;
				}
				nBytes--;
			}
		}
		else {
			if (chr < 0x40 || chr>0xFE) {
				return false;
			}
			nBytes--;
		}//else end
	}
	if (nBytes != 0) {   //违返规则
		return false;
	}
	if (bAllAscii) { //如果全部都是ASCII, 也是GBK
		return true;
	}
	return true;
}
bool is_str_utf8(const char* str)
{
	unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节
	unsigned char chr = *str;
	bool bAllAscii = true;
	for (unsigned int i = 0; str[i] != '\0'; ++i) {
		chr = *(str + i);
		//判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx
		if (nBytes == 0 && (chr & 0x80) != 0) {
			bAllAscii = false;
		}
		if (nBytes == 0) {
			//如果不是ASCII码,应该是多字节符,计算字节数
			if (chr >= 0x80) {
				if (chr >= 0xFC && chr <= 0xFD) {
					nBytes = 6;
				}
				else if (chr >= 0xF8) {
					nBytes = 5;
				}
				else if (chr >= 0xF0) {
					nBytes = 4;
				}
				else if (chr >= 0xE0) {
					nBytes = 3;
				}
				else if (chr >= 0xC0) {
					nBytes = 2;
				}
				else {
					return false;
				}
				nBytes--;
			}
		}
		else {
			//多字节符的非首字节,应为 10xxxxxx
			if ((chr & 0xC0) != 0x80) {
				return false;
			}
			//减到为零为止
			nBytes--;
		}
	}
	//违返UTF8编码规则
	if (nBytes != 0) {
		return false;
	}
	if (bAllAscii) { //如果全部都是ASCII, 也是UTF8
		return true;
	}
	return true;
}

c++


c++


c++


c++


c++


c++


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值