My C++ coding style

Every programmer has his/her own coding style. Time goes by, and my coding style is changing. There following is my note on c++ coding style. I will explain why I want to coding in that style. Why do I care about my coding style so much? Because I am a virgo. Always add a m_ prefix to the members and use this to refer to other member functions in the member function.

Postponing definitions instead of preponing

Prepone definitions? Wrong! Before C99, you must prepone all the variables’ definitions before any statements. But after C99 was released, you can put a variable’s definition in any place as long as before being used. I think this new feature is introduced by learning from C++. This makes programer write codes more convenient. But I used to insist the old style. I even teach my students that they should use the old style when I was a TA of the course Programming with C.

Ruby-getting started

Running ruby Interactive In the past, I always tested some ruby statements with irb. Now I can test a small ruby program in the following way. [monkey@itlodge octopress]$ ruby str = "abc" puts str Ctrl+d abc After hit ruby, it allows me to type as many code as possible.Finally, press Ctrl+d will end the input and it will evaluate the code. Programs When writing a script, we always use #!

Some basic Ruby

String and name When defining a function, the parentheses are not necessary.So this one def say_hello name "Hello " + name end puts say_hello "man" is the same as this. def say_hello(name) "Hello " + name end puts(say_hello("man")) However, when there are more than one parameters, it will be difficult to know which argument goes with which method invocation.So it’s recommended using parentheses in all but the simplest cases.

Some interview questions about array and string

Hash Table In C++, the namespacestd::tr1 contains lots of hash tables. We can play with it. {% include_code careerup-array/careerup-hash.cpp %} Questions 1.1. Implement an algorithm to determine if a string has all unique characters. What if you can not use additional data structures? I have come up with 2 methods. Iterate the string and compare each character to other character. This is the common way to solve this problem.

Some reviews about C plus plus

Declaration and Definition I have already known the difference between declaration and definion, but now I find that my understanding is not complete. Declaration just tells the compiler about the name and the type of something.It’s very common about the declaration of objects and functions. extern int a; int max(int a, int b); However, I never think about the declaration of class.Recently, I have to view some codes in a large project and I have found many of these declarations.

Understanding Encapsulation

Once I loved C very much and I thought it’s the best programming language in the world. I used to argue that C can be used to implement the object-oriented design with struct. I used to argue that private and public is useless in C++. However, now I know I was wrong. Why we need private It’s related to a very important concept in C++, that is, encapsulation. Encapsulation is not invisibility.

程序员面试宝典读书笔记

C/C++程序设计 浮点数的存储 有下面的代码: #include <stdio.h> int main(int argc, char *argv[]) { int a; float *b = (float *)&a; *b = 13.0f; printf("%d\n", a); printf("%#x\n", a); return 0; } 最后的输出会是多少呢? 答案是: 1095761920 0x41500000 涉及到IEE

程序员面试宝典读书笔记

C/C++程序设计 浮点数的存储 有下面的代码: #include <stdio.h> int main(int argc, char *argv[]) { int a; float *b = (float *)&a; *b = 13.0f; printf("%d\n", a); printf("%#x\n", a); return 0; } 最后的输出会是多少呢? 答案是: 1095761920 0x41500000 涉及到IEE

一个带颜色输出的echo

动机 为了找到工作,最近需要多写shell了,有时候我很想输出信息的时候有颜色,比如 成功的信息用绿色,错误的信息用红色,去查了一下echo,发