摘要: 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的。自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将要实现19个功能。到目前我只写了一半,先传上来,大家有兴趣的可以帮忙指正,谢谢在vs2010上面编译运行无错误。每天都会把我写的新代码添加到这个里面。直到此链表完成。#include "stdafx.h"#include "stdio.h"#include <stdlib.h>#include "string.h"typedef int elemType ;/***阅读全文
posted @ 2011-08-20 21:37 Lee.Kevin 阅读(2456) 评论(5) 编辑

转自国外的一个网站,非常好的东西。国外的网址图片显示不全且不容易查看。加密问题。现在自己整理发上来以帮助网友和自己。

 

Before we go over with this tutorial, I would like to thank everyone for reading my posts, for asking questions and for giving suggestions. Because of that, I will continue writing tutorials that might help other developers get rid of headaches and frustrations.

This tutorial is a follow up tutorial from my previous post. Someone find it simple yet helpful and ohers have questions if it is possible to mark/stylize and get those strings inside UIWebView that are selected or highlighted. So I created this tutorial to support that question.

Let's get started!

Since we are still manipulating UIWebView, we need to gain access to its contents. In order to do this, we have to use Javascripts.

With Javascript, we can access UIWebViews contents like normal browsers do.

In this tutorial, we will use UIWebViews method (stringByEvaluatingJavaScriptFromString) to access javascript files and execute its functions.

Download the javascript and html file here. After downloading the zip file, extract the file and you should have two files (HighlightedString.js and index.html).

What are these files?

The html file (index.html) handles the UIWebView content.

That javascript file (HighlightedString.js) handles all the job of getting or marking highlighted strings inside UIWebView.

Inside that javascript file, we have variable to store the selected text.

var selectedText = "";

We also have a function to get the highlighted strings.

function getHighlightedString() {
    var text        = window.getSelection();
    selectedText    = text.anchorNode.textContent.substr(text.anchorOffset, text.focusOffset - text.anchorOffset);

}

This function above will access the window and perform the getSelection() operation to access of UIWebViews contents.

Then, we parse UIWebView's contents using the anchorOffset and focusOffset.

  • anchorOffset() - returns the first index position of the highlighted strings. For example if my UIWebView has content "My name is Zaldy", and you highlighted Zaldy. You're anchorOffset is 11 which is the character "Z". Because it counts all characters from the beginning of the content as index 0.
  • focusOffset() - returns the last index position of the highlighted strings. With the latter example, our focusOffset is 15 which refers to the last character of the highlighted string,  the character "y".

Now, in order to acquire the highlighted strings, we parse the UIWebView contents and scan the content using the operation text.anchorNode.textContent.substr(int startingIndex, int length). This operation takes two paramters, the first one is the starting position of the character to parse, the second one is the length of the string to end the parsing.

So if our UIWebView has content "My name is Zaldy" and we have anchorOffset() = 11 with focusOffset() = 15. Our string length would be 15 - 11 = 4. Remember that 4 means we have 0,1,2,3,4 characters, a total of 5characters. The parser now scan's the content from index 11 with string length 4, so it will end scanning to character "y".

Hope that sounds clear to you guys.

Another function is to stylize/mark any highlighted string inside UIWebView content.

function stylizeHighlightedString() {
    
    var range               = window.getSelection().getRangeAt(0);
    var selectionContents   = range.extractContents();
    var span                = document.createElement("span");
    
    span.appendChild(selectionContents);
    
    span.setAttribute("class","uiWebviewHighlight");
    span.style.backgroundColor  = "black";
    span.style.color            = "white";
    
    range.insertNode(span);
}

That function above captures the range of our selection and put a <span> element before the first character of the highlighted string and put the </span> after the last character of the highlighted string. After that, we add a class attribute named "uiWebviewHighlight" with defined style backgroundColor to black and font color style to white. Then insert the changes to the highlighted strings and update the UIWebView content.

Note: You can always change the colors of marker's background and text.

 We also have a function to remove all highlights. This function is also used on my previous post to mark/highlight a searched string.
// helper function, recursively removes the highlights in elements and their childs
function uiWebview_RemoveAllHighlightsForElement(element) {
    if (element) {
        if (element.nodeType == 1) {
            if (element.getAttribute("class") == "uiWebviewHighlight") {
                var text = element.removeChild(element.firstChild);
                element.parentNode.insertBefore(text,element);
                element.parentNode.removeChild(element);
                return true;
            } else {
                var normalize = false;
                for (var i=element.childNodes.length-1; i>=0; i--) {
                    if (uiWebview_RemoveAllHighlightsForElement(element.childNodes[i])) {
                        normalize = true;
                    }
                }
                if (normalize) {
                    element.normalize();
                }
            }
        }
    }
    return false;
}

That function above recursively removes all element occurence that has class attribute named "uiWebviewHighlight". This will remove all marks/highlights defined to that class attribute.

.Let's do this!

This tutorial project is using an iOS5 SDK with Storyboards and ARC.

1. Create an XCode project named (WebViewHighlight) as a single view applciation. Check the Use Of Storyboard and the Use of Automatic Reference Counting.

2. Add the javascript and html file to your XCode.

3. Note: once you have added a javascript file to your XCode. XCode will displays a warning:

warning: no rule to process file '$(PROJECT_DIR)/.../HighlightedString.js' of type sourcecode.javascript for architecture i386

This happens because it compiles a javascript file and will find it weird. So that our XCode will not compile our javascript file, go to your TARGET and select the BUILD PHASES tab and expand the COMPILE SOURCES then  remove the HighlightedString.js by selecting it and click the minus (-) button.

Add HighlightedString.js to the COPY BUNDLE RESOURCES by clicking the plus (+) button and select the jabascript fileSee below screen-shot.

Remove js file from Compile Sources.

 

 

Add js file to the Copy Bundle Resources.

 

This will add the javascript file to your project's bundle resources without compiling it.

 

4. Update your ViewController header file (ViewController.h) with the code below.

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

@property(nonatomic, retain) IBOutlet UIWebView *webView;
- (IBAction)removeAllHighlights;
- (IBAction)markHighlightedString:(id)sender;
- (IBAction)getHighlightedString:(id)sender;
@end

We have an IBOutlet UIWebView variable and functions to mark, get and remove highlighted strings.

5. Update your @implementation file (ViewController.m).

Add these below @implementation.

{
    UIWebView *_webView;
}

@synthesize webView = _webView;

Add this method.

- (IBAction)markHighlightedString:(id)sender {
    
    // The JS File   
    NSString *filePath  = [[NSBundle mainBundle] pathForResource:@"HighlightedString" ofType:@"js" inDirectory:@""];
    NSData *fileData    = [NSData dataWithContentsOfFile:filePath];
    NSString *jsString  = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
    [_webView stringByEvaluatingJavaScriptFromString:jsString];
    
    // The JS Function
    NSString *startSearch   = [NSString stringWithFormat:@"stylizeHighlightedString()"];
    [_webView stringByEvaluatingJavaScriptFromString:startSearch];
    
}

Above code will call the javascript file and execute the function to mark any highlighted strings inside your webview content.

Add this method.

- (IBAction)getHighlightedString:(id)sender {
    
    // The JS File   
    NSString *filePath  = [[NSBundle mainBundle] pathForResource:@"HighlightedString" ofType:@"js" inDirectory:@""];
    NSData *fileData    = [NSData dataWithContentsOfFile:filePath];
    NSString *jsString  = [[NSMutableString alloc] initWithData:fileData encoding:NSUTF8StringEncoding];
    [_webView stringByEvaluatingJavaScriptFromString:jsString];
    
    // The JS Function
    NSString *startSearch   = [NSString stringWithFormat:@"getHighlightedString()"];
    [_webView stringByEvaluatingJavaScriptFromString:startSearch];
    
    NSString *selectedText   = [NSString stringWithFormat:@"selectedText"];
    NSString * highlightedString = [_webView stringByEvaluatingJavaScriptFromString:selectedText];
    
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Highlighted String" 
                                                    message:highlightedString
                                                   delegate:nil 
                                          cancelButtonTitle:@"Oh Yeah" 
                                          otherButtonTitles:nil];
    [alert show];
    //[alert release]; // not required anymore because of ARC
}

Above code will call the javascript file and execute the function to get any highlighted strings inside webview content and push an alert view.

Add this method.

- (IBAction)removeAllHighlights
{
    // calls the javascript function to remove html highlights
    [_webView stringByEvaluatingJavaScriptFromString:@"uiWebview_RemoveAllHighlights()"];
}

Above code will call the javascript function to remove string highlights.

Update your - (void)viewDidUnload with this code:

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    _webView = nil;
}

Update your - (void)viewDidLoad with this code:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    // Load index.html to our webview
    NSString *urlAddress        = [[NSBundle mainBundle] pathForResource:@"index" 
                                                                  ofType:@"html"]; //you can also use PDF files
    NSURL *url                  = [NSURL fileURLWithPath:urlAddress];
    NSURLRequest *requestObj    = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:requestObj];
    
    // Menu Controller
    UIMenuController *menuController = [UIMenuController sharedMenuController];
    UIMenuItem *getHighlightString = [[UIMenuItem alloc] initWithTitle: @"Get String" action: @selector(getHighlightString:)];
    UIMenuItem *markHighlightedString = [[UIMenuItem alloc] initWithTitle: @"Mark String" action: @selector(markHighlightedString:)];
    [menuController setMenuItems: [NSArray arrayWithObjects:getHighlightString, markHighlightedString, nil]];

}

Above code will load the HTML file index.html file to our webview by the time our view is loaded. There is a UIMenuController object added there as well, which will add two popup menu options when you highlight a string inside UIWebView with targets pointing to our IBAction methods. See screenshot below.

 

Add this method.

 

- (BOOL) canPerformAction:(SEL)action withSender:(id)sender
{
    
    if (action == @selector(getHighlightString:)) {
        return YES;
    } else if (action == @selector(markHighlightedString:)) {
        return YES;
    }
    
    return NO;
}

Above code will disable other popup menu items and show our newly added popup menu items.

6. Now, add the UIWebView and UIButton components to your storyboard file (MainStoryboard.storyboard). Then link UIWebView outlet and UIButton actions . See screen-shot below.

link UIWebView outlet reference:

 

link UIButton action events (Do this also with the rest of the buttons Get Highlighted String and Remove Highlights to action events getHighlightedString: and removeAllHighlights)

 

:Compile and Run!

Go ahead, compile and run. You should see something like this:

Marking a highlighted string.

 

Get the highlighted string.

 

The Code Please!

 

You can download the full source code here.

 原帖地址:https://zaldzbugz.posterous.com/how-to-mark-or-get-the-highlighted-string-ins

posted @ 2012-04-16 15:41 Lee.Kevin 阅读(86) 评论(0) 编辑

创建一个controller名为YKWebViewDemoViewController

.h文件代码如下

#import <UIKit/UIKit.h>

@interface YKWebViewDemoViewController : UIViewController<UIWebViewDelegate>{
    
}
@property (strong, nonatomic) UIWebView *webView;

- (void)loadWebPageWithString:(NSString*)urlString;
- (void)customAction1:(id)sender;
- (void)customAction2:(id)sender;
@end

.m文件代码

#import "YKWebViewDemoViewController.h"

@interface YKWebViewDemoViewController ()

@end

@implementation YKWebViewDemoViewController

@synthesize webView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    CGRect rect = [[UIScreen mainScreen] bounds];
    
    webView = [[UIWebView alloc]initWithFrame:rect];
    webView.scalesPageToFit =YES;
    webView.delegate =self;
    [self.view addSubview:webView];             //加载到自己的view
    
    NSString *urlAddress = @"http://www.baidu.com";
    [self loadWebPageWithString:urlAddress];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    webView = nil;
    // Release any retained subviews of the main view.
}
- (void)dealloc
{
    [webView release];
    [super dealloc];
}
//自定义弹出菜单
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    UIMenuItem *customMenuItem1 = [[[UIMenuItem alloc] initWithTitle:@"custom 1" action:@selector(customAction1:)] autorelease];
    UIMenuItem *customMenuItem2 = [[[UIMenuItem alloc] initWithTitle:@"custom 2" action:@selector(customAction2:)] autorelease];
    UIMenuController *menu = [UIMenuController sharedMenuController];
    [menu setMenuItems:[NSArray arrayWithObjects:customMenuItem1, customMenuItem2, nil]];
    [menu setMenuVisible:YES animated:YES];
}
- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    
    [[UIMenuController sharedMenuController] setMenuItems:nil];
}

-(void)customAction1:(id)sender
{
    NSLog(@"customAction1 is active");
}

-(void)customAction2:(id)sender
{
    NSLog(@"customAction2 is active");    
}
- (BOOL)canPerformAction:(SEL)action withSender:(id)sender {
    [super canPerformAction:action withSender:sender];
    if (action == @selector(customAction1:) || action == @selector(customAction2:)) {
        return YES;
    }
    else {
        return NO;
    }
}

-(BOOL)canBecomeFirstResponder
{
    [super canBecomeFirstResponder];
    return YES;
}
//加载网址
- (void)loadWebPageWithString:(NSString*)urlString
{
    NSURL *url =[NSURL URLWithString:urlString];
    NSURLRequest *request =[NSURLRequest requestWithURL:url];
    [webView loadRequest:request];
}

- (void)webViewDidStartLoad:(UIWebView *)webView;
{
    NSLog(@"didStartLoad is called");
}

- (void)webViewDidFinishLoad:(UIWebView *)webView
{
    NSLog(@"didFinishLoad is called");
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error
{
    UIAlertView *alterview = [[UIAlertView alloc] initWithTitle:@"" 
//                                                        message:[error localizedDescription]  
                                                        message:@"页面加载失败。" 
                                                       delegate:nil 
                                              cancelButtonTitle:nil 
                                              otherButtonTitles:@"OK", nil];
    [alterview show];
    [alterview release];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
@end

 

在AppDelegate.h里面添加如下代码

#import <UIKit/UIKit.h>
@class YKWebViewDemoViewController;
@interface YKAppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) YKWebViewDemoViewController *viewController;

@end

在AppDelegate.m里面导入Controller头文件

#import "YKWebViewDemoViewController.h"

并且添加

@synthesize viewController = _viewController;

修改原有didFinishLaunchingWithOptions代码如下

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
    // Override point for customization after application launch.
    self.viewController = [[YKWebViewDemoViewController alloc] init];
    self.window.rootViewController = self.viewController; 
    [self.window makeKeyAndVisible];
    return YES;
}

 

posted @ 2012-04-11 18:45 Lee.Kevin 阅读(58) 评论(0) 编辑

在iphone程序中,属性合成中的retain/copy/assign有什么区别?

1)assign就不用说了,因为基本上是为简单数据类型准备的,原子类类型,例如CGPoint、CGFloat等,而不是NS对象们;

2)retain VS copy

  • copy: 建立一个索引计数为1的对象,然后释放旧对象
  • retain:释放旧的对象,将旧对象的值赋予输入对象,再提高输入对象的索引计数为1

Copy其实是建立了一个相同的对象,而retain不是:

比如一个NSString对象,地址为0×1111,内容为@”STR”

Copy到另外一个NSString之后,地址为0×2222,内容相同,新的对象retain1,旧有对象没有变化

retain到另外一个NSString之后,地址相同(建立一个指针,指针拷贝),内容当然相同,这个对象的retain+1

也就是说,retain是指针拷贝,copy是内容拷贝。

 

ObjectiveC中的copyc++的一样分深拷贝和浅拷贝,怎样区分这两个对象呢?我的理解是:

   (1)深拷贝,就是新拷贝一块内存交给对象使用。

   (2)浅拷贝,就是觉得拷贝内存太浪费,直接给你我的地址吧,相当于retain

 

3)怎么区分这两种对象呢?

    ObjectiveC里面只有一种情况是浅拷贝,那就是不可变对象的copy,其它的都是深拷贝(包括不可变对象mutableCopy、可变对象的的copymutableCopy)。

posted @ 2012-04-09 10:36 Lee.Kevin 阅读(27) 评论(0) 编辑

写这篇日志居然已经是时隔三年了,还依稀记得刚上大学那会儿。。。时间飞逝啊,感慨万千。

不是神马教程类文章,也不具有代表性,仅作为自己记录找工作过程的点滴。

腾讯笔试+第一次面试

24日上午10点到12点笔试,题目还算比较基础,涉及面比较广,数据结构,操作系统,数据库,网络,linux等是重点。一共三类题型,选择题(20个,每题3分)共60分,填空题(10个,每题4分),填写程序段缺失语句,基本都是算法。然后是选做题(有加分),还有JAVA WEB方向必须做的题。这个我面的是C/C++,所以没做。考试时间2个小时,基本在30分钟就答完了,但是有很多不会的就胡乱选择一通。交卷走人。

24日下午6点收到短信,25日上午9点去参加XXX酒店面试,不错,拿到了入场券。

25日早晨提前10分钟赶到酒店,等待过程中发现很多都是拿大包小包过来的,简历都用专用简历袋装着,基本都是两到三页,还有很多拿着笔记看着代码的?我就想 兄弟有必要吗?要命中技术面试的考题比命中高考题还难吧。

腾讯一共安排了70个左右面试官同时面试一对一,等了二十分钟左右轮到我,拿着号码牌找面试官面试去。气氛还挺融洽,面试官虽然长得不面善,但是说话做事很和善,不苛刻。给我的第一印象还不错,至于我给他什么印象,想必永远不会知道了,以后有幸见到他问他也不会知道。

面试开始:面试例行工作问好。起码给对方一个好印象,我是带着简历去的,虽然只有一张纸,但也是经过我仔细处理过的,打印还花了我五块钱呢,还贴了一张照片。我见他桌子上也有一份我的简历,但是惨不忍睹啊,后悔笔试的时候没把简历做好,完全是凑合过去的,不知道会不会给我减分啊。问我两份简历有什么不同吗?我说我手里这份更清晰,用这个吧,遂给之。

1.首先来个自我介绍。这个我之前是有准备的,介绍时间一分钟,别太久,自己提前一定要打个草稿,不需要多华丽,但是要一气呵成。体现出自己的优势,与明确的目的。介绍完毕,面试官会抓住你所介绍或者简历里面罗列的项目实践开始炮轰,从这也可以看出企业重视的是实践,问的全是我在华为实习的项目问题,丝毫没有提及我在学校所做项目的知识,我也觉得学校那些项目真的太水了。

2.基础知识提问,问了我一些C语言中的函数的传引用和传指针的区别,(&a,*p)这两个的区别,最近一直弄C,所以这没问题搞定。后来问我数据结构里面的字符串比较,我把我知道的说了一下,好像没什么问题。再后来问我算法有没有深入研究,我这个真的是纠结了,我说我会点吧,我还真没学那个课,要是问我 我真不知道不是糗大了。我反正照实说了。我说我们是3选2,当时我没有修算法。它被安排再下个学期了。这事就算过去了。到现在我还在想,会不会因为算法这块把我刷掉。哎,听天由命吧

3.让我写一个函数,实现功能 从源字符串中提取出一个目标字符串,并返回目标字符串的指针。还好,这个字符串问题,上次华为实习面试的时候 就是问的我字符串匹配,当时那叫一个囧啊,我知道KMP,但是一直没看懂那个算法,也没深入研究,最后只用了最笨的BF算法。比较幸运,这次又面试的这个类似的问题,果断写之,加上最近一直在用C语言写代码,5分钟搞定,大体算法写了,之后给他讲解了一下,那面试官一直点头。后来问我能比较出来吗?我说要想实现还需要调试的。

4.工作意向,问我,你不是本地人吧,你期望工作地点是成都,如果让你去深圳,你会不会去。我果断答之,会。我在成都呆的久了,就选成都了,我是外地的,去哪里都没问题。

面试官说,好,那你有没有什么想问我的没。我知道 面试基本结束了。

我当时还真没想到太好的问题,都怪自己没提前准备好问题啊。所以当时就问问,搞终端开发的是在哪里工作,他说 成都、北京、广州、深圳、上海都有。我当时就想,你是不是糊弄我玩呢?难道真的哪里都有,这个以后再定哈哈。

然后我又问了一个问题(这个问题纯粹是凑数的),我问他,公司对移动终端这块重视程度够不够,他就说现在比如android系统的腾讯的软件你能搜到多少,我说恩不少,得有20多吧。彼此聊聊。

再问我还有什么问题。我就说暂时没有了,以后有机会再请教。

然后他居然比我先站起来了,感谢您来参加面试,伸手和我握手。我心想 你也太快了,还这么客气。我都不好意思了。你可别给我减分啊。呵呵,以前都是我先站起来握手的。。。

一面到此结束,回去等通知。整个工程不到1个小时(包括等待)还是很有效率的。赞一个。

看看有没有机会写二面吧。

posted @ 2011-09-25 14:41 Lee.Kevin 阅读(271) 评论(0) 编辑

温习一下C#基础知识。简要记录一下

C#中对变量的初始化要求比较高,是为了安全考虑

一般的编译器,只把变量的初始化当做是警告,而C#则当做错误。

C#有两个方法可以确保变量在使用前进行初始化

1.变量是类或结构体中的字段,如果没有显示的初始化,创建这些变量时,其值默认是0

2.方法的局部变量必须在代码中显示的初始化,之后才能在语句中使用它们的值。此时,初始化不是在声明变量时进行的,但编译器会通过检查所有可能的路径,如果检测到局部变量在初始化之前就使用了它的值,就会产生错误。

C#的方法与C++的方法相反,在C++中,编译器让程序员确保变量在使用之前进行了初始化。

例如,C#中不能使用下列语句

public static void Main()
{
int a;
Console.WriteLine(a);
return;
}

报错:error CS0165:使用了未赋值的局部变量“a”

看一下下面这语句

Test objTest;

在C++中,上面的代码会在对堆栈中创建一个Test类的实例。而再C#中,这行代码仅会为Test类创建一个引用。这个引用还没有指向任何对象。

C#中实例化一个对象需要使用new关键字  

Test objTest;
objTest
= new Test();

  

posted @ 2011-09-17 00:18 Lee.Kevin 阅读(42) 评论(0) 编辑
摘要: 最近在复习数据结构,想把数据结构里面涉及的都自己实现一下,完全是用C语言实现的。自己编写的不是很好,大家可以参考,有错误希望帮忙指正,现在正处于编写阶段,一共将要实现19个功能。到目前我只写了一半,先传上来,大家有兴趣的可以帮忙指正,谢谢在vs2010上面编译运行无错误。每天都会把我写的新代码添加到这个里面。直到此链表完成。#include "stdafx.h"#include "stdio.h"#include <stdlib.h>#include "string.h"typedef int elemType ;/***阅读全文
posted @ 2011-08-20 21:37 Lee.Kevin 阅读(2456) 评论(5) 编辑
摘要: #include <iostream>#include <iomanip>using namespace std;template <class T>class node //节点{public:T data; node *next;};template <class T>class list{public:list();void Create(); //创建链表bool Empty() const; //判断链表是否为空void InsertLast(const T e); //从尾部插入一个元素void InsertFirst(const T阅读全文
posted @ 2011-08-17 22:49 Lee.Kevin 阅读(248) 评论(0) 编辑
摘要: 写出一个struct,然后sizeof,你会不会经常对结果感到奇怪?sizeof的结果往往都比你声明的变量总长度要大,这是怎么回事呢?讲讲字节对齐吧./******************************分割线如果体系结构是不对齐的,A中的成员将会一个挨一个存储,从而sizeof(a)为11。显然对齐更浪费了空间。那么为什么要使用对齐呢?体系结构的对齐和不对齐,是在时间和空间上的一个权衡。对齐节省了时间。假设一个体系结构的字长为w,那么它同时就假设了在这种体系结构上对宽度为w的数据的处理最频繁也是最重要的。它的设计也是从优先提高对w位数据操作的效率来考虑的。比如说读写时........阅读全文
posted @ 2011-08-17 20:23 Lee.Kevin 阅读(106) 评论(0) 编辑
摘要: 内存的使用规则:在使用malloc()或new申请空间时,要检查有没有分配空间成功,判断方法是判断指针是否为NULL,如申请一块很大的内存而没有这么大的内存则分配内存会失败;申请成功后最好是将该内存清空,使用memset()后ZeroMemory()清空,不然存在垃圾而造成有时候输出很大乱码;不要忘记为数组和动态内存赋初值,防止将未被初始化的内存作为右值使用;要防止数组或指针内存越界;申请内存成功后,使用结束后要释放,系统不会自动释放手动分配的内存;内存释放后,指针还是指向那块地址,不过这指针已经是“野指针”了,所以释放内存后指针要指向NULL,不然很危险,容易出错,if()对野指针的判断不起阅读全文
posted @ 2011-08-17 20:19 Lee.Kevin 阅读(119) 评论(0) 编辑
摘要: memest原型(pleasetype"manmemset"inyourshell)void*memset(void*s,intc,size_tn);memset:作用是在一段内存块中填充某个给定的值,它对较大的结构体或数组进行清零操作的一种最快方法。常见的三种错误第一:搞反了c和n的位置.一定要记住如果要把一个chara[20]清零,一定是memset(a,0,20)而不是memset(a,20,0)第二:过度使用memset,我想这些程序员可能有某种心理阴影,他们惧怕未经初始化的内存,所以他们会写出这样的代码:charbuffer[20];memset(buffer,0阅读全文
posted @ 2011-08-17 18:32 Lee.Kevin 阅读(124) 评论(5) 编辑