Ray's playground

 

03 2011 档案

Item31: Minimize compilation dependencies between files.(Effective C++)
摘要:The general idea behind minimizing compilation dependencies is to depend on declarations instead of definitions. Two approaches based on this idea are Handle classes and Interface classes. Library header files should exist in full and declaration-only forms. This applies regardless of whether templ. 阅读全文

posted @ 2011-03-31 23:52 Ray Z 阅读(221) 评论(0) 推荐(0)

Item 30: Understand the ins and outs of inlining.(Effective C++)
摘要:Limit most inlining to small, frequently called functions. This facilitates debugging and binary upgradability, minimizes potential code bloat, and maximizes the chances of greater program speed. Don't declare function templates inline just because they appear in header files. 阅读全文

posted @ 2011-03-31 14:11 Ray Z 阅读(190) 评论(0) 推荐(0)

Item29: Strive for exception-safe code.(Effective C++)
摘要:Exception-safe functions leak no resources and allow no data structures to become corrupted, even when exceptions are thrown. Such functions offer the basic, strong, or nothrow guarantees. The strong guarantee can often be implemented via copy-and-swap, but the strong guarantee is not practical for. 阅读全文

posted @ 2011-03-30 23:33 Ray Z 阅读(303) 评论(0) 推荐(0)

Item 28: Avoid returning "handles" to object internals.(Effective C++)
摘要:Avoid returning handles (references, pointers, or iterators) to object internals. It increases encapsulation, helps const member functions act const, and minimizes the creation of dangling handles. 阅读全文

posted @ 2011-03-30 12:01 Ray Z 阅读(194) 评论(0) 推荐(0)

Item 27: Minimize casting.(Effective C++)
摘要:Avoid casts whenever practical, especially dynamic_casts in performance-sensitive code. If a design requires casting, try to develop a cast-free alternative. When casting is necessary, try to hide it inside a function. Clients can then call the function instead of putting casts in their own code. P. 阅读全文

posted @ 2011-03-30 11:36 Ray Z 阅读(192) 评论(0) 推荐(0)

Item 26: Postpone variable definitions as long as possible.(Effective C++)
摘要:Postpone variable definitions as long as possible. It increases program clarity and improves program efficiency. 阅读全文

posted @ 2011-03-29 10:58 Ray Z 阅读(277) 评论(0) 推荐(0)

Item 25: Consider support for a non-throwing swap(Effective C++)
摘要:Provide a swap member function when std::swap would be inefficient for your type. Make sure your swap doesn't throw exceptions. If you offer a member swap, also offer a non-member swap that calls the member. For classes (not templates), specialize std::swap, too. When calling swap, employ a usin 阅读全文

posted @ 2011-03-28 20:41 Ray Z 阅读(212) 评论(0) 推荐(0)

Item 24: Declare non-member functions when type conversions should apply to all parameters(Effective C++)
摘要:If you need type conversions on all parameters to a function (including the one pointed to by the this pointer), the function must be a non-member. 阅读全文

posted @ 2011-03-28 17:54 Ray Z 阅读(169) 评论(0) 推荐(0)

Project Euler Problem 23
摘要:A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.A numbernis called deficient if the sum of its proper divisors is less tha 阅读全文

posted @ 2011-03-28 15:26 Ray Z 阅读(264) 评论(0) 推荐(0)

Item 23: Prefer non-member non-friend functions to member functions(Effective C++)
摘要:Prefer non-member non-friend functions to member functions. Doing so increases encapsulation, packaging flexibility, and functional extensibility. 阅读全文

posted @ 2011-03-28 12:02 Ray Z 阅读(177) 评论(0) 推荐(0)

Manipulating Strings(Chapter 6 of Objective-C Phrasebook)
摘要:Foundation provides a class for assigningarbitrary attributes to strings. TheNSAttributedString class lets you attachdictionaries to ranges in attributed strings.These dictionaries can contain any arbitraryinformation that you want, including semanticmarkup. It’s important to remember that sending . 阅读全文

posted @ 2011-03-28 11:48 Ray Z 阅读(189) 评论(1) 推荐(0)

Item 22: Declare data members private(Effective C++)
摘要:Declare data members private. It gives clients syntactically uniform access to data, affords fine-grained access control, allows invariants to be enforced, and offers class authors implementation flexibility. protected is no more encapsulated than public. 阅读全文

posted @ 2011-03-27 20:56 Ray Z 阅读(187) 评论(0) 推荐(0)

Item 21: Don't try to return a reference when you must return an object(Effective C++)
摘要:Never return a pointer or reference to a local stack object, a reference to a heap-allocated object, or a pointer or reference to a local static object if there is a chance that more than one such object will be needed. 阅读全文

posted @ 2011-03-27 10:45 Ray Z 阅读(229) 评论(0) 推荐(0)

bitset
摘要:1#include<iostream>2#include<bitset>3usingnamespacestd;45intmain()6{7bitset<16>a(0xff01);8cout<<"a:"<<a<<endl;910bitset<32>b("100010");11cout<<"b:"<<b<<endl;12cout<<"countofb:"<<b.count()& 阅读全文

posted @ 2011-03-24 22:03 Ray Z 阅读(159) 评论(0) 推荐(0)

Item 20: Prefer pass-by-reference-to-const to pass-by-value(Effective C++)
摘要:Prefer pass-by-reference-to-const over pass-by-value. It's typically more efficient and it avoids the slicing problem. The rule doesn't apply to built-in types and STL iterator and function object types. For them, pass-by-value is usually appropriate. 阅读全文

posted @ 2011-03-24 21:35 Ray Z 阅读(177) 评论(0) 推荐(0)

Item 19: Treat class design as type design(Effective C++)
摘要:Class design is type design. Before defining a new type, be sure to consider all the issues discussed in this Item. 阅读全文

posted @ 2011-03-24 20:54 Ray Z 阅读(177) 评论(0) 推荐(0)

Item 18: Make interfaces easy to use correctly and hard to use incorrectly(Effective C++)
摘要:Good interfaces are easy to use correctly and hard to use incorrectly. Your should strive for these characteristics in all your interfaces. Ways to facilitate correct use include consistency in interfaces and behavioral compatibility with built-in types. Ways to prevent errors include creating new . 阅读全文

posted @ 2011-03-24 18:09 Ray Z 阅读(202) 评论(0) 推荐(0)

Item 17: Store newed objects in smart pointers in standalone statements.(Effective C++)
摘要:Store newed objects in smart pointers in standalone statements. Failure to do this can lead to subtle resource leaks when exceptions are thrown. 阅读全文

posted @ 2011-03-23 21:52 Ray Z 阅读(213) 评论(0) 推荐(0)

Item 16: Use the same form in corresponding uses of new and delete.(Effective C++)
摘要:If you use [] in a new expression, you must use [] in the corresponding delete expression. If you don't use [] in a new expression, you mustn't use [] in the corresponding delete expression. 阅读全文

posted @ 2011-03-23 21:29 Ray Z 阅读(224) 评论(0) 推荐(0)

Item 15: Provide access to raw resources in resource-managing classes.(Effective C++)
摘要:APIs often require access to raw resources, so each RAII class should offer a way to get at the resource it manages. Access may be via explicit conversion or implicit conversion. In general, explicit conversion is safer, but implicit conversion is more convenient for clients.1#include<iostream> 阅读全文

posted @ 2011-03-23 21:06 Ray Z 阅读(233) 评论(0) 推荐(0)

Web Service(Chapter 28 of Cocoa Programming for Mac OS X)
摘要:1#import"AppController.h"2#defineAWS_ID@"1CKE6MZ6S27EFQ458402"34@implementationAppController56-(id)init7{8self=[superinit];9if(self){10//Initializationcodehere.11}1213returnself;14}1516-(IBAction)fetchBooks:(id)sender17{18[progressstartAnimation:nil];1920NSString*input=[searchFie 阅读全文

posted @ 2011-03-22 15:06 Ray Z 阅读(336) 评论(0) 推荐(0)

Project Euler Problem 24
摘要:A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:012 021 102 120 201 210 阅读全文

posted @ 2011-03-22 11:56 Ray Z 阅读(220) 评论(0) 推荐(0)

Project Euler Problem 28
摘要:Starting with the number 1 and moving to the right in a clockwise direction a 5 by 5 spiral is formed as follows:2122 23 242520 78 91019 6 12 1118 54 3121716 15 1413It can be verified that the sum of the numbers on the diagonals is 101.What is the sum of the numbers on the diagonals in a 1001 by 100 阅读全文

posted @ 2011-03-22 10:03 Ray Z 阅读(215) 评论(0) 推荐(0)

Project Euler Problem 25
摘要:The Fibonacci sequence is defined by the recurrence relation:Fn= Fn1+ Fn2, where F1= 1 and F2= 1.Hence the first 12 terms will be:F1= 1F2= 1F3= 2F4= 3F5= 5F6= 8F7= 13F8= 21F9= 34F10= 55F11= 89F12= 144The 12th term, F12, is the first term to contain three digits.What is the first term in the Fibonacc 阅读全文

posted @ 2011-03-21 21:45 Ray Z 阅读(250) 评论(0) 推荐(0)

C++ sizeof
摘要:1#include<iostream>2usingnamespacestd;34#definemy_sizeof(type)(char*)(&type+1)-(char*)(&type)56unionu7{8doublea;9intb;10};1112unionu213{14chara[13];15intb;16};1718unionu319{20chara[13];21charb;22};2324structs425{26inti:8;27intj:4;28inta:3;29doubleb;30};3132intmain()33{34doublex;35strin 阅读全文

posted @ 2011-03-19 11:21 Ray Z 阅读(255) 评论(0) 推荐(0)

boost::addressof
摘要:#include<iostream>#include<boost/utility.hpp>usingnamespacestd;classObject{public:inti;Object():i(0x11223344){}intoperator&(){return0;}};intmain(){Objectobj;cout<<&obj<<endl;Object*p=boost::addressof(obj);Object*p1=std::addressof(obj);cout<<p<<endl;cout< 阅读全文

posted @ 2011-03-14 22:53 Ray Z 阅读(388) 评论(0) 推荐(0)

Project Euler Problem 22
摘要:Usingnames.txt(right click and 'Save Link/Target As...'), a 46K text file containing over five-thousand first names, begin by sorting it into alphabetical order. Then working out the alphabetical value for each name, multiply this value by its alphabetical position in the list to obtain a na 阅读全文

posted @ 2011-03-14 14:10 Ray Z 阅读(272) 评论(0) 推荐(0)

Printing(Chapter 27 of Cocoa Programming for Mac OS X)
摘要:1#import"PeopleView.h"2#import"Person.h"34@implementationPeopleView56-(id)initWithPeople:(NSArray*)persons7{8[superinitWithFrame:NSMakeRect(0,0,700,700)];9people=[personscopy];10attributes=[[NSMutableDictionaryalloc]init];11NSFont*font=[NSFontfontWithName:@"Monaco"size: 阅读全文

posted @ 2011-03-14 11:43 Ray Z 阅读(279) 评论(0) 推荐(0)

Numbers(Chapter 5 of Objective-C Phrasebook)
摘要:1#import<Foundation/Foundation.h>23intmain(intargc,constchar*argv[])4{56NSAutoreleasePool*pool=[[NSAutoreleasePoolalloc]init];78NSString*test=@"Shanghai;40.00;RayZhang;";9NSScanner*scanner=[NSScannerscannerWithString:test];1011NSString*location;12floatnumber;1314NSCharacterSet*charac 阅读全文

posted @ 2011-03-14 10:39 Ray Z 阅读(209) 评论(0) 推荐(0)

noncopyable implementation in C++
摘要:1#include<iostream>2usingnamespacestd;34classnoncopyable5{6protected:7noncopyable(){}8~noncopyable(){}9private:10noncopyable(constnoncopyable&);11constnoncopyable&operator=(constnoncopyable&);12};1314classmyclass:privatenoncopyable15{16public:17myclass()18{19cout<<"mycla 阅读全文

posted @ 2011-03-13 16:20 Ray Z 阅读(272) 评论(0) 推荐(0)

Project Euler Problem 19
摘要:You are given the following information, but you may prefer to do some research for yourself.1 Jan 1900 was a Monday.Thirty days has September,April, June and November.All the rest have thirty-one,Saving February alone,Which has twenty-eight, rain or shine.And on leap years, twenty-nine.A leap year 阅读全文

posted @ 2011-03-13 12:07 Ray Z 阅读(381) 评论(0) 推荐(0)

Metaphors for a Richer Understanding of Software Development (Chapter 2 of Code Complete)
摘要:Metaphors are heuristics, not algorithms. As such, they tend to be a little sloppy. Metaphors help you understand the software-development process by relating it to other activities you already know about. Some metaphors are better than others. Treating software construction as similar to building . 阅读全文

posted @ 2011-03-13 11:12 Ray Z 阅读(212) 评论(0) 推荐(0)

Project Euler Problem 21
摘要:Let d(n) be defined as the sum of proper divisors ofn(numbers less thannwhich divide evenly inton).If d(a) =band d(b) =a, whereab, thenaandbare an amicable pair and each ofaandbare called amicable numbers.For example, the proper divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55 and 110; therefo 阅读全文

posted @ 2011-03-11 22:40 Ray Z 阅读(211) 评论(0) 推荐(0)

Welcome to Software Construction(Chapter 1 of Code Complete)
摘要:Developing computer software can be a complicated process, and in the last 25 years, researchers have identified numerous distinct activities that go into software development. They include Problem definition Requirements development Construction planning Software architecture, or high-level design 阅读全文

posted @ 2011-03-11 22:03 Ray Z 阅读(153) 评论(0) 推荐(0)

Project Euler Problem 15
摘要:Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner.How many routes are there through a 2020 grid?1#include<iostream>2usingnamespacestd;34#definenum415longlongPascalTriangle[num][num];67voidCalcPascalTriangle()8{9PascalTriangle[0][ 阅读全文

posted @ 2011-03-11 17:41 Ray Z 阅读(209) 评论(0) 推荐(0)

Common Objective-C Patterns(Chapter 4 of Objective-C Phrasebook)
摘要:1#import"Singleton.h"234@implementationSingleton5staticSingleton*instance;67-(id)init8{9self=[superinit];10if(self){11//Initializationcodehere.12}1314returnself;15}1617+(id)alloc18{19@synchronized([Singletonclass])20{21instance=[superalloc];22returninstance;23}24returnnil;25}2627+(Singleto 阅读全文

posted @ 2011-03-11 16:25 Ray Z 阅读(230) 评论(0) 推荐(1)

Creating NSFormatters(Chapter 26 of Cocoa Programming for Mac OS X)
摘要:1#import"ColorFormatter.h"23@interfaceColorFormatter()45-(NSString*)firstColorKeyForPartialString:(NSString*)string;67@end89@implementationColorFormatter1011-(id)init12{13[superinit];14colorList=[[NSColorListcolorListNamed:@"Apple"]retain];15returnself;16}1718-(void)dealloc19{20[ 阅读全文

posted @ 2011-03-11 09:19 Ray Z 阅读(231) 评论(0) 推荐(0)

Project Euler Problem 12
摘要:The sequence of triangle numbers is generated by adding the natural numbers. So the 7thtriangle number would be 1 + 2 + 3 + 4 + 5 + 6 + 7 = 28. The first ten terms would be:1, 3, 6, 10, 15, 21, 28, 36, 45, 55, ...Let us list the factors of the first seven triangle numbers:1: 13: 1,36: 1,2,3,610: 1,2 阅读全文

posted @ 2011-03-10 21:19 Ray Z 阅读(214) 评论(0) 推荐(0)

Project Euler Problem 3
摘要:The prime factors of 13195 are 5, 7, 13 and 29.What is the largest prime factor of the number 600851475143 ?1#include<iostream>2usingnamespacestd;34longlongGetFirstFactor(longlongnum)5{6longlongroot=sqrt((longdouble)num);7for(longlongi=2;i<root;i++)8{9if(num%i==0)10{11returni;12}13}1415retu 阅读全文

posted @ 2011-03-10 20:35 Ray Z 阅读(178) 评论(0) 推荐(0)

Sheets(Chapter 25 of Cocoa Programming for Mac OS X)
摘要:1//2//AppController.m3//TypingTutor4//5//Createdbyb1mobileon3/3/11.6//Copyright2011__MyCompanyName__.Allrightsreserved.7//89#import"AppController.h"10#import"BigLetterView.h"1112#defineMAX_COUNT(100)13//#defineCOUNT_STEP(5)1415@implementationAppController1617-(id)init18{19[superi 阅读全文

posted @ 2011-03-08 17:36 Ray Z 阅读(200) 评论(0) 推荐(0)

NSTimer(Chapter 24 of Cocoa Programming for Mac OS X)
摘要:1#import"AppController.h"2#import"BigLetterView.h"34#defineMAX_COUNT(100)5#defineCOUNT_STEP(5)67@implementationAppController89-(id)init10{11[superinit];1213letters=[[NSArrayalloc]initWithObjects:@"a",@"s",@"d",@"f",@"j",@"k&q 阅读全文

posted @ 2011-03-05 13:20 Ray Z 阅读(281) 评论(0) 推荐(0)

Drag-and-Drop(Chapter 23 of Cocoa Programming for Mac OS X)
摘要:#import"BigLetterView.h"@implementationBigLetterView-(NSDragOperation)draggingSourceOperationMaskForLocal:(BOOL)isLocal{returnNSDragOperationCopy|NSDragOperationDelete;}-(NSDragOperation)draggingUpdated:(id<NSDraggingInfo>)sender{NSDragOperationop=[senderdraggingSourceOperationMask]; 阅读全文

posted @ 2011-03-03 10:43 Ray Z 阅读(515) 评论(0) 推荐(1)

Pasteboards and Nil-Targeted Actions(Chapter 21 of Cocoa Programming for Mac OS X)
摘要:1-(void)writeToPasteboard:(NSPasteboard*)pb2{3[pbdeclareTypes:[NSArrayarrayWithObject:NSStringPboardType]owner:self];4[pbsetString:stringforType:NSStringPboardType];5}67-(BOOL)readFromPasteboard:(NSPasteboard*)pb8{9NSArray*types=[pbtypes];10if([typescontainsObject:NSStringPboardType])11{12NSString*v 阅读全文

posted @ 2011-03-02 09:41 Ray Z 阅读(235) 评论(0) 推荐(1)

Drawing Text With Attributes(Chapter 20 of Cocoa Programming for Mac OS X)
摘要:1#import"BigLetterView.h"234@implementationBigLetterView56-(IBAction)savePDF:(id)sender7{8NSSavePanel*panel=[NSSavePanelsavePanel];9[panelsetRequiredFileType:@"pdf"];10[panelbeginSheetForDirectory:nilfile:nilmodalForWindow:[selfwindow]modalDelegate:selfdidEndSelector:@selector(di 阅读全文

posted @ 2011-03-01 14:52 Ray Z 阅读(694) 评论(0) 推荐(0)

导航