随笔分类 -  C/C++

摘要:copyAssigns the values of elements from a source range to a destination range, iterating through the source sequence of elements and assigning them new positions in a forward direction.template<class InputIterator, class OutputIterator> OutputIterator copy( InputIterator _First, InputIt... 阅读全文
posted @ 2013-03-07 14:48 freewater 阅读(614) 评论(0) 推荐(0) 编辑
摘要:mismatchCompares two ranges element by element either for equality or equivalent in a sense specified by a binary predicate and locates the first position where a difference occurs.template<class InputIterator1, class InputIterator2> pair<InputIterator1, InputIterator2> mismatch( InputIt 阅读全文
posted @ 2013-03-07 13:15 freewater 阅读(267) 评论(0) 推荐(0) 编辑
摘要:maxCompares two objects and returns the larger of the two, where the ordering criterion may be specified by a binary predicate.template<class Type> const Type& max( const Type& _Left, const Type& _Right );template<class Type, class Pr> const Type& max( const Type& _Le 阅读全文
posted @ 2013-03-07 12:34 freewater 阅读(3375) 评论(0) 推荐(0) 编辑
摘要:lexicographical_compareCompares element by element between two sequences to determine which is lesser of the two.(以字典排列方式进行比较)template<class InputIterator1, class InputIterator2> bool lexicographical_compare( InputIterator1 _First1, InputIterator1 _Last1, InputIterator2 _First2, ... 阅读全文
posted @ 2013-03-07 11:07 freewater 阅读(268) 评论(0) 推荐(0) 编辑
摘要:iter_swapExchanges two values referred to by a pair of specified iterators.template<class ForwardIterator1, class ForwardIterator2> void iter_swap( ForwardIterator1 _Left, ForwardIterator2 _Right );这个好像没有什么特别的,就是交换两个迭代器所指向的内容。swapThe first override exchanges the values of two objects. ... 阅读全文
posted @ 2013-03-07 10:59 freewater 阅读(616) 评论(0) 推荐(0) 编辑
摘要:fillAssigns the same new value to every element in a specified range.template<class ForwardIterator, class Type> void fill( ForwardIterator _First, ForwardIterator _Last, const Type& _Val );fill_nAssigns a new value to a specified number of elements in a range beginning with a p... 阅读全文
posted @ 2013-03-07 10:55 freewater 阅读(659) 评论(0) 推荐(0) 编辑
摘要:equalCompares two ranges element by element either for equality or equivalence in a sense specified by a binary predicate.template<class InputIterator1, class InputIterator2> bool equal( InputIterator1 _First1, InputIterator1 _Last1, InputIterator2 _First2 );template<class Input... 阅读全文
posted @ 2013-03-06 19:42 freewater 阅读(328) 评论(0) 推荐(0) 编辑
摘要:<numeric>:Defines container template functions that perform algorithms provided for numerical processing.accumulateComputes the sum of all the elements in a specified range including some initial value by computing successive partial sums or computes the result of successive partial results si 阅读全文
posted @ 2013-03-06 19:26 freewater 阅读(1251) 评论(0) 推荐(0) 编辑
摘要:getline is to read a line of characters from input stream.My naive implement as follows, 1 #include<stdio.h> 2 3 /* getline: get line into s, return length. 4 arguments: lim is the max length of s. 5 */ 6 int getline(char s[],int lim) 7 { 8 int c;//store character acquired from input stream. 9 阅读全文
posted @ 2013-02-05 12:16 freewater 阅读(604) 评论(0) 推荐(0) 编辑
摘要:简洁的atoi 1 #include<ctype.h> //isspace and isdigit 2 3 /* convert string to integer */ 4 int atoi(char s[]) 5 { 6 int i,n,sign; 7 for(i=0;isspace(s[i]);i++)//skip white space 8 ; 9 sign=(s[i]=='-')?-1:1;10 if(s[i]=='+'||s[i]=='-')//skip sign11 i++;12 for(n=0;isdig... 阅读全文
posted @ 2013-02-05 10:51 freewater 阅读(187) 评论(0) 推荐(0) 编辑
摘要:itoa是广泛应用的非标准C语言扩展函数。由于它不是标准C语言函数,所以不能在所有的编译器中使用。但是,大多数的编译器(如Windows上的)通常在<stdlib.h>头文件中包含这个函数。在<stdlib.h>中与之有相反功能的函数是atoi。功能:把一整数转换为字符串。那么如何实现一个itoa函数呢?这个好像难度一般,我就直接附上我的实现,对此不熟悉的人最好自己code一下。atoi version one 1 /* reverse the string named s */ 2 void reverse(char s[]) 3 { 4 for(int i=0,j= 阅读全文
posted @ 2013-02-04 19:40 freewater 阅读(473) 评论(0) 推荐(0) 编辑
摘要:有陷阱,要考虑溢出问题int comp_int(int var1, int var2)/* return value < 0: means var1 < var2; * = 0: means var1 = var2; * > 0: means var1 > var2;*/{ int bit_width, msb, rtnvar; bit_width = sizeof(int) << 3; msb = var2 >> (bit_width -1) - var1 >> (bit_width -1);... 阅读全文
posted @ 2012-10-10 09:47 freewater 阅读(573) 评论(2) 推荐(0) 编辑
摘要:本文来自http://blog.csdn.net/zxcred C++程序的复杂性很大一部分在于他的内存管理,没有C#那样的垃圾回收机制,内存管理对初学者来说很困难。经常会出现内存泄露的情况。那么我们写程序如何避免内存泄露呢?首先我们需要知道程序有没有内存泄露,然后定位到底是哪行代码出现内存泄露了,这样才能将其修复。本文描述了如何检测内存泄露。最主要的是纯C,C++的程序如何检测内存泄露。现在有很多专业的检测工具,比如比较有名的BoundsCheck, 但是这类工具也有他的缺点,我认为首先BoundsCheck是商业软件,呵呵。然后呢需要安装,使用起来不太方便。因为我们检测的时候不一定经常会启 阅读全文
posted @ 2012-10-10 09:46 freewater 阅读(252) 评论(0) 推荐(0) 编辑
摘要:断言Andrei Alexandrescu本部分“泛型<编程>”讨论断言(assertions),一个你兵器库内非常强大的工具。以assert为基础,我们建立一个更强大的工具,帮助你建立更好的程序。我们很快就能看到,断言不仅是简单的工具/宏/函数。这是一种生活方式,一道深深的鸿沟把程序员们分成两类:了解,和不了解断言的力量。Assert(cool);那么,断言到底包含了什么?为什么你要重视断言?你什么时候需要用到断言,并且同样重要的,什么时候你不想要用到断言。我的看法是断言(例如用标准assert宏表示)是一个最最简单强大的工具来保证程序的正确性。断言的威力通常被低估了,至少在我参 阅读全文
posted @ 2012-09-24 22:59 freewater 阅读(2487) 评论(0) 推荐(0) 编辑
摘要:题目如下:递归函数最终会结束,那么这个函数一定(不定项选择):1. 使用了局部变量 2. 有一个分支不调用自身3. 使用了全局变量或者使用了一个或多个参数这是一道简单的选择题,但包含的内容并不算简单,而不定项选择更加大了难度。我一眼看去,自然就选择了2和3。1显然不是,局部变量只在一次调用局部范围有效,出了这次调用的范围就无效了,它不能控制递归的结束。(这个选项是考查局部变量生命周期/有效范围的问题)需要注意的就是局部变量不是局部静态变量。对于2,很自然了,如果没有一个分支不调用自身,递归就不会结束了。(这是在考查递归的定义)对于3,这是最有迷惑性的,因为使用全局变量或使用一个或多个参数的确可 阅读全文
posted @ 2012-09-21 09:52 freewater 阅读(4304) 评论(0) 推荐(0) 编辑
摘要:一、预备知识—程序的内存分配一个由c/C++编译的程序占用的内存分为以下几个部分1、栈区(stack)—由编译器自动分配释放,存放函数的参数值,局部变量的值等。其操作方式类似于数据结构中的栈。2、堆区(heap)—一般由程序员分配释放,若程序员不释放,程序结束时可能由OS回收。注意它与数据结构中的堆是两回事,分配方式倒是类似于链表,呵呵。3、全局区(静态区)(static)—全局变量和静态变量的存储是放在一块的,初始化的全局变量和静态变量在一块区域,未初始化的全局变量和未初始化的静态变量在相邻的另一块区域。程序结束后由系统释放。4、文字常量区—常量字符串就是放在这里的。程序结束后由系统释放。5 阅读全文
posted @ 2012-09-18 14:02 freewater 阅读(264) 评论(0) 推荐(0) 编辑
摘要:本文主要讨论C++标准库中的顺序容器及相应的容器适配器,这些内容主要涉及顺序容器类型:vector、list、deque,顺序容器适配器类型:stack、queue、priority_queue。 标准库中的容器分为顺序容器和关联容器。顺序容器(sequential container)内的元素按其位置存储和访问,顾名思义,这些内部元素是顺序存放的;顺序容器内的元素排列次序与元素值无关,而是由元素添加到容器里的次序决定。而关联容器的元素按键(key)排序。 容器类共享部分公共接口。标准库定义的三种顺序容器类型:vector、list、deque(double-ended queue的缩写,发音 阅读全文
posted @ 2012-09-04 14:36 freewater 阅读(444) 评论(0) 推荐(0) 编辑
摘要:1 char* myitoa(int x,char* str){ 2 assert(str!=0); 3 4 int negPos=-1; 5 if(x<0){ 6 negPos=0; 7 str[negPos]='-'; 8 x*=-1; 9 } 10 11 int i,j; 12 i=j=negPos+1; 13 do{ 14 str[j++]=x%10+'0'; 15 x/=10; 16 }while(x!=0); 17 ... 阅读全文
posted @ 2012-08-13 20:07 freewater 编辑
摘要:C语言中一般使用#define来定义常量,而C++中推荐使用const。因为#define定义的常量只是定义了名字和值的映射,存储在符号表中,在预处理阶段进行名值替换,不会进行类型检查操作。而const会进行类型检查。c++中const默认是内部链接的,也就说,const仅在const被定义过的文件里才是可见的,而在链接中不能被其他编译单元看到。这是因为const默认也是放到符号表中的,不会为其分配内存空间。但也有例外:当使用extern和取const变量的地址时,会强制为其分配内存空间。e.g. extern const int size=10; 或 const int size=10; c 阅读全文
posted @ 2012-07-30 12:33 freewater 阅读(534) 评论(0) 推荐(0) 编辑
摘要:在变量定义前加extern表示声明一个变量但不定义它,例如:extern int a;extern 也可用于函数声明。例如:extern int func(int, int);但对于函数声明extern不是必须的。对变量和函数进行声明后就可以使用别人的函数了,但是大部分的库包含众多的函数和变量。要使用多个,自己声明起来既麻烦也容易出错,这时可以使用包含头文件的方法。头文件是一个包含某个库的外部声明函数和变量的文件,其中已经包含了函数和变量的声明信息。因而,用户只需使用预处理指令#include包含由创建库的程序员提供的头文件就可以了。static member function 的主要特性就是 阅读全文
posted @ 2012-07-26 21:38 freewater 阅读(214) 评论(0) 推荐(0) 编辑