码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • C++常用脚本合集


    C++常用脚本合集

      • ① 文件夹中文件分类
      • ② 找文件夹中,最长的名字有多长
      • ③读取某个文件夹下面的子文件夹及其所有文件
      • ④大量文件转移
      • ⑤判断文件夹是否存在,不存在则创建
      • ⑥处理txt文件常用功能方法
        • 1.当字符串中只有一对双引号,取引号里面的内容
        • 2.string替换所有指定字符串
        • 3.数据类型转换
      • ⑦两个文件夹互相找对方的同名文件
      • ⑧txt读取中文
      • ⑨读取文件夹下文件的文件名
      • ⑩ vector乱序
      • ①① 字典对应
      • ①② 分割字符串
      • ①③ 删除string最后一个字符的几种方法
      • ①④ 找字符串关键词
      • ①⑤ 找字符串中的数字

    ① 文件夹中文件分类

    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 
    #include 
    #include 
    
    int main()
    {
    	std::string prefix = "G:/test/";
    	if (_access(prefix.c_str(), 0) == -1)	//如果文件夹不存在
    		_mkdir(prefix.c_str());				//则创建
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

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

    #include 
    #include 
    #include 
    
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    ⑥处理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 
    #include
    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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    ⑩ vector乱序

    // random_shuffle_demo.cpp : 定义控制台应用程序的入口点。
    //
    
    
    
    #include   
    #include  
    #include  
    #include   
    
    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] << " "; /* 显示打乱顺序后的元素 */
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    ①① 字典对应

    // random_shuffle_demo.cpp : 定义控制台应用程序的入口点。
    //
    
    
    
    #include   
    #include  
    #include  
    #include   
    #include
    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] << " "; /* 显示打乱顺序后的元素 */
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    ①② 分割字符串

    // random_shuffle_demo.cpp : 定义控制台应用程序的入口点。
    //
    
    
    
    #include   
    #include  
    #include  
    #include   
    #include
    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] << " "; /* 显示打乱顺序后的元素 */
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    ①③ 删除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;
    	}
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ①⑤ 找字符串中的数字

    
    /*
    提取字符串中的数字
    */
    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;
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
  • 相关阅读:
    Python爬虫——JsonPath解析方式
    4-20mA转RS-485,Modbus数据采集模块 YL121
    网站页脚展示备案号并在新标签页中打开超链接
    Day_43插入排序
    流媒体传输 - RTSP 协议报文分析
    [docker] volume 补充 & 环境变量 & 参数
    frps内网穿透
    安全评估报告怎么写 安全风险评估报告流程 安全评估报告认定
    获得淘宝商品快递费用接口调用展示
    【云原生之kubernetes实战】在k8s环境下部署Leanote蚂蚁笔记工具
  • 原文地址:https://blog.csdn.net/Cream_Cicilian/article/details/127948541
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号