
C++
尽头牙
这个作者很懒,什么都没留下…
展开
-
【C++】模板函数介绍 Template
声明函数模板的声明格式template <class T> function_declaration;template <typename T> function_declaration;这里关键字class和typename的用法一样,随你习惯。举个例子,创建一个比较大小的模板函数:template <class T>T getMax(T a, T b){ return (a>b?a:b)}这里把T当做模板参数,它的类型还没有被定义,可以原创 2021-03-15 16:28:19 · 293 阅读 · 0 评论 -
【C++】引用& 用法 实例
在C++里 & 符号有两种意思,一个表示取址符号,另一个表示引用。引用介绍引用就是某一变量的一个别名,对引用的操作等于对变量直接操作,不会产生临时变量。//引用的声明方法:类型标识符 &引用名=目标变量名;int a;int &b = a; // b是a的引用(别名)注意声明引用以后必须初始化引用相当于变量的别名,不占内存。这时候&a=&b引用用法引用参数引用作为函数参数,相对于值传递有两个优点:1. 不会产生临时变量(实参副本)占用栈内存原创 2021-03-12 15:51:29 · 393 阅读 · 0 评论 -
【C++】list 用法总结
list是双向链表头文件#include <list>定义std::list<int> nameList; // empty liststd::list<int> nameList(20); // name list with 20 empty elementsstd::list<int> List(9, 13);原创 2021-03-11 17:34:02 · 682 阅读 · 0 评论 -
【C++】Vector 用法
Vector是所有相同对象的集合,每个对象有各自的索引。头文件#include <vector>using std::vector;声明vector<int> ivec; // ivec hold objects of type intvector<T> v1;vector<T> v2(9);vector<T> v3 = 10;vector<T> v4 = {1, 2, 3};vector 是一个模板原创 2021-03-08 15:15:30 · 470 阅读 · 4 评论 -
【C++11】bind函数用法
调用bind的一般形式:auto newCallable = bind(callable, arg_list);newCallable 是可调用对象,arg_list 是参数列表,绑定callable函数。当我们调用newCallable 时,newCallable 会调用callable,并且把arg_list 参数传给callable【一般绑定】bool check_size(const string &s, string::size_type sz){ retu原创 2021-02-20 10:38:51 · 561 阅读 · 0 评论 -
【Note】C++11 std::thread 创建线程实例
c++ 11 之后有了标准的线程库:std::thread,项目中用到过这里记一下。#include <iostream>#include <thread>// TestThread.hppclass TestThread{public: void init(); void threadA(); void threadB(int value);};// TestThread.cppvoid TestThread::init()原创 2021-01-06 13:43:05 · 367 阅读 · 0 评论 -
【Note】C++ 重载 overload
C++重载C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载。重载声明是指一个与之前已经在该作用域内声明过的函数或方法具有相同名称的声明,但是它们的参数列表和定义、实现不同。函数重载在同一个作用域内,可以声明功能类似的同名函数,这些同名函数的形式参数(指参数的个数、类型或者顺序)必须不同。class myOverload{public: void writeType(int value) { print.原创 2020-12-30 14:55:40 · 279 阅读 · 0 评论 -
[Note] C++ 函数作为参数传递实例
**********************/** class Gauge ***************************************/class Gauge{public: // declare a method to deliver address static void passMethod(void); void init();};void Gauge::passMethod(){ std::cout << "原创 2020-12-23 15:13:18 · 167 阅读 · 0 评论 -
【Note】C++ 函数后加 const override 的用法
1. const 用法const加在函数前表示函数返回值为const,加在函数后表示不可修改class的成员变量。const加在函数后面表示此函数“只读”。2. override用法override关键字表示重写父类的虚函数。1. 表示重写了父类的函数增加可读性 2. 编译器会检查重写的函数与父类的虚函数是否一致Example:class Base{public: virtual void Test(int number) const;};class Oba原创 2020-11-24 14:37:26 · 11771 阅读 · 0 评论 -
sprintf() 与 vsprintf()可变参数 用法比较
函数名:sprintf()功能:格式化传递给字符串#include <stdio.h>// 函数概要int sprintf( char* buf, const char* format, ... );参数:buf 用于存储格式化字符串的指针bufferformat 传递给buf的格式化字符串实例:#include <stdio.h>#include <stdlib.h>void Test::test(){ char bu原创 2020-11-19 15:14:09 · 527 阅读 · 0 评论 -
strcpy() strcat()的用法与区别
参考QNX官网https://www.qnx.com/developers/docs/7.0.0/index.html?textToSearch=qtd#com.qnx.doc.neutrino.lib_ref/topic/s/strcat.html#include <string.h>char* strcpy( char* dst, const char* src );char* strcat( char* dst, const char* src );strcpy()函.原创 2020-11-19 11:32:10 · 3788 阅读 · 0 评论 -
C++ 文件读写实例 open() read() write()
参考qnx官网https://www.qnx.com/developers/docs/7.0.0/index.html?textToSearch=qtd#com.qnx.doc.neutrino.lib_ref/topic/o/open.htmlNo.1 读操作读input.txt文本里的内容,调用open函数拿到fd然后用read读fd里的内容。void TATestReadMsg::startOpenfile(){ read_fd = 0; // read_fd ...原创 2020-11-18 11:28:19 · 3254 阅读 · 0 评论 -
【Note 】C++ if break, if continue, if return 的区别
老是会忘记if continue的作用,查了一下用法做个记录。1. if break 用来终止循环,例如#include <iostream>using namespace std;int main(){ for(int i=0;i<5;i++) { if(i==3) break; cout<<"a...原创 2018-11-22 17:02:52 · 2489 阅读 · 0 评论 -
C++枚举 sample
typedef enum ETM_MODE_ID_TAG{ ETM_MODE_INVISIBLE = 0, ETM_MODE_IMAGE, ETM_MODE_ILLUMINATION, ETM_MODE_CHIME, ETM_MODE_SOFTWARE, ETM_MODE_SPEED, ETM_MODE_FUEL, ETM_MOD...原创 2018-10-26 10:56:43 · 314 阅读 · 0 评论 -
C++类的实例
挺喜欢这个例子,Mark一下。Warning.h 头文件#ifndef _WARNING_H_#define _WARNING_H_#include <string>class Warning{public: // enum struct defaultprotected:private:public: // method Warnin...原创 2018-10-12 14:13:21 · 483 阅读 · 0 评论