C++基础
基础程序实现
阿飞_Y
机器学习
展开
-
【千律】C++基础:删除只读属性文件、文件剪切、修改文件扩展名
1.删除带有只读属性的文件#include <tchar.h>#include <windows.h>int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow){ // 变量初始化 LPCTSTR ExistFile = _T("D:\\File.txt"); // 获取..原创 2022-04-24 16:47:25 · 2059 阅读 · 0 评论 -
【千律】C++基础:文件的删除、复制、移动和重命名
#include <tchar.h>#include <windows.h>int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow){ // 变量初始化 LPCTSTR ExistFile = _T("D:\\File.txt"); LPCTSTR NewFile = _T("D.原创 2022-04-24 15:50:44 · 1951 阅读 · 0 评论 -
【千律】C++基础:将字符串通过 cmd 命令行对EXE文件传参
#include <tchar.h>#include <windows.h>int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow){ // 显示提示框 MessageBox(NULL, lpCmdLine, _T("提示窗口"), MB_ABORTRETRYIGNORE | M.原创 2022-04-23 17:53:28 · 1235 阅读 · 0 评论 -
【千律】C++基础:ShellExecuteEx 函数的相关使用案例
1. 以 Windows 系统自带的浏览器,打开指定的网址。#include <tchar.h>#include <Urlmon.h>#include <windows.h>#pragma comment (lib, "Urlmon.lib")int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _.原创 2022-04-22 20:27:39 · 1277 阅读 · 0 评论 -
【千律】C++基础:打开并下载网页 -- ShellExecuteEx 和 URLDownloadToFile 函数
#include <tchar.h>#include <Urlmon.h>#include <windows.h>#pragma comment (lib, "Urlmon.lib")int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmdLine, _In_ int nCmdShow){ // 初始化变量.原创 2022-04-21 17:05:07 · 1169 阅读 · 0 评论 -
【千律】C++基础:string扩展工具箱的使用方法
#include <vector>#include <string.h>#include <windows.h>#include "string_util.h"using namespace std;int main(){ // 初始化字符串 string str_cctry = " Hello, world! My name is cctry.com. "; // 去除两端空格 str_cctry = string_util::trim(st.原创 2022-04-19 15:34:06 · 316 阅读 · 0 评论 -
【千律】C++基础:CString类的常用方法和控制台输出
1.CString类的常用方法2. 在控制台输出CString#include <iostream>#include "atlbase.h"#include "atlstr.h"using namespace std;int main(){ // 初始化字符串 CString str = _T("你好,世界!Hello, world "); // 去除字符串末尾空格 CString str_trim = str.Trim(); // 解决控制台不输出中文.原创 2022-04-17 21:35:31 · 1453 阅读 · 0 评论 -
【千律】C++基础:宽窄字节字符串的相互转换与控制台输出
方案1:#include <string>#include <tchar.h>#include <iostream>#include <windows.h>using namespace std;// 窄字节转换为宽字节wchar_t* str_a2w(char* str_a){ // 计算字节所需长度 int str_a_len = MultiByteToWideChar(CP_ACP, 0, str_a, -1, NULL, 0.原创 2022-04-17 19:12:21 · 762 阅读 · 0 评论 -
【千律】C++基础:计算宽窄字节字符串所占用的字节数
#include <string>#include <tchar.h>#include <iostream>using namespace std;int main(){ // 初始化字符串 char* str_v = "20220425"; // 窄字节字符串 wchar_t* str_w = L"20220425"; // 宽字节字符串 TCHAR* str_t = _T("20220425"); // 通用字.原创 2022-04-17 16:52:45 · 1066 阅读 · 0 评论 -
【千律】C++基础:根据指定字符“#”对字符串进行分割,将分割后的字符串存储在vector动态数组中
#include <vector>#include <string>#include <iostream>using namespace std;int main(){ // 初始化变量 string str_cctry = "123#abc#cctry.com#"; vector<string> vec_cctry; // 判断字符串中是否存在“#”字符 while (str_cctry.find("#") != -1) { .原创 2022-04-11 20:17:30 · 1177 阅读 · 0 评论 -
【千律】C++基础:map 循环遍历删除元素,及其报错的解决方案
报错原因:采用erase移除迭代器后,迭代器的值变为-572662307,无法作为迭代器继续运算。详细:当程序执行到 stu_map.erase(itor) 时,满足条件的第一个元素被删除,从而导致 itor 指针被删除,使其不指向任何元素。针对该问题我们首先想到的就是在删除指针之前,对其进行备份,C++ 将这个临时变量直接建立在 erase 实现里。因此将stu_map.erase(itor)改为stu_map.erase(itor--)即可#include <map>#incl原创 2022-04-11 16:56:36 · 3036 阅读 · 1 评论 -
【千律】C++基础:List 链表循环删除元素,及其报错的解决方案
报错原因:采用erase移除迭代器后,迭代器的值变为-572662307,无法作为迭代器继续运算。详细:当程序执行到 list_int.erase(itor) 时,满足条件的第一个元素被删除,从而导致 itor指针被删除,使其不指向任何元素。针对该问题我们首先想到的就是在删除指针之前,对其进行备份,C++ 将这个临时变量直接建立在 erase 实现里。#include <list>#include <iostream>using namespace std;int.原创 2022-04-09 21:58:30 · 2719 阅读 · 1 评论 -
【千律】C++基础:使用vector动态数组初始化list链表,并输出list链表
使用vector动态数组初始化list链表(构造函数的方式),并输出list链表。#include <list>#include <vector>#include <iostream>using namespace std;int main(){ // 初始化动态数组 vector<int> vec_int{ 7, 9, 4, 2, 5, 6, 1, 3 }; // 初始化列表 list<int> list_int(ve原创 2022-04-09 17:27:44 · 1982 阅读 · 0 评论 -
【千律】C++基础:动态数组的数组插入与删除
实现:定义一个vector动态数组,将下列元素 {0, 1, 3, 2, 5} 插入到动态数组中。随后,通过for循环删除动态数组中值为 erase_num 的元素,并输出最后结果。#include <vector>#include <iostream>using namespace std;int main(){ // 初始化 int erase_num = 3; int int_num[] = {0, 1, 3, 2, 5}; vector<int&原创 2022-04-07 21:44:11 · 2426 阅读 · 0 评论 -
【千律】C++基础:使用动态数组vector初始化自定义类
使用动态数组vector初始化自定义类:构造函数的方式。#include <vector>#include <iostream>using namespace std;class CStudent{public: char* stu_name; int stu_numeber; // 构造函数 CStudent(char* p_name, int p_number) { stu_name = p_name; stu_numeber = p_nu.原创 2022-04-07 16:49:19 · 882 阅读 · 0 评论 -
【千律】C++基础:通过函数实现文件的拷贝与复制
#include <fstream>#include <iostream>using namespace std;// 将文件1的内容拷贝到文件2中(TXT文件)void CopyFile(char* iread_file, char* write_file){ // 初始化读取和写入 ifstream iread_txt; // 初始化输入流 ofstream write_txt; .原创 2022-04-07 14:23:37 · 1128 阅读 · 0 评论 -
【千律】C++基础:通过文件指针获取文件大小(字节数)
#include <fstream>#include <iostream>using namespace std;int main(){ // 初始化 ofstream write_txt; // 初始化输出流 // 打开文件 write_txt.open("D:\\words.txt", ios::app); // 判断文件是否打开 if (!write_txt.is_open()) { cout <.原创 2022-04-06 22:06:58 · 1273 阅读 · 0 评论 -
【千律】C++基础:判断文件是否读取到末尾,判断文件是否读取和写入成功。
#include <fstream>#include <iostream>#include <stdlib.h>using namespace std;int main(){ // 初始化 ifstream iread_txt; // 初始化输入流 ofstream write_txt; // 初始化输出流 char* words = "Hello, world!"; .原创 2022-04-04 19:51:44 · 5348 阅读 · 0 评论 -
【千律】C++基础:TXT文件的创建、写入和读取
#include <fstream>#include <iostream>using namespace std;int main(){ // 初始化 ifstream iread_txt; // 初始化输入流 ofstream write_txt; // 初始化输出流 char* words = "Hello, world!"; // 初始化字符串 // 打开文件 wr.原创 2022-04-04 16:39:57 · 4209 阅读 · 0 评论 -
【千律】C++基础:多态性与虚函数
虚函数:通过父类的指针,指向子类的对象。调用虚函数时,调用子类的函数。#include <iostream>using namespace std;class CDog {public: // 声明虚函数 virtual void barking() { cout << "All dogs are barking!" << endl; }};class CGolden : public CDog{public...原创 2022-04-03 00:18:08 · 300 阅读 · 0 评论 -
【千律】C++基础:父类对象和子类对象之间的相互转换
#include <iostream>using namespace std;// 枚举类型:用于判断子类类型enum RoomType{ Type_ErrorRoom = 0, Type_Star_Room = 1, Type_TrvalRoom = 2};// 定义父类class Hotels{public: int type; int price; char* name; // 构造函数 Hotels(const char* t_name, .原创 2022-04-01 20:04:59 · 1415 阅读 · 0 评论 -
【千律】C++基础:子类的构造函数和析构函数
#include <iostream>using namespace std;class CRoom{public: char* name; int number; // 设置价格 void SetPrice(int t_price) { price = t_price; } // 获取价格 int GetPrice() { return price; } // 构造函数 CRoom(const char* t_name, int t_n.原创 2022-04-01 18:21:51 · 546 阅读 · 0 评论 -
【千律】C++基础:类的派生与继承
#include <iostream>using namespace std;class CRoom{public: char* name; int number; // 设置价格 void SetPrice(int t_price) { price = t_price; } // 获取价格 int GetPrice() { return price; }private: int price;};class CStarRoom : .原创 2022-04-01 00:10:39 · 675 阅读 · 0 评论 -
【千律】C++基础:运算符重载
#include <iostream>using namespace std;class CRoom{public: char* name; int number; // 构造函数 CRoom(const char* t_name, int t_number) : number(t_number) { // 得到输入字符长度 int len = strlen(t_name); // 动态申请内存 name = new char[len + 1];.原创 2022-03-31 20:10:51 · 360 阅读 · 0 评论 -
【千律】C++基础:类的静态成员变量和静态成员函数
#include <iostream>using namespace std;class CRoom{public: char* name; int number; static int master; static void set_master(int mas_zoom);};// 初始化静态成员变量int CRoom::master = 1001;// 建立静态成员函数void CRoom::set_master(int mas_zoom){ ma.原创 2022-03-25 19:40:24 · 1317 阅读 · 0 评论 -
【千律】C++基础:对象的复制、赋值和拷贝构造函数
#include <iostream>using namespace std;class CRoom{public: char* name; int number; // 构造函数(复制) CRoom(CRoom &room) { this->name = room.name; this->number = room.number; } // 构造函数 CRoom(const char* t_name, int t_number) :.原创 2022-03-25 16:42:50 · 1302 阅读 · 0 评论 -
【千律】C++基础:析构函数
报错strcpy不安全解决方法:项目->属性->C/C++->预处理器->预处理器定义添加:_CRT_SECURE_NO_DEPRECATE_CRT_NONSTDC_NO_DEPRECATE#include <iostream>using namespace std;class CRoom {public: char* name; int number; // 构造函数 CRoom(const char* t_name, int t_原创 2022-03-22 22:43:19 · 1035 阅读 · 0 评论 -
【千律】C++基础:函数的重载
#include <iostream>using namespace std;// 传入指针 -- 函数重载void vswap(int* fig_1, int* fig_2){ // 将fig_1地址对应的变量赋值给fig_n。 int fig_n = *fig_1; // 将fig_2地址对应的变量赋值给fig_1地址对应的变量 *fig_1 = *fig_2; // 将变量fig_n赋值给fig_2地址对应的变量 *fig_2 = fig_n;}// .原创 2022-03-22 20:08:30 · 746 阅读 · 0 评论 -
【千律】C++基础:对字符串指定位置的字符变换大小写
实现功能:当输入参数b_odd_pos为ture时,将字符串中奇数位置的字符转换为大写,同时将偶数位置的字符转换为小写。反之,当b_odd_pos为false时,将字符串中奇数位置的字符转换为小写,同时将偶数位置的字符转换为大写。#include <iostream>using namespace std;bool string_upper_diy(char str[], bool b_odd_pos = true){ // 获取数组长度 int length = strlen原创 2022-03-22 18:21:35 · 1283 阅读 · 0 评论 -
【千律】C++基础:类的构造函数和初始化
#include <iostream>using namespace std;class CStudent{public: int age = 0; int num = 0; char sex; char name[50]; // 声明构造函数 CStudent(int stu_num, int stu_age) { num = stu_num; // 赋值学号 age = stu_age; // 赋值年龄 sex = 'f.原创 2022-03-20 19:44:23 · 199 阅读 · 0 评论 -
【千律】C++基础:类定义和类实现的分离
类定义就是指定义类名,是.h文件类实现是指对类定义的具体实现,是.cpp文件下面是Student.h中的内容:#pragma once#include <iostream>using namespace std;class CStudent{public: void GetStudent();private: int age = 0; int num = 0; string sex; string name;};下面是Student.cp原创 2022-03-18 21:12:50 · 613 阅读 · 0 评论 -
【千律】C++基础:类的定义与实现
要求:建立一个学生类,定义全部成员变量为私有,并添加一个公有的成员函数,实现从键盘录入该类成员变量的信息。#include <iostream>using namespace std;class CStudent{// 公有成员变量和成员函数public: void GetStudent() { cout << "请输入学生的名字:"; cin >> name; cout << "请输入学生的年龄:"; cin &g原创 2022-03-18 19:19:13 · 539 阅读 · 0 评论 -
【千律】C++基础:获取两个字符串中的最大相同子串
#include <iostream>using namespace std;char* GetString(char* str1, char* str2){ // 申请内存用于存储地址 char* SameString = new char[100]; // 初始化相同字符串长度 int update = 0; int undate_end = 2; // 获取字符串1的实际长度 int len_str1 = 0; while (str1[len_str1] !.原创 2022-03-17 19:05:08 · 1163 阅读 · 0 评论 -
【千律】C++基础:编写实现自定义的字符串拼接函数
#include <iostream>using namespace std;char* strconnect(char str_1[], char str_2[], int len_1, int len_2){ // 初始化字符串 str_1 的 int Actlen_1 = 0; // 得到字符串 str_1 的实际长度 for (int i = 0; i < len_1; i++) { if (str_1[i] == '\0') { Actlen.原创 2022-03-17 16:32:08 · 1272 阅读 · 0 评论 -
【千律】C++基础:编写实现数组升序排列的函数
#include <iostream>using namespace std;void MySort(int IntegerArray[], int length){ // 初始化中间变量 int InterVariables = 0; for (int i = 0; i < length; i++) { // 对第i个数进行i次比较,依次前移 for (int j = i; j > 0; j--) { // 对前后两个数字进行比较,将数字.原创 2022-03-17 15:07:11 · 2280 阅读 · 0 评论 -
【千律】C++基础:编写实现数组降序排列的函数
#include <iostream>using namespace std;void MySort(int IntegerArray[], int length){ // 初始化中间变量 int InterVariables = 0; for (int i = 0; i < length; i++) { // 对第i个数进行i次比较,依次前移 for (int j = i; j > 0; j--) { // 对前后两个数字进行比较,将数字.原创 2022-03-17 15:05:02 · 3460 阅读 · 0 评论 -
【千律】C++基础:通过递归函数计算N的阶乘
#include <iostream>using namespace std;int GetFactorial(int N){ // 递归到最后一位,返回1 if (N == 1) { return 1; } else if (N > 1) { // 若 N > 1,则计算 【N】 与【N-1 的递归值】 return GetFactorial(N - 1) * N; } return 0;}int main(){ int N = .原创 2022-03-16 23:08:52 · 562 阅读 · 0 评论 -
【千律】C++基础:求分数序列2/1, 3/2, 5/3, 8/5, 13/8, 21/13...前20项的和
#include <iostream>using namespace std;int main(){ // 初始化设置参数(分子:numerator 分母:denominator) double denominator = 1, numerator = 2, sum_nd = 0, update = 0; for (int i = 0; i < 20; i++) { // 累加分数序列 sum_nd += numerator / denominator; .原创 2022-03-16 19:53:51 · 1989 阅读 · 0 评论 -
【千律】C++基础:通过函数实现数据交换--引用方案
引用:引用作为函数参数进行传递时,实质上传递的是实参本身#include <iostream>using namespace std;void vswap(int& fig_1, int& fig_2){ int fig_n = fig_1; fig_1 = fig_2; fig_2 = fig_n;}int main(){ // 通过函数实现数据交换 int fig_1 = 12, fig_2 = 24; // 输出交换前的数据值 co.原创 2022-03-16 19:20:42 · 100 阅读 · 0 评论 -
【千律】C++基础:通过函数实现数据交换--指针方案
#include <iostream>using namespace std;void vswap(int* fig_1, int* fig_2){ // 将fig_1地址对应的变量赋值给fig_n。 int fig_n = *fig_1; // 将fig_2地址对应的变量赋值给fig_1地址对应的变量 *fig_1 = *fig_2; // 将变量fig_n赋值给fig_2地址对应的变量 *fig_2 = fig_n;}int main(){ // 通过函.原创 2022-03-16 19:08:22 · 97 阅读 · 0 评论