C++常用脚本合集

① 文件夹中文件分类

https://blog.csdn.net/Cream_Cicilian/article/details/114375866?spm=1001.2014.3001.5501

② 找文件夹中,最长的名字有多长

https://blog.csdn.net/Cream_Cicilian/article/details/108962302?spm=1001.2014.3001.5501

③读取某个文件夹下面的子文件夹及其所有文件

https://blog.csdn.net/Cream_Cicilian/article/details/108659285

④大量文件转移

https://blog.csdn.net/Cream_Cicilian/article/details/124524084?spm=1001.2014.3001.5501

⑤判断文件夹是否存在,不存在则创建

https://www.manongdao.com/article-586585.html
单级文件夹:

#include <io.h>
#include <direct.h>
#include <string>

int main()
{
	std::string prefix = "G:/test/";
	if (_access(prefix.c_str(), 0) == -1)	//如果文件夹不存在
		_mkdir(prefix.c_str());				//则创建
}

多级文件夹,最后一个如果是文件夹的话,需要加上 ‘\’ 或者 ‘/’:

#include <io.h>
#include <direct.h>
#include <string>

int createDirectory(std::string path)
{
	int len = path.length();
	char tmpDirPath[256] = { 0 };
	for (int i = 0; i < len; i++)
	{
		tmpDirPath[i] = path[i];
		if (tmpDirPath[i] == '\\' || tmpDirPath[i] == '/')
		{
			if (_access(tmpDirPath, 0) == -1)
			{
				int ret = _mkdir(tmpDirPath);
				if (ret == -1) return ret;
			}
		}
	}
	return 0;
}

⑥处理txt文件常用功能方法

https://blog.csdn.net/Cream_Cicilian/article/details/122362436?spm=1001.2014.3001.5501

1.当字符串中只有一对双引号,取引号里面的内容

2.string替换所有指定字符串

3.数据类型转换

⑦两个文件夹互相找对方的同名文件

https://blog.csdn.net/Cream_Cicilian/article/details/122370824?spm=1001.2014.3001.5501

⑧txt读取中文

https://blog.csdn.net/Cream_Cicilian/article/details/115347354?spm=1001.2014.3001.5501

⑨读取文件夹下文件的文件名

#include <iostream>
#include<filesystem>
using namespace std;
using namespace std::experimental::filesystem;

int main() {
	path str("D:\\WWY\\1");
	if (!exists(str))		//必须先检测目录是否存在才能使用文件入口.
		return 1;
	directory_entry entry(str);		//文件入口
	if (entry.status().type() == file_type::directory)	//这里用了C++11的强枚举类型
		cout << "该路径是一个目录" << endl;
	directory_iterator list(str);	        //文件入口容器
	for (auto& it : list)
		cout << it.path().filename() << endl;	//通过文件入口(it)获取path对象,再得到path对象的文件名,将之输出
	system("pause");
	return 0;
}

⑩ vector乱序

// random_shuffle_demo.cpp : 定义控制台应用程序的入口点。
//



#include <string>  
#include <vector> 
#include <algorithm> 
#include <iostream>  

using namespace std;

int main()
{
	vector<int> vs;
	vs.push_back(1);
	vs.push_back(2);
	vs.push_back(3);
	vs.push_back(4);
	vs.push_back(5);
	vs.push_back(6);
	vs.push_back(7);

	random_shuffle(vs.begin(), vs.end()); /* 打乱顺序 */

	for (int i = 0; i < 7; i++)
		cout << vs[i] << " "; /* 显示打乱顺序后的元素 */

}

①① 字典对应

// random_shuffle_demo.cpp : 定义控制台应用程序的入口点。
//



#include <string>  
#include <vector> 
#include <algorithm> 
#include <iostream>  
#include<map>
using namespace std;

int main()
{
	std::map<int, string> mymap;

	// 插入单个值
	mymap.insert(std::pair<int, string>(1, "100"));
	mymap.insert(std::pair<int, string>(2, "200"));
	mymap.insert(std::pair<int, string>(3, "300"));
	mymap.insert(std::pair<int, string>(4, "400"));
	mymap.insert(std::pair<int, string>(5, "500"));
	mymap.insert(std::pair<int, string>(6, "600"));
	mymap.insert(std::pair<int, string>(7, "700"));
	mymap.insert(std::pair<int, string>(8, "00"));

	vector<int> vs;
	vs.push_back(1);
	vs.push_back(2);
	vs.push_back(3);
	vs.push_back(4);
	vs.push_back(5);
	vs.push_back(6);
	vs.push_back(7);

	random_shuffle(vs.begin(), vs.end()); /* 打乱顺序 */

	for (int i = 0; i < 7; i++)
		cout << vs[i] << " "; /* 显示打乱顺序后的元素 */

	//cout << mymap.at(3) << endl;
	vector<string> vs_rusalt;
	for (int k = 0; k < 7; k++)
	{
		int tmp =0;
		
			tmp = vs[k];		
		string temp = mymap[tmp];
		cout << temp << " ";
	}

	/*for (int i = 0; i < 7; i++)
		cout << vs_rusalt[i] << " "; /* 显示打乱顺序后的元素 */

}

①② 分割字符串

// random_shuffle_demo.cpp : 定义控制台应用程序的入口点。
//



#include <string>  
#include <vector> 
#include <algorithm> 
#include <iostream>  
#include<map>
using namespace std;

int main()
{
	std::map<int, string> mymap;

	// 插入单个值
	mymap.insert(std::pair<int, string>(1, "100"));
	mymap.insert(std::pair<int, string>(2, "200"));
	mymap.insert(std::pair<int, string>(3, "300"));
	mymap.insert(std::pair<int, string>(4, "400"));
	mymap.insert(std::pair<int, string>(5, "500"));
	mymap.insert(std::pair<int, string>(6, "600"));
	mymap.insert(std::pair<int, string>(7, "700"));
	mymap.insert(std::pair<int, string>(8, "00"));

	vector<int> vs;
	vs.push_back(1);
	vs.push_back(2);
	vs.push_back(3);
	vs.push_back(4);
	vs.push_back(5);
	vs.push_back(6);
	vs.push_back(7);

	random_shuffle(vs.begin(), vs.end()); /* 打乱顺序 */

	for (int i = 0; i < 7; i++)
		cout << vs[i] << " "; /* 显示打乱顺序后的元素 */

	//cout << mymap.at(3) << endl;
	vector<string> vs_rusalt;
	for (int k = 0; k < 7; k++)
	{
		int tmp =0;
		
			tmp = vs[k];		
		string temp = mymap[tmp];
		cout << temp << " ";
	}

	/*for (int i = 0; i < 7; i++)
		cout << vs_rusalt[i] << " "; /* 显示打乱顺序后的元素 */

}

①③ 删除string最后一个字符的几种方法

https://blog.csdn.net/u011857683/article/details/81058622

①④ 找字符串关键词

bool FindKeyWord(string s, string keyWord) {
	string::size_type idx;
	idx = s.find(keyWord);//在a中查找b.
	if (idx != string::npos) {
		return 1;
	}
	else {
		return 0;
	}
}

①⑤ 找字符串中的数字


/*
提取字符串中的数字
*/
string FindNumber(string s) {
	int i = 0, j = 0;
	int len_s = s.size();
	vector <int> a;
	while (i < len_s)
	{
		if (s[i] >= '0'&& s[i] <= '9')
		{
			j = i;
			int len = 0;
			while (s[i] >= '0'&& s[i] <= '9')
			{
				i++;
				len++;
			}
			string s0 = s.substr(j, len);//获取子串
			int num = 0;//数字字符串转换为整型数字
			stringstream s1(s0);
			s1 >> num;
			a.push_back(num);
		}
		else
		{
			i++;
		}
	}
	//for (int w = 0; w < a.size(); w++) {
	//	std::cout << a[w] << endl;
	//}
	stringstream ss;
	string str;
	copy(a.begin(), a.end(), ostream_iterator<int>(ss, ""));
	str = ss.str();
	return str;
}

  • 2
    点赞
  • 40
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值