自定义博客皮肤VIP专享

*博客头图:

格式为PNG、JPG,宽度*高度大于1920*100像素,不超过2MB,主视觉建议放在右侧,请参照线上博客头图

请上传大于1920*100像素的图片!

博客底图:

图片格式为PNG、JPG,不超过1MB,可上下左右平铺至整个背景

栏目图:

图片格式为PNG、JPG,图片宽度*高度为300*38像素,不超过0.5MB

主标题颜色:

RGB颜色,例如:#AFAFAF

Hover:

RGB颜色,例如:#AFAFAF

副标题颜色:

RGB颜色,例如:#AFAFAF

自定义博客皮肤

-+
  • 博客(76)
  • 收藏
  • 关注

原创 C++之String常用操作

1-字符串分配 assign()#include <iostream>#include <string>using namespace std;int main(){ string str1 = "0123456789"; string str2,str3; //assign参数为const char[]数组与const char*不同之处在于, //后一位参数分别为截取前3位以及截取3位之后的字符 str2.assign("0123456",3);

2021-10-13 00:17:42 158

原创 C++(windows)遍历文件夹

#include <iostream>#include<windows.h>#include<tchar.h>using namespace std;int main(){ //------注意需要设置多字符集,使用wcout,不然会输出不全 HANDLE hand; LPCTSTR lpFileName = _T("F:\\*.*"); //指定搜索目录和文件类型,如搜索d盘的音频文件可以是"D:\\*.mp3",*.*搜索全部文件。 WIN32

2021-08-09 00:14:46 484

原创 C++回溯算法之二

/*题目地址:https://leetcode-cn.com/problems/restore-ip-addresses/给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式。有效的 IP 地址 正好由四个整数(每个整数位于 0 到 255 之间组成,且不能含有前导 0),整数之间用 '.' 分隔。例如:"0.1.2.201" 和 "192.168.1.1" 是 有效的 IP 地址,但是 "0.011.255.245"、"192.168.1.312" 和 "[email protected]

2021-07-22 23:37:04 129 1

原创 C++回溯算法解数独

/* char str [9][9] = { '5', '3', '.', '.', '7', '.', '.', '.', '.', '6', '.', '.', '1', '9', '5', '.', '.', '.', '.', '9', '8', '.', '.', '.', '.', '6', '.', '8', '.', '.', '.', '6', '.', '.', '.', '3', '4', '.',

2021-07-22 23:27:57 179

原创 C++回溯算法之一

/*给出由小写字母组成的字符串 S,重复项删除操作会选择两个相邻且相同的字母,并删除它们。在 S 上反复执行重复项删除操作,直到无法继续删除。在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。示例:输入:"abbaca"输出:"ca"解释:例如,在 "abbaca" 中,我们可以删除 "bb" 由于两字母相邻且相同,这是此时唯一可以执行删除操作的重复项。之后我们得到字符串 "aaca",其中又只有 "aa" 可以执行重复项删除操作,所以最后的字符串为 "ca"。*/#i

2021-07-16 22:43:17 187

原创 算法之滑动窗口

*/给定一个含有 n 个正整数的数组和一个正整数 target 。找出该数组中满足其和 ≥ target 的长度最小的 连续子数组 [numsl, numsl+1, …, numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。示例 1:输入:target = 7, nums = [2,3,1,2,4,3]输出:2解释:子数组 [4,3] 是该条件下的长度最小的子数组。**/*给定一个含有 n 个正整数的数组和一个正整数 target 。找出该数组中满足其

2021-07-06 17:49:01 45

原创 Vector插入数组和遍历数组

#include <iostream>#include<string>#include<algorithm>#include<vector>#include<map>using namespace std;int main(){ /* C++中,数组其实就是一个int*的指针,vector中插入int*类型,即可插入数组 遍历时需要取二级指针 */ int num [4]= {1,2,3,4}; vector&

2021-07-04 21:20:39 1127

原创 C++常用算法之二(二分查找)

/-------找出两个字符数组中不同的字符-------------//*-------找出两个字符数组中不同的字符-------------*/#include <iostream>#include<map>using namespace std;int main(){ char s[] = "abcd"; //找出两组字符串中不同的字符 char t[] = "abcde"; map<char, int> m; for (size_t i

2021-07-04 17:27:33 373

原创 C++常用算法题之一(移除元素,统计个数,查找重复元素)

1-/-------计算连续1的最大个数-------------//*-------计算连续1的最大个数-------------*/#include <iostream>using namespace std;int main(){ int sum[] = {1,1,0,1,1,1,1}; //题目给定数组 int count = 0; //保存1的个数值 int result = 0; //保存1的连续最大个数值 /* 思路:用count来统计连续个数,用resu

2021-07-01 20:34:23 483

原创 C++升/降序排列(任意输入数字,自动排序小程序)

/*-----------------升序排列-------------------*/#include <iostream>using namespace std;{ int arr[]= {9,8,7,6,5,4,3,2,1}; int temp; cout << "排序前arr数组为:"<<endl; for (auto a : arr) { cout << a << ends; } cout <&lt

2021-06-21 19:31:33 2464 1

原创 C++虚析构和纯虚析构

/*------虚析构和纯虚析构---------*/#include<iostream>using namespace std;class Animal{public: virtual void speak() = 0; //纯虚函数,抽象类 virtual ~Animal() //将抽象类的析构函数设为虚析构,才可以走子类的析构函数 //否则子类的析构函数不执行,无法释

2021-06-17 23:28:12 42

原创 C++纯虚函数

/*----------纯虚函数------------*/#include<iostream>using namespace std;class Test{public: virtual void func() = 0; //纯虚函数的基本格式};class Call:public Test {public: void func() //重写父类虚函数 { cout << "子类重写函数被

2021-06-17 23:27:42 35

原创 C++多态基本案例

/*多态基本案例*/#include<iostream>using namespace std;class Animal{public: virtual void speak() //添加virtual关键字,得以区分父类,基类相同函数名的调用 { cout<<"动物在说话"<<endl; }};/*下面两个继承父类Animal,并且函数名相同*/class Cat:public Animal {publi

2021-06-17 23:26:48 167

原创 纯虚函数练习小案例

#pragma once//头文件 #include"Computer.h"/*------纯虚函数电脑组装练习案例---------*/#include<iostream>using namespace std;/*---------------------------------------------------*/class Cpu //cpu抽象类{public: virtual void Calculate() = 0;};class

2021-06-17 23:26:17 105

原创 C++运算符重载

/*----------------+号运算符重载---------------*/#include<iostream>using namespace std;class Person{public: Person operator+(Person &p) { Person temp; temp.first_num = this->first_num + p.first_num; temp.secon

2021-06-15 21:43:54 43

原创 C++函数对象封装器

#include<iostream>#include<functional>using namespace std;int fun(int n){ cout << n << endl; return n;}int main(){ /*函数对象包装器--头文件<functional>*/ //为函数提供了一种容器(封装) //普通函数 std::function<int(int)> f = fun; f(

2021-06-12 16:48:18 83

原创 Lambda表达式基础使用

//Lambda基本格式#include<iostream>#include<string>#include<algorithm>using namespace std;int num(int fir, int sec){ return fir + sec;};int main(){ /*Lambda的基本格式*/ auto fun1 = [] (){return 40; }; //如果忽略(),即等于空括号,没有参数 cout <&lt

2021-06-12 16:17:25 43

原创 利用<vector><deque>容器,写一个平均分测试小程序

#include<iostream>#include<deque>#include<vector>#include<string>#include<algorithm>#include<stdio.h>#include<ctime>using namespace std;class Person{ public: Person(string name,int score) { thi...

2021-06-10 22:53:39 129

原创 顺序容器之vector基础操作

/*vector基础操作*/#include<iostream>#include<vector>using namespace std;int main(){ /*容器赋值操作*/ vector<int> v1, v2, v3; v1.push_back(10); v2 = v1; //等号赋值 v3.assign(v2.begin(),v2.end()); //使用assign(分配)赋值 /*容器大小*/ auto sum=v

2021-06-09 20:30:32 53

原创 C++输入输出流(读取,写入,复制文件,读取单个中文字符)

//读取文件内容#include<string>#include<iostream>#include<fstream>using namespace std;int main(){ char c; fstream file; file.open("test.txt",ios::in); while (!file.fail()) { if (file.get(c)) { cout << c; }

2021-06-06 22:00:14 771

原创 C++使用stringstream流获取输入信息

#include<string>#include<iostream>#include<fstream>#include<vector>#include<sstream>using namespace std;class PersonInfo{public: string name; vector<string> phone;private:};int main(){ string line, wor

2021-06-06 17:57:06 202

原创 编写一个小程序,使用范围for循环,将字符串中的文字,全部替换为X

//编写一个小程序,使用范围for循环,将字符串中的文字,全部替换为X#include <string>#include<iostream>using namespace std;int main(){ string s("some things"); string result; for (auto &c:s) { if (isalnum(c)) { result = result + "x"; } } cout &

2021-06-04 20:27:41 276

原创 利用指针将二维数组转换成一维数组

#include <iostream>using namespace std;//利用指针将二维数组转换成一维数组int main(int argc, char* argv[]){ int num[5][5] = {1,2,3,4,5,6,7,8,9,10}; int sum[20]; int* p = &num[0][0]; for (size_t i = 0; i < 10; i++) { sum[

2021-05-29 23:10:05 673

原创 字符串数组拼接字符

#include <iostream>using namespace std;//字符拼接int main(int argc, char* argv[]){ char str1[100], str2[100]; char* p1 = str1; char* p2 = str2; gets_s(str1,99); gets_s(str2,99); while (*p1) { p1++; } whi

2021-05-29 22:56:04 214

原创 利用数组写出一个统计学生平均成绩的程序

#include <iostream>using namespace std;int main(){ int resoult[10]; //学生成绩数组 int input; int sum = 0; //学生成绩总数 int count = 0; //数组下标位置 cout << "请输出学生成绩(输入-1自动结束)" <

2021-05-28 20:55:20 1011

原创 linux系统编程之poll函数使用案例

#include<unistd.h>#include<sys/socket.h>#include<netinet/in.h>#include <strings.h>#include<error.h>#include <errno.h>#include<stdio.h>#include<stdlib.h>#include<signal.h>#include <sys/wait.h

2020-10-05 21:11:33 233

原创 TCP服务端/客户端基本创建代码(字符回射小程序)

TCP服务端代码:/*tcp/ip服务端的基本创建流程如下1-socket 创建套接字,拿到套接字描述符2-bind 将地址端口协议族绑定到套接字描述符上3-listen 开始监听端口4-accept 开始接受服务端发出的信息数据以上为基本代码创建流程,代码并不完善,只是雏形,将在后续的程序进行改进,并且为了代码的直观性并未判断返回值错误与否*/#include<unistd.h>#include<sys/socket.h>#include<

2020-10-01 20:48:19 121

原创 Linux套接字(1)--简单的传输接受数据

头文件"proto.h"#ifndef PROTO_H__#define PROTO_H__ #include <stdint.h>#define NAMESIZE 13 struct msg_st { uint8_t name[NAMESIZE]; uint32_t math; uint32_t chinese;}__attribute__((packed)); #endif接收方:#include<stdio.h

2020-09-08 20:51:19 122

原创 APUE复习--标准I/O操作(1)

1-复制文件(fopen,fclose,fgetc,fpuc)/*标准io实现一个复制文件的小case打开一个文件(fopen),通过返回值获取其中的内容,然后使用fgetc和fputc两个函数,将读取的文件流写入到另外一个流中。注意:在windows平台二进制文件需要加上b(bin)模式。 另此程序案例将绝对路径写死,也可以通过argv[1],来指定路径同样也可以使用fgets和fpus函数,不同在于,这两个函数进行字符串操作,可以一次读取写入多个指定数量的字符注意:fgets如果读取到

2020-08-22 20:32:10 67

原创 Linux系统编程之管道

/*最基本的一个创建管道的方法1-创建管道2-创建一个子进程3-关闭父进程读端,并写入数据4-子进程关闭写端,读取数据,并向标准输出写入*/#include "apue.h"intmain(void){ int n; int fd[2]; pid_t pid; char line[MAXLINE]; if (pipe(fd) < 0) //创

2020-08-10 23:16:51 187

原创 Linux系统编程之守护进程(Daemon)

/*创建守护进程步骤:1-创建父进程,让父进程退出2-在子进程中,调用setsid创建一个新的会话(会话是一个隐藏在后台,没有窗口的进程)3-改变当前目录(chdir函数)4-重设文件掩码:子进程从父进程继承的文件创建屏蔽字可能会拒绝某些许可权。为防止这一点,使用unmask(0)将屏蔽字清零5-关闭文件描述符:如不关闭,将会浪费系统资源,造成进程所在的文件系统无法卸下以及引起无法预料的错误6-处理SIGCHLD信号:将SIGCHLD信号的操作设为SIG_IGN。这样,子进程结束时不会产生僵尸

2020-08-06 21:23:11 77

原创 Linux系统编程之读写锁

/*1-读写锁,读共享,写独占2-读写锁,写加锁为pthread_rwlock_wrlock,读加锁为pthread_rwlock_rdlock,解锁同为unlock.3-读写锁类型为pthread_rwlock_t类型4-读写锁需要初始化,类似互斥锁--pthread_rwlock_init5-使用完毕后需要调用pthread_rwlock_destroy销毁锁6-c++通过pthread_create函数传参时(第四个参数),需要加&符,否则报段错误7-所有函数均未加返回值判断,.

2020-08-05 20:59:42 100

原创 Linux系统编程之线程条件变量

#include<pthread.h>#include<stdio.h>#include<unistd.h>#include<stdlib.h>pthread_cond_t cond=PTHREAD_COND_INITIALIZER; //初始化条件变量pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;//静态初始化互斥锁struct msg //公共代码区{ int num; .

2020-08-04 21:19:48 70

原创 Linux系统编程之创建线程和互斥锁

一个互斥锁的小例子#include<pthread.h>#include<stdio.h>static int glob=0;static pthread_mutex_t mutex=PTHREAD_MUTEX_INITIALIZER;//静态初始化互斥锁static void* fun(void * arg){ int loc; for ( int i = 0; i < 100000; i++) {

2020-08-02 22:07:18 95

原创 Linux系统编程之创建进程

#include"apue.h"#include<pthread.h>#include<stdio.h>void *prtid(void* ){ printf("进程id=%u\n,线程id==%u\n",getpid(),pthread_self()); }int main(int argc, char const *argv[]){ pthread_t thread; pthread_create(&thread,

2020-07-28 22:24:19 53

原创 Linux系统编程之信号集

#include "apue.h"static voidsig_quit(int signo){ printf("caught SIGQUIT\n"); if (signal(SIGQUIT, SIG_DFL) == SIG_ERR) err_sys("can't reset SIGQUIT");}intmain(void){ sigset_t newmask, oldmask, pendmask; //设置三个信号集备用 if (signal(SIGQUIT, sig_qu

2020-07-23 21:33:00 108

原创 Linux系统编程之定时器(longjmp,setjmp,alram,signal)

#include "apue.h"#include <setjmp.h>/*一个简单的计时器代码,倒计时10秒就会推出程序*/static jmp_buf env;static void arm(int sig){ longjmp(env,1);} intmain(void){ char buf[20]; int n; signal(SIGALRM,arm); if ( set

2020-07-22 21:37:27 106

原创 Linux系统编程之wait

#include "apue.h"#include <sys/wait.h>#include "apue.h"#include <sys/wait.h>void pr_exit(int status) //用来打印wait退出信号的函数{ if (WIFEXITED(status)) printf("normal termination, exit status = %d\n", WEXITSTATUS(status)); else if (WIFSIGN

2020-07-14 20:27:10 75

原创 Linux系统编程之创建,写入,复制文件

这里写自定义目录标题#include <fcntl.h>#include <unistd.h>#include <stdio.h>#define PERMS 0666 #define DUMMY 0#define MAXSIZE 1024 //常数定义int main(int argc, char *argv[]){ int sourcefd, targetfd; //目标文件和源文件的描述符 int readCou

2020-07-07 22:37:54 207

原创 Linux循环创建子进程

#include<unistd.h>#include<iostream>using namespace std;int main(){ pid_t pid; int i=0; for ( i = 0; i <5; i++) { if ((pid=fork())==0)//通过判断语句添加break,终端子进程重复创建孙进程 { break; } } if (pid>0) { sleep(1); cout<&l

2020-06-24 23:01:53 360

空空如也

空空如也

TA创建的收藏夹 TA关注的收藏夹

TA关注的人

提示
确定要删除当前文章?
取消 删除