Wonderland of C++

不要做一个浮躁的人

  C++博客 :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  13 随笔 :: 1 文章 :: 0 评论 :: 0 Trackbacks

2008年11月27日 #

1,解释inheritance
2,空类编译器自动添加了哪些函数
3,类型转换有哪些,分个解释
4,delete this会怎么样?
5,什么时候用到拷贝构造函数
6,拷贝构造函数为什么不用值传递
7,static关键字的问题,static members,static function意思。可否从object调用static function?
8,stl,什么时候用list,什么时候用vector,两者的区别是什么。
9,解释override,定义为override的函数需要什么条件
10,virtual destructor的作用,为什么要virtual

1,解释inheritance
...自己发挥,说些显示生活的中继承相关的东西会更形象生动。
2,空类编译器自动添加了哪些函数
应该产生默认构造函数、析构函数、拷贝构造函数、赋值操作符函数。
3,类型转换有哪些,分个解释
static_cast:普通转换
const_cast:将const转换成非const
dynamic_cast:将父类指针或引用转化为子类指针或引用,如果指针或引用指向的确为待转化子类,则成功转化,否则抛出异常(引用情况)或返回0(指针情况)。
reinterpret_cast:通常为操作数的位模式提供较低层次的重新解释。就是当前内存解释为某种类型对象。
4,delete this会怎么样?
如果对象是new来的,那么delete应该没关系,但是后来不能再用delete pt;如果对象是在栈中申请的,则很危险,要根据编译器不同有不同情况出现。
5,什么时候用到拷贝构造函数
初始化时,即对象未构建时使用。例如:className A(B)或className A=B;B为className类型对象。
6,拷贝构造函数为什么不用值传递
值 传递时,参数有一个初始化问题,如className( className a );调用时,要将实参赋给形参,如调用className A(B),函数先调用className a (B)需要用到拷贝构造函数,形成无限递归。即使可以,如果类很大的话,效率也是很低下的。
7,static关键字的问题,static members,static function意思。可否从object调用static function?
类的静态成员和静态函数,静态成员即为类共有的成员变量。静态函数不含this指针参数。
当然可以。
8,stl,什么时候用list,什么时候用vector,两者的区别是什么。
list即链表,如果经常插入、删除元素,则用它,
vector即数组,如果经常随机访问元素,则用它,
区别:连续存储和非连续存储,其实区别很多,可以参看数组和链表数据结构的特点。
如果既要插入又要查找,则先用list建立数据集,在将list拷贝给vector。
9,解释override,定义为override的函数需要什么条件
就是重写,首先父类的相应函数要有virtual关键字修饰,用父指针调用该函数时,根据指针实际指向对象类型调用其定义的函数,如果其没有重定义函数,则调用其父类的。
10,virtual destructor的作用,为什么要virtual
一般析构函数都要用virtual,这样析构的时候可以调用指针所指向对象(可能为子类对象)的定义的析构函数,避免一些操作未做,或资源未释放。
posted @ 2008-11-27 13:22 Robert.Su 阅读(299) | 评论 (0)编辑 收藏

2008年10月31日 #

用 异或^ 运算可以很轻松的代替中间变量来做很多事情,比如互换两个变量的值,实现字符串的反转。

//字符串反转
void reverse_str(char *ch)   
{
   
int len;
   
int i;
   len 
= strlen(ch)-1;
   
char ctemp;

   
for(i = 0; i < len-i; i++){
         ch[i] 
= ch[i] ^ ch[len-i];
         ch[len
-i] = ch[i] ^ ch[len-i];
         ch[i] 
= ch[i] ^ ch[len-i];
   }

   ch[len
+1= 0;                    
}


测试 
char c[] = "abcdefg";
 reverse_str(c);
 cout<<c<<endl;

//交互变量
void swap(int &a,int &b){
    
//a = a + b;
    
//b = a - b;
    
//a = a - b;
    
    a
=a^b;
    b
=b^a;
    a
=a^b;
}
posted @ 2008-10-31 15:32 Robert.Su 阅读(380) | 评论 (0)编辑 收藏

2008年10月28日 #

有什么快的办法?能把141.24.249.130提取出来

Fwd'd *         Reg *
Query *
1       planetlab2.fem.tu-ilmenau.de
141.24.249.130 *        no transit
summary failed  sees 171
287 avoiding    20.6 D
7.5 D   121
0       2.48


正则表达式
(\d+\.){3}\d+

方法-2:

import re

text = '''
asb sdwe 1.1
123.123.123.567 get ip
test data
'''
lines = text.split("\n")

for line in lines:    
    g = re.compile(r"^.*?([0-9]+[.][0-9]+[.][0-9]+[.][0-9]+).*$").match(line)
    if g<> None:        
        print g.groups(1)[0]
posted @ 2008-10-28 14:35 Robert.Su 阅读(1266) | 评论 (0)编辑 收藏

2008年10月10日 #

#include "stdafx.h"
#include 
<iostream>
#include 
<vector>   
using   namespace std;   


void   vect(vector<int>   &ve)   
{   
          ve.push_back(
100);   
}
   
    
void   main()   
{   
          vector
<int>   v;   
          vect(v);
          
for (vector<int>::iterator it = v.begin();it != v.end();++it)
          cout
<<*it<<endl;}

          cin.
get();
}


Robert Su 2008-10-10 11:10 发表评论

文章来源:http://www.blogjava.net/babymouse/archive/2008/10/10/233536.html
posted @ 2008-10-10 11:10 Robert.Su 阅读(1828) | 评论 (0)编辑 收藏

2008年10月8日 #

 

#include <iostream> 
#include 
<vector> 

using namespace std; 
int main()
int ia[] = {1,2,3,4,5,6,7,8,9}
vector
<int> ivec(ia,ia+9); 
vector
<int> tvec(ia,ia+9); 
for (vector<int>::iterator it = ivec.begin();it != ivec.end();++it)
for (vector<int>::iterator iv = tvec.begin();iv != tvec.end();++iv)
cout
<<*it<<"*"<<*iv<<"="<<*it * *iv<<" "
if(*iv == 9
cout
<<endl; 
}
 
}
 
}
 


Robert Su 2008-10-08 15:34 发表评论

文章来源:http://www.blogjava.net/babymouse/archive/2008/10/08/233152.html
posted @ 2008-10-08 15:34 Robert.Su 阅读(476) | 评论 (0)编辑 收藏

2008年8月15日 #

111
import sys
def readfile(filename):
        
'''Print a file the standard output.'''
        f 
= file(filename)
        
while True:
                line 
= f.readline()
                
if len(line) == 0:
                        
break
                
print line, #notice comma
        f.close()

if len(sys.argv) < 2:
        
print 'No action specified.'
        sys.exit()

if sys.argv[1].startswith('--'):
        option 
= sys.argv[1][2:]
        
if option == 'verison':
                
print 'Version 1.2'
        
elif option == 'help':
                
print '''\
This program prints files to the standard output.
Any number of files can be specified.
Options include:
        --version : Print the version number
        --help    : Display this help
'''
                
else:
                
print 'Unknown option.'
                sys.exit()
else:
        
for filename in sys.argv[1:]:
                readfile(filename)
运行提示:
D:\python>python cat.py
  File "cat.py", line 27
    else:
       ^
SyntaxError: invalid syntax

谁知道这个哪里出错了啊


Robert Su 2008-08-15 17:41 发表评论

文章来源:http://www.blogjava.net/babymouse/archive/2008/08/15/222312.html
posted @ 2008-08-15 17:41 Robert.Su 阅读(282) | 评论 (0)编辑 收藏

2008年8月8日 #

 

#include <stdio.h> 
void ShowMe() 

  printf(
"showme\n"); 

int add(int value,int value1) 

  
int * pAdd=&value; 
  
int* value2=pAdd-1
  
*value2=*value2-0x0e
  
return (value+value1); 

/* 
*/ 
int add3v(int v,int v1,int v2) 

  
return (v+v1+v2); 

int main(int argc, char* argv[]) 
{    ShowMe(); 
      
int temp=add(10,12); 
      
int te=add3v(3,4,5); 
        printf(
"%d,%d",temp,te); 

        
return 0
}

这个程序输入在VC下是一直是showMe,死循环
结果是:不停的调用showme()。
showme
showme
showme
showme
showme
showme
showme
。。。


请教了同学,找出了答案
这个是自动变量存储栈,传给函数的参数是以栈这种结构来开辟暂时存贮空间的。现在的C++编译器处理函数参数后按照参数表的顺序往临时开辟的栈空间中压入数据,以这段程序来说~先压进value,再压进value1,而函数内部语句的执行代码也是以栈的形式存贮的
 ShowMe();这段程序的执行代码是最先被压入函数的执行栈中的,int temp=add(10,12); 地址要高于ShowMe();这个函数的地址
,int temp=add(10,12); 把地址又改回里低地址。
局部函数的参数
函数内代码的执行,都是按照栈这种方法进行的

这道题目不能说没有意思,主要考察了基础的汇编以及堆栈知识。
int* value2=pAdd-1;
*value2=*value2-0x0e;

这里明显修改了add函数返回地址,刚恰好showme()的入口地址,所以就
add->showme->add->showme ...
不停的调用下去。




Robert Su 2008-08-08 18:14 发表评论

文章来源:http://www.blogjava.net/babymouse/archive/2008/08/08/220960.html
posted @ 2008-08-08 18:14 Robert.Su 阅读(376) | 评论 (0)编辑 收藏

去年的一道面试题,想起来了,顺遍把代码贴到这里
两个数组合并到一起,然后排序;用set确实比较方面了

#include<iostream>
#include
<set>  
#include
<iterator>
#include
<algorithm>  
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{

    
int a[5]={2,5,1,2,3    };
    
int b[10]={8,9,5,9,7,1,3,4,4,6};

    
set<int>ist(a,a+sizeof(a)/sizeof(a[0])); 
    
set<int>::iterator  itor; 
    
set<int>it(b,b+sizeof(b)/sizeof(b[0]));
    
set<int>::iterator p;
    
for( itor = ist.begin();itor !=ist.end();++itor)   
    { 
        cout
<<*itor<<endl;
    }
    
for(p=it.begin();p!=it.end();++p)
    { 
        cout
<<*p<<endl;

    }
    cout
<<"合并后:"<<endl;  

    
set<int>su;
    
set<int>::iterator q;
    set_union(ist.begin(),ist.end(),it.begin(),it.end(),inserter(su,su.begin()));

    
for(q=su.begin();q!=su.end();++q)
    {
        cout
<<*q<<endl;
    }
    
return 0;
}

}


Robert Su 2008-08-08 15:26 发表评论

文章来源:http://www.blogjava.net/babymouse/archive/2008/08/08/220927.html
posted @ 2008-08-08 15:26 Robert.Su 阅读(349) | 评论 (0)编辑 收藏

2008年8月7日 #

今天qu推荐了一个软件给我——Cardio Calipers
一款刻度尺软件,可以测量屏幕上某个东西的长度,挺好玩的,很有想法:)

下载地址:
http://www.iconico.com/cardioCaliper/

附截图




这个尺子通过鼠标可以旋转,拉长缩短

Robert Su 2008-08-07 15:50 发表评论

文章来源:http://www.blogjava.net/babymouse/archive/2008/08/07/220691.html
posted @ 2008-08-07 15:50 Robert.Su 阅读(352) | 评论 (0)编辑 收藏

2008年7月24日 #

操作的系统的多进程实现了___________
多线程的根本是________________
JVM线程调度方式是______________

Robert Su 2008-07-24 10:37 发表评论

文章来源:http://www.blogjava.net/babymouse/archive/2008/07/24/217113.html
posted @ 2008-07-24 10:37 Robert.Su 阅读(221) | 评论 (0)编辑 收藏

仅列出标题  下一页