#include <tr1/unordered_map>
#include <stdio.h>
#include <string>
int main() {
typedef std::tr1::unordered_map<std::string, int> HashMap;
HashMap mapNumber;
mapNumber["one"] = 1;
mapNumber["two"] = 2;
std::tr1::hash<st…… 阅读全文
tr1 的 bind 和 function
首先来看两个文章,function/bind的救赎(上),http://blog.csdn.net/myan/arti…,以boost::function和boost:bind取代虚函数,http://blog.csdn.net/solstice/…,前者讲背景和理论,后者讲实际操作和细节对比,读来酣畅淋漓,醍醐灌顶
这么好的东西,当然要想着怎么在现有的环境条件下用起来,没有 boost,…… 阅读全文
对有符号数的位移操作
来看代码
#include <stdio.h>
int main() {
int a = 0x80000000;
int b = a>>1;
int c = a>>2;
int d = a>>31;
int e = a>>32;
printf("%d\n%d\n%d\n%d\n%d\n", a, b, c, d, e);
return 0;
}
这个东西的输出是什么呢
vs2005 和 g…… 阅读全文
最长公共子序列
#include <stdio.h>
#include <string.h>
static const int MAX = 100;
char str1[MAX], str2[MAX], str3[MAX];
int c[MAX][MAX], f[MAX][MAX];
inline int max(int a, int b) {
return a>b ? a : b;
}
int dp(int i, int j) {
if (c[i][j] == -1) {
if (i==0 || j==0) {
…… 阅读全文
Jon Bently 一种快排的写法
看到知乎上有这个讨论,程序员能20分钟徒手写出一个没bug的快速排序吗?
void quicksort(int l, int u){
int i, m;
if(l >= u) return;
m = l;
for(i = l+1; i<= u; i++)
if(x[i] < x[l]) // buggy!
swap(++m, i);
swap(l, m);
quicksort(l, m-1);
…… 阅读全文
对引用取地址
今天突然想到,如果我们对一个引用取地址,那么会怎么样呢,例如这样
#include <stdio.h>
class CNumber {
public:
int n;
};
void printByPointer(CNumber* pNumber) {
printf("%d\n", pNumber->n);
}
void printByRef(CNumber& oNumber) {
printf("%d\n", oNumb…… 阅读全文
gcc 链接时动态库和静态库的优先选择
老话题了,不过还是记一笔
先看文件
ModuleA.cpp
int add(int a, int b)
{
return a + b;
}
ModuleB.cpp
int minus(int a, int b)
{
return a - b;
}
Main.cpp
#include <stdio.h>
int add(int, int);
int minus(int , int);
int main()
{
printf("%d\n", add(…… 阅读全文
tr1 memory 中的智能指针
tr1/memory 中带了一个 shared_ptr 的智能指针,也有配套的 weak_ptr,但是没有 scoped_ptr,不过起码可以不用等到 c++11 的编译器了
#include <tr1/memory>
#include <stdio.h>
class CTest {
public:
CTest() {
printf("CTest\n");
}
~CTes…… 阅读全文
Python inet_ntoa 小脚本
用的时候发现这东西居然还要自己写
#!/usr/bin/env python
import socket
import struct
import sys
def Int2Ip(ip):
return socket.inet_ntoa(struct.pack('!I', ip))
if __name__ == "__main__":
if len(sys.argv) > 1:
print Int2Ip(int(sys.argv[1]))
对于多行的文件…… 阅读全文
最简 Makefile
依赖于 Makefile 的自动推导,可以写出非常简化的 Makefile,假设目录下有有 Number.cpp 的单个 cpp 文件,内含 main 函数,那么,Makefile 可以这样写
all: Number
是的,就一行,all 作为默认 target,Number 作为依赖,自动推导出依赖源文件 Number.c*,如果是 cpp,得到编译器 g++,如果是 c,得打编译器 cc,然…… 阅读全文