
C++
yvhqbat
whuster
展开
-
第十六章 模板与泛型编程
模板是泛型编程的基础。16.1 定义模板16.1.1 函数模板 function template 函数模板 关键字 template + 模板参数列表 template parameter list 在模板定义中,模板参数列表不能为空 实例化模板参数 instantiate 编译器推断模板参数 模板的实例:编译器生成的版本 模板类原创 2015-11-13 17:02:49 · 380 阅读 · 0 评论 -
如何定位内存泄漏?
1. 什么是内存泄漏 内存泄漏是指堆内存的泄漏。堆内存是指程序从堆中分配的、大小任意的(内存块的大小可以在程序运行期决定)、使用完后必须显示释放的内存。应用程序一般使用malloc、realloc、new等函数从堆中分配到一块内存,使用完后,程序必须负责相应的调用free或delete释放该内存块。否则,这块内存就不能被再次使用,造成这块内存泄漏。2. 内存泄漏的检测C++程序缺乏相应的手段转载 2016-07-20 11:43:52 · 7387 阅读 · 0 评论 -
第一章 词法陷阱
=(赋值运算符)不同%G4��==(比较运算符) 二者经常互相误写。&(按位与运算符) 和 | (按位或运算符)不同于 &&(逻辑与运算符) 和 ||(逻辑或运算符) 程序员易因为其他语言的影响而犯错。词法分析中的“贪心法” 单字符符号:�Ƃ / 、*、= 多字符符号:如 /*、== 编译器将程序分解成符号的方法。 符号的中间不能嵌有空白(空格符、制表符、换行符)。整形常量 如果一个整原创 2016-01-10 17:23:25 · 372 阅读 · 0 评论 -
C++11:随机数库
#include <iostream>#include <string>#include <ctime>using namespace std;#include <random>int main(){ //C库的随机数 srand((unsigned)time(0)); //使用时间作为种子,初始化随机数 for (int i = 0; i < 10; i++){原创 2016-07-19 00:39:24 · 512 阅读 · 0 评论 -
C++ 常用泛型算法的使用
#include <algorithm>#include <numeric>#include <iterator>#include <vector>#include <iostream>#include <functional>using namespace std;//template<typename T>void printing(const vector<int> &vec){原创 2016-07-17 17:32:33 · 1109 阅读 · 0 评论 -
C++11:lambda表达式、bind函数、function类型
#include <iostream>#include <string>using namespace std;//lambda表达式:表示一个可调用单元,理解为一个未命名的内联函数。//[capture list] (parameter list) -> return type { function body }void lambda_test(){ //可以忽略参数列表和返回类原创 2016-07-13 15:49:09 · 788 阅读 · 0 评论 -
c++11:正则表达式(re)
//#include <boost/regex.hpp>//using namespace boost;#include <regex>#include <string>#include <iostream>using namespace std;void regex_test(){ string pattern("[^c]ei"); pattern = "[[:alph原创 2016-07-13 14:33:32 · 1135 阅读 · 0 评论 -
C++primer
1.带符号类型和无符号类型混合运算时,带符号数会自动转换成无符号数。 2.true和false 是布尔类型的字面值,nullptr是指针字面值。 C++程序最好使用nullptr,同时尽量避免使用NULL。#include <cstdlib>#define NULL 03.指定字面值的类型,如L'a' //宽字符型字面值,wchar_t4.定义于函数体内的内置类型的对象如果没有初始化,则其值原创 2016-05-18 21:38:01 · 448 阅读 · 0 评论 -
动态数组
1、一维数组 int *p1 = new int[10]; //一维数组 //todo delete[] p1;2、二维数组m*n int m = 10; int n = 5; int **p2 = new int*[m]; for (int i = 0; i < m; i++){ p2[i] = new int[n]; }原创 2016-04-04 11:00:10 · 459 阅读 · 0 评论 -
C/C++可变参数va_list
VA_LIST 是在C语言中解决变参问题的一组宏,可变参数即表示参数个数、类型可以变化。可变参数的每个参数并没有实际的名称与之相对应,用起来是很灵活。 可变参数是实现printf(),sprintf()等函数的关键之处,也可以用可变参数来对任意数量的数据进行求和,求平均值带来方便(不然就用数组或每种写个重载)。#include <stdarg.h> //头文件(必需)int AveInt(int转载 2016-05-07 19:17:56 · 2800 阅读 · 1 评论 -
回车和换行的区别
“回车”(carriage return)和“换行”(line feed)这两个概念区别Unix系统里,每行结尾只有“<换行>”,即“\n”; Windows系统里面,每行结尾是“<换行><回车>”,即“\n\r”; Mac系统里,每行结尾是“<回车>”。 一个直接后果是,Unix/Mac系统下的文件在Windows里打开的话,所有文字会变成一行; 而Windows里的文件在Unix/Mac原创 2016-01-03 20:53:56 · 744 阅读 · 0 评论 -
What and where are the stack and heap?
The stack is the memory set aside as scratch space for a thread of execution. When a function is called, a block is reserved on the top of the stack for local variables and some bookkeeping data. When翻译 2015-11-17 20:45:38 · 322 阅读 · 0 评论 -
C++ difference of keywords 'typename' and 'class' in templates
typename and class are interchangeable in the basic case of specifying a template:template class Foo { };andtemplate class Foo { };are equivalent.Having said that, there are specific cases where翻译 2015-11-13 10:06:45 · 669 阅读 · 0 评论 -
条款49 了解new-handler的行为
#include "stdafx.h"#include <cstring>#include <iostream>void OutOfMenory(){ std::cerr << "unable to satisfy request for memory /n" << std::endl; std::abort();}int _tmain(int argc, _TCHAR* ar原创 2015-11-03 19:19:10 · 417 阅读 · 0 评论