c/c++常用技巧(更新中)

1. double -> int   //directx9c以上失效,因为其会改变单浮点精度

ftol
 1inline long ftol(double x)
 2{
 3    #ifdef __WINDOWS__
 4  static const double magic = 6755399441055744.0// 2^51 + 2^52
 5
 6  double tmp = x;
 7  tmp += (x > 0? -0.499999999999 : +0.499999999999;
 8  tmp += magic;
 9  return *(long*)&tmp;
10#else
11  return (long)x;
12#endif
13}



2. c双向链表操作

list
 1struct list_head {   
 2    struct list_head *next, *prev;   
 3}
;   
 4  
 5  
 6#define LIST_HEAD_INIT(name) { &(name), &(name) }   
 7  
 8#define LIST_HEAD(name) \   
 9    struct list_head name = LIST_HEAD_INIT(name)   
10  
11#define INIT_LIST_HEAD(ptr) do { \   
12    (ptr)->next = (ptr); (ptr)->prev = (ptr); \   
13while (0)   
14  
15/**//*  
16 * Insert a new entry between two known consecutive entries.  
17 *  
18 * This is only for internal list manipulation where we know  
19 * the prev/next entries already!  
20 */
   
21static inline void __list_add(struct list_head *new,   
22                  struct list_head *prev,   
23                  struct list_head *next)   
24{   
25    next->prev = new;   
26    new->next = next;   
27    new->prev = prev;   
28    prev->next = new;   
29}
   
30  
31/**//**  
32 * list_add - add a new entry  
33 * @new: new entry to be added  
34 * @head: list head to add it after  
35 *  
36 * Insert a new entry after the specified head.  
37 * This is good for implementing stacks.  
38 */
   
39static inline void list_add(struct list_head *newstruct list_head *head)   
40{   
41    __list_add(new, head, head->next);   
42}
   
43  
44..   
45  
46/**//**  
47 * list_entry - get the struct for this entry  
48 * @ptr:    the &struct list_head pointer.  
49 * @type:    the type of the struct this is embedded in.  
50 * @member:    the name of the list_struct within the struct.  
51 */
   
52#define list_entry(ptr, type, member) \   
53    container_of(ptr, type, member)   
54  
55/**//**  
56 * list_for_each    -    iterate over a list  
57 * @pos:    the &struct list_head to use as a loop counter.  
58 * @head:    the head for your list.  
59 */
   
60#define list_for_each(pos, head) \   
61    for (pos = (head)->next; prefetch(pos->next), pos != (head); \   
62            pos = pos->next)   

container_of 宏
1 /**/ /*  GCC   */
2 #define  container_of(ptr, type, member) ({            \   
3          const   typeof ( ((type  * ) 0 ) -> member )  * __mptr  =  (ptr);    \   
4         (type  * )( ( char   * )__mptr  -  offsetof(type,member) );})   
5
6 // or
7
8 #define  container_of(ptr, type, member) (            \   
9         (type  * )( ( char   * )ptr  -  offsetof(type,member) ) )  

how to use
 1 struct  my_data  {   
 2    int x;   
 3    int y;   
 4    struct list_head list;   
 5}
   
 6   
 7 /**/ /* 链表头 */    
 8 LIST_HEAD(my_listhead);   
 9   
10 void  my_function()   
11 {   
12       
13    /**//* 节点对象 */   
14    struct my_data *node_1 = (struct my_data *) malloc(sizeof(struct my_data));   
15    struct my_data *node_2 = (struct my_data *) malloc(sizeof(struct my_data));   
16       
17    /**//* 加入链表 */   
18    list_add (node_1->list, &my_listhead);   
19    list_add (node_2->list, &my_listhead);   
20       
21    /**//* 遍历链表 */   
22    struct my_data * node;   
23    struct list_head *pos;   
24    list_for_each (pos, &my_listhead) {   
25       node = list_entry (pos, struct my_data, list);   
26          
27    }
   

转载于:https://www.cnblogs.com/yyliuliang/archive/2008/06/13/1219070.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: C/C++算法常用手册是程序员日常工作不可或缺的工具书之一。该手册主要收录了程序员在开发过程常用的算法,以及相应的代码实现。该手册涵盖了诸如数据结构、排序、查找、递归、贪心、动态规划、字符串等算法,帮助程序员快速掌握这些算法的基本原理和实现方式。简单地说,该手册将算法的核心原理和实现细节集在了一起,兼顾了易懂性和实用性。 随着程序员需求的不断增加,该手册逐渐扩充了更多的算法类型。同时,该手册还根据算法的不同应用场景进行分类,方便程序员快速查找和使用。例如,程序员可以通过该手册快速了解不同数据结构的原理和实现方法,了解常见算法的时间复杂度和空间复杂度,还可以查找常见的实际问题的算法实现方式。 总的来说,C/C++算法常用手册是程序员必备的工具之一,帮助程序员提高算法的实现能力和解决实际问题的能力,提高程序的效率和质量。 ### 回答2: C/C++常用算法手册是一本介绍计算机算法的参考手册,主要面向C/C++语言程序员。该手册总结了各种常用的算法,包括排序、查找、图论、字符串等。通过该手册的学习,可以让程序员更好地掌握C/C++编程的技巧和方法。 该手册介绍了排序算法,包括冒泡排序、插入排序、选择排序、快速排序、归并排序等。对于不同的排序算法,手册详细介绍了它们的思路和实现方法,同时也对它们的时间复杂度和效率进行了分析和比较。 在查找方面,手册介绍了常用的顺序查找和二分查找算法,它们可以帮助程序员快速地定位和查找数据。 在图论和字符串方面,手册介绍了很多有用的算法,如最短路径算法、最小生成树算法、字符串匹配算法等。这些算法可以帮助程序员更好地解决实际问题。 总之,C/C++常用算法手册是一本非常实用和有价值的参考书,它可以让程序员掌握更多的C/C++算法技巧,提高程序员的编程能力和开发效率。 ### 回答3: C/C++ 常用算法手册是一本总结了 C/C++ 编程语言常用的算法、数据结构、设计模式等知识的参考书籍。 相对于其他语言,C 和 C++ 语言有着更高的执行效率和更多的编程自由度,也因此被广泛应用于开发高性能、底层的软件程序。在这样的应用场景下,对算法和数据结构的掌握显得尤为重要。 C/C++ 常用算法手册涵盖了各种基础的算法和数据结构,比如排序、查找、链表、树等。同时,它也介绍了一些常用的高级算法,比如动态规划、贪心算法和回溯算法。 此外,该手册还详细说明了面向对象编程领域常用的设计模式和其实现方式,例如工厂模式、装饰器模式等。 阅读 C/C++ 常用算法手册不但能够让读者掌握常用算法的实现方法,更能提高编程思维和技巧。另外,在实际应用,编写高效的程序不仅需要算法的巧妙运用,更需要细致、严谨的代码风格和设计思路。此时,该手册丰富的示例代码和编码规范性的讲解也能为读者提供很大的帮助。 总之,C/C++ 常用算法手册是一本既实用又深入的参考书,适合广大 C/C++ 开发者和算法学习者阅读。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值