
c/c++小例子
铲灰
平凡的一个人
展开
专栏收录文章
- 默认排序
- 最新发布
- 最早发布
- 最多阅读
- 最少阅读
-
ffmpeg音视频开发基础与实战
日常抄代码,每天学一点原创 2022-08-21 17:17:19 · 1732 阅读 · 0 评论 -
验证C++中的虚函数表的存在
///从网上抄来的,做了点修改,能正确运行出来而已#include <stdio.h>#include <iostream>using namespace std;///基类,有2个成员变量width和height,有2个虚函数和一个成员函数class Shape{public: Shape(int a = 0, int b = 0) { width = a; height =b; } virt...原创 2022-04-04 00:05:26 · 1161 阅读 · 0 评论 -
C++ Json::Value 依次获取Json中的元素值
jsHttp = { "headers" : { "1111" : "111", "2222" : "222", "3333" : "333" }} ///Json::Value 依次获取jsSrc中的每个元素 const Json::Value jsSrc = jsHttp["headers"]; Json::Value::Members member = jsSrc.getMemberNames(); Json...原创 2021-03-17 16:58:54 · 6159 阅读 · 0 评论 -
C++将数字转化成字符串
#include <iostream>#include <sstream>using namespace std;int main(){ ostringstream os; int i = 1234; os << i; std::string str = os.str(); cout << str << endl; return 0;}原创 2021-02-27 11:07:13 · 2184 阅读 · 0 评论 -
C++用ifstream读取文件内容
#include <iostream>#include <fstream>using namespace std;void read_file(const std::string &path, std::string &out){ ///std::ifstream fs(path.c_str(), std::ios_base::binary); std::ifstream fs(path, std::ios_base::binary);...原创 2021-01-03 13:55:00 · 3139 阅读 · 0 评论 -
将整数转换成十六进制字符串
#include <string>#include <iostream>using namespace std;std::string from_i_to_hex(size_t n) { const char *charset = "0123456789abcdef"; std::string ret; do { ret = charset[n & 15] + ret; n >&...原创 2021-01-03 13:42:59 · 2323 阅读 · 2 评论 -
把十六进制字符串转换成整数
#include <iostream>#include <string>#include <stdio.h>using namespace std;bool is_hex(char c, int &v){ if (0x20 <= c && isdigit(c)) { v = c - '0'; return true; } else if ('A' <= ...原创 2021-01-03 13:38:07 · 1325 阅读 · 0 评论 -
《C++沉思录》第13章访问容器中的元素
/*1.用类来表示概念,将复杂的功能拆分成小模块,每个类只做一件事情。2.Pointer来专门用来访问数据,Array_data类是中间层,避免了用户删除对象指针导致内存泄漏的问题,Array类是专门提供给用户操作的类(层次分明)*////Array用户避免使用指针操作template<class T>class Array{ friend class Pointer<T>;public: Array(unsigned size): ...原创 2021-01-02 15:35:53 · 185 阅读 · 1 评论 -
《C++沉思录》第12章简单数组Array类
#ifndef _ARRAY_H__#define _ARRAY_H__///设计Array类void f(){ int* p; { Array<int> x(20); p = &x[10]; }}/** 存在2个缺陷:* 第一个:Array<T>::operator[]返回一个T&,所以根本无法阻止用户取得它返回的对象的地址,在上面的例子中,Array对象x超出了作用域,而p还指向它的...原创 2020-12-20 10:35:39 · 188 阅读 · 1 评论 -
C/C++利用管道机制实现输出重定向功能
#include<sys/types.h>#include<sys/stat.h>#include<unistd.h>#include<fcntl.h>#include<stdio.h>#include<stdlib.h>#include<errno.h>#include<string.h>#include<signal.h>#include <string>using原创 2020-11-19 10:18:56 · 889 阅读 · 0 评论 -
类A的函数注册到类B的实现代码
#include <iostream>using namespace std;class A{public: ///proc是一种函数指针类型,该类型的返回值是void,有2个int类型的参数 typedef void (A::*proc)(int, int); void attach(proc p) { m_proc = p; } void d(int a, int b) { cout <...原创 2020-11-16 18:06:08 · 149 阅读 · 0 评论 -
《C++沉思录》第六章-句柄:第一部分
///学习了一下《C++沉思录》第六章,手动敲了下代码,加深印象///本章采用引起计数器的技术避免了对象的复制时进行不必要的开销,同时介绍了指针语义和值语义的区别#ifndef __POINT_H__#define __POINT_H__class Point{public: Point():xval(0), yval(0){} Point(int x, int y):xval(x), yval(y){} int x() const { re...原创 2020-11-08 11:21:11 · 163 阅读 · 0 评论 -
僵尸进程
僵尸进程: 子进程退出,父进程没有回收子进程资源(PCB),则子进程变成僵尸进程孤儿进程: 父进程先于子进程结束,则子进程成为孤儿进程,子进程的父进程成为1号进程init进程,称为init进程领养孤儿进程一个进程在终止时会关闭所有文件描述符,释放在用户空间分配的内存,但它的PCB还保留着,内核在其中保存了一些信息:如果是正常终止则保存着退出状态,如果是异常终止则保存着导致该进程终止的信...原创 2020-02-26 10:48:50 · 210 阅读 · 0 评论 -
fork函数
#include <unistd.h>#include <sys/types.h>#include <stdio.h>#include <stdlib.h>/*pid_t fork(void);子进程复制父进程的0到3g空间和父进程内核的PCB,但id不同fork调用一次返回两次父进程中返回子进程ID子进程中返回0读时共享,写...原创 2020-02-26 10:27:00 · 125 阅读 · 0 评论 -
select函数实现非阻塞读数据
#include <iostream>#include <map>#include <string.h>#include <unistd.h>#include <assert.h>#include <queue>#include <sys/types.h>#include <sys/stat.h...原创 2019-12-26 15:31:43 · 583 阅读 · 1 评论 -
C++类中函数指针的用法
#include <iostream>#include <string>#include <map>#include <vector>using namespace std;class A{public: typedef void (A::*PROC)(int a1, int a2);///这里的PROC是void ()(in...原创 2019-11-01 11:19:58 · 500 阅读 · 0 评论 -
C++根据字符提取字符串
int main(){ string url = "http://10.12.2.136:80/api/v3"; string s; char* str = "http://"; int len = strlen("http://"); if (strstr(url.c_str(), str)) { s = url.substr(...原创 2019-10-22 17:12:48 · 1021 阅读 · 0 评论 -
简单模板单例
#include <stdio.h>#include <string.h>#include <stdlib.h>#include <iostream>using namespace std;#define BT_SAFE_DELETE(p) if ((p)) {delete (p);(p) = NULL;}/** 这里...原创 2019-05-11 14:31:37 · 177 阅读 · 0 评论 -
简单版本的类A回调类B的成员函数
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <iostream>using namespace std;class A;class B{public: ///这里的(A::*test1)中用到了类A,所以需要前置声明一下 void...原创 2019-07-19 13:35:45 · 201 阅读 · 0 评论 -
每行打印一个单词
#include <stdio.h>#include <stdlib.h>#define IN 1 // 在单词内#define OUT 0 //在单词外int main(){ int c = 0 ; int state = OUT; while ((c = getchar()) != EOF) { ...原创 2018-05-14 22:46:27 · 340 阅读 · 0 评论 -
编程实现删除字符串中所有指定的字符
#include #include #include void delete_char(char *string, char c){//检查string是否有效性if (string == NULL){printf("string is NULL\n");return;}char *p, *q;p = q = string;whi原创 2016-12-02 09:01:37 · 1782 阅读 · 0 评论 -
分割字符串,提取数字中的最大值
void split(char str[],char delims[]){ int max_num = 0; char *result = NULL; result = strtok( str, delims ); while( result != NULL ) { if (atoi(result) > max_num) ...原创 2019-05-15 14:11:24 · 751 阅读 · 0 评论 -
内存释放多次
#include <iostream>using namespace std;class A{public: A(){}};int main(){ A *a = new A; /* * delete a; * delete a; *如果释放2次,程序就会挂掉,最好delete之后把a赋值为NULL,每次使...原创 2019-04-29 14:15:26 · 734 阅读 · 0 评论 -
字符串的操作函数
#ifndef _My_String_#define _My_String_#include class MyString{ private:int size;char *data;public:void print(){std::cout}int length(){return size;}原创 2016-12-31 14:36:30 · 282 阅读 · 0 评论 -
笔记——2
列举并解释c++中的4种运算符转化以及它们的不同点1.const_cast操作符:用来帮助调用哪些应该使用却没有使用const关键字的函数。换句话说,就是供程序设计师在特殊情况下将限制为const成员函数的const定义解除,使其能更改特定属性2.dynamic_cast操作符:如果启动了支持运行时间类型信息(RTTI),dynamic_cast可以有助于判断在运行时所指向对象原创 2017-01-08 19:10:09 · 252 阅读 · 0 评论 -
c/c++笔记
内建数据类型的情况下,效率没有区别自定义数据类型的情况,++i的效率较高c++和c有什么不同1. C是一个结构化语言,它的重点在于算法和数据结构。对语言本身而言,c是c++的子集。c程序的设计首要考虑的是如何通过一个过程,对输入进行运算处理,得到输出。对于c++,首要考虑的是如何构建一个对象模型,让这个模型能够配合对应的问题,这样就可以通过对象的状态信息得到输出或实现过原创 2016-12-02 09:16:11 · 200 阅读 · 0 评论 -
编程实现strcat库函数
#include #include #include void my_strcat(char *str1, char *str2){//判断字符是否为空if (str1 == NULL || str2 == NULL){printf("str1 == NULL || str2 == NULL");return;}//到str1的末尾whi原创 2016-12-02 09:13:47 · 399 阅读 · 0 评论 -
不使用库函数将字符串转换为数字
//string是你输入的字符串//num是一个指针变量,表示字符串string转换为整数的值为numvoid char_change_num(int *num, char *string){ //判断字符串或者num是否为空 if (string == NULL || num == NULL) { printf("string i原创 2016-11-18 08:41:26 · 745 阅读 · 0 评论 -
自己实现strcpy函数
#include #include #include //source是源字符串,desc是目的字符串//字符串从源字符串拷贝到目的字符串void silence_strcpy(char *desc, char *source){//养成一个好习惯,判断主调函数分配的内存是否为空 if (desc == NULL || source == NULL)原创 2016-11-18 09:00:01 · 408 阅读 · 0 评论 -
编程实现字符串中子串的查找
//src是源字符串,sub是需要查找字符的长度//返回值为在子串在源字符串的位置int silence_strstr(const char *src, const char *sub){ if (src == NULL || sub == NULL) { printf("src == NULL || sub == NULL\n");原创 2016-11-18 10:20:28 · 556 阅读 · 0 评论 -
不使用库函数,将十进制数转换为二进制数
很简单的转换函数原创 2016-11-25 09:18:58 · 623 阅读 · 0 评论 -
封装一个打印日期和行号的函数
封装一个打印调试信息的宏,这样就不需要每次都printf("xxxxxxxx\n")l了原创 2017-07-17 17:05:57 · 498 阅读 · 0 评论 -
C++中将数字转换成string类型
#include #include #include using namespace std;int main(){int i = 54;string str;str = to_string(i);//将int转换成stringcout printf("str = %s\n", str.c_str());cin原创 2017-09-27 22:18:24 · 9186 阅读 · 2 评论 -
对端主动关闭连接后,recv收的返回值为0
#include<stdio.h>#include<stdlib.h>#include<string.h>#include<errno.h>#include<sys/types.h>#include<sys/socket.h>#include<netinet/in.h>#include <uni...原创 2019-04-17 18:54:49 · 1705 阅读 · 0 评论 -
线程简单操作
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <unistd.h>#include <string.h>void *f(void *arg){ int n = 3; while (n--) { printf(...原创 2018-07-30 16:15:53 · 165 阅读 · 0 评论 -
线程简单操作
#include <stdio.h>#include <stdlib.h>#include <pthread.h>#include <unistd.h>void *f1(void *arg){ printf("hello 111111\n"); return (void*)1;}void *f2(void *arg...原创 2018-07-30 15:43:11 · 170 阅读 · 0 评论 -
使用C++实现锁机制
#include <stdio.h>#include <assert.h>#include <stdlib.h>#include <iostream>#include <unistd.h>#include <pthread.h>using namespace std;class Mutex{public: Mutex() { ...原创 2018-06-26 09:16:42 · 2619 阅读 · 0 评论 -
lua中的lua_rawgeti和lua_rawseti函数用法
// gcc a.c -llua5.1#include <lua5.1/lua.h>#include <lua5.1/lualib.h>#include <lua5.1/lauxlib.h>int l_map(lua_State *L){int i, n;//第一个元素必须是tableluaL_checktype(L, 1, LUA_TTABL...原创 2018-04-19 22:32:25 · 6529 阅读 · 0 评论 -
C++中枚举的用法
#include <iostream>using namespace std;class A{public: A(){}public: enum Count{ aa = 1, bb = 2, cc = 3, dd = 4, }; private:};int main(){ cout <<...原创 2018-03-15 13:07:51 · 676 阅读 · 0 评论 -
一个字符串是否是另外一个字符串的子字符串
一个字符串是否是另外一个字符串的子字符串原创 2017-11-08 20:30:38 · 527 阅读 · 0 评论