C++ offer
haker_road吧
全部回复
仅看楼主
level 9
戒者K💯 楼主
2016年03月10日 13点03分 1
level 9
戒者K💯 楼主
1、实现String类:
class String
{
public:
String(char *pStr = NULL)
{
if(p = NULL)
{
_pStrData = new char[1];
_pStrData[0] = '\0';
}
else
{
_pStrData = new char[strlen(pStr) + 1];
strcpy(_pStrData, pStr);
}
}
String(String &otherStr)
{
if(this != &otherStr)
{
_pStrData = new char[strlen(otherStr._pStrData) + 1];
strcpy(_pStrData, otherStr._pStrData);
}
}
String & operator =(String &otherStr)
{
if(this != &otherStr)
{
delete []_pStrData;
_pStrData = new char[strlen(otherStr._pStrData) + 1];
strcpy(_pStrData, otherStr._pStrData);
}
return *this;
}
~String()
{
delete []_pStrData;
}
public:
char *_pStrData;
}
2016年03月10日 14点03分 2
level 9
戒者K💯 楼主
2、在C++程序中调用被C 编译器编译后的函数,为什么要加extern “C”?:
C头文件1.h中有一个函数:add(int,int);
C++编译程序后,会在C编译的dll中查找_add_int_int函数,与C编译后的dll里的函数名不同,找不到。
加了extern "C" add(int,int)后,查找的函数名为:add(int,int),与C编译后的dll里的函数名相同,可以找到。
2016年03月10日 15点03分 3
用法: extern "C" { #include "cExample.h" }
2016年03月10日 15点03分
level 9
戒者K💯 楼主
3、New delete 与mallocfree 的联系与区别?:
答案:都是在堆(heap)上进行动态的内存操作。用malloc函数需要指定内存分配的字节数并且不能初始化对象,new 会自动调用对象的构造函数。delete 会调用对象的destructor,而free 不会调用对象的destructor.
2016年03月10日 15点03分 4
level 9
戒者K💯 楼主
4、#define DOUBLE(x) x+x ,i = 5*DOUBLE(5); i 是多少?
答案:i 为30。(注意直接展开就是了) 5 * 5 + 5
2016年03月10日 15点03分 5
level 9
戒者K💯 楼主
5、描述内存分配方式以及它们的区别?
1) 从静态存储区域分配。内存在程序编译的时候就已经分配好,这块内存在程序的整个运行期间都存在。例如全局变量,static 变量。
2) 在栈上创建。在执行函数时,函数内局部变量的存储单元都可以在栈上创建,函数执行结束时这些存储单元自动被释放。栈内存分配运算内置于处理器的指令集。
3) 从堆上分配,亦称动态内存分配。程序在运行的时候用malloc 或new 申请任意多少的内存,程序员自己负责在何时用free 或delete 释放内存。动态内存的生存期由程序员决定,使用非常灵活,但问题也最多。
4) 代码区。
2016年03月10日 15点03分 6
level 9
戒者K💯 楼主
6.分别写出BOOL,int,float,指针类型的变量a 与“零”的比较语句。
答案:
BOOL :  if ( !a ) or if(a)
int :   if ( a ==0)
float : const EXPRESSION EXP =0.000001
  if ( a < EXP&& a >-EXP)
pointer : if ( a != NULL) or if(a == NULL)
2016年03月10日 15点03分 7
level 9
戒者K💯 楼主
7.文件中有一组整数,要求排序后输出到另一个文件中(面试官,超级喜欢考排序的。你要去面试,数据结构的那几个排序一定要非常熟悉,用笔也可以写出代码来,用笔写代码,就是这样变态啊,其实感觉没有必要这样笔试)
答案:
#include<iostream>
#include<fstream>
usingnamespace std;
void Order(vector<int>& data)//bubble sort // 此处可以用任意排序方法实现
{
int count = data.size() ;
int tag =false ; // 设置是否需要继续冒泡的标志位
for ( int i =0 ; i < count ; i++)
{
for ( int j =0 ; j < count - i -1 ; j++)
{
if ( data[j] > data[j+1])
{
tag =true ;
int temp = data[j] ;
data[j] = data[j+1] ;
data[j+1] = temp ;
}
}
if ( !tag )
break ;
}
}
void main( void )
{
vector<int>data;
ifstream in("c:\\data.txt");
if ( !in)
{
cout<<"file error!";
exit(1);
}
int temp;
while (!in.eof())
{
in>>temp;
data.push_back(temp);
}
in.close(); //关闭输入文件流
Order(data);
ofstream out("c:\\result.txt");
if ( !out)
{
cout<<"file error!";
exit(1);
}
for ( i =0 ; i < data.size() ; i++)
out<<data[i]<<"";
out.close(); //关闭输出文件流
}
2016年03月10日 15点03分 8
level 9
戒者K💯 楼主
8.0、数据结构
一、链表逆序:
一个链表的结点结构
#include <iostream>
using namespace std;
class Node
{
public:
Node(Node *node = NULL)
{
next = node;
}
void Show()
{
cout<<this<< " ";
}
~Node()
{
if(next)
{
delete next;
next = NULL;
}
}
public:
Node *next;
};
Node *ReverseList(Node *head)
{
if(head == NULL || head->next == NULL)
{
return head;
}
Node *p1 = head->next;
head->next = NULL;
Node *p2 = p1->next;
p1->next = head;
while(p2)
{
Node *p3 = p2->next;
if(!p3)
{
p2->next = p1;
break;
}
p2->next = p1;
p1 = p2;
p2 = p3;
}
head = p2;
return head;
}
void ShowNode(Node *head)
{
Node *p = head;
while(p)
{
p->Show();
p = p->next;
}
}
int main()
{
Node *head = new Node;
Node *node1 = new Node;
Node *node2 = new Node;
Node *node3 = new Node;
head->next = node1;
node1->next = node2;
node2->next = node3;
node3->next = NULL;
ShowNode(head);
Node *p = ReverseList(head);
ShowNode(p);
delete p;
return 0;
}
2016年03月11日 11点03分 9
level 9
戒者K💯 楼主
2016年03月11日 13点03分 10
level 9
戒者K💯 楼主
9、list和vector的区别
1 逻辑结构:list线性的可以非连续,vector顺序结构。
2 物理结构:list 是线性链表、插入删除元素非常方便,vector是顺序数组支持随机访问、查找方便。
2016年03月14日 11点03分 11
level 9
戒者K💯 楼主
10、SendMessage和PostMessage的区别
PostMessage 不管同线程或非同线程都是直接将消息投递到线程的消息队列中
SendMessage 同线程直接调用消息响应回调函数,非同线程将消息投递到消息队列,阻塞线程等待消息处理完毕返回。 假如对方窗口同时也用SendMessage发送一个消息过来,这时候就会产生死锁,操作系统会立即处理消息避免死锁发生。
2016年03月14日 12点03分 12
level 9
戒者K💯 楼主
11、死锁
产生死锁的四个必要条件:
(1) 互斥条件:一个资源每次只能被一个进程使用。
(2) 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。
(3) 不剥夺条件:进程已获得的资源,在末使用完之前,不能强行剥夺。
(4) 循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系。
解决方案:合理的规划线程之间资源的调度。预防为主
2016年03月15日 10点03分 13
level 9
戒者K💯 楼主
1)互斥条件:指进程对所分配到的资源进行排它性使用,即在一段时间内某资源只由一个进程占用。如果此时还有其它进程请求资源,则请求者只能等待,直至占有资源的进程用毕释放。2)请求和保持条件:指进程已经保持至少一个资源,但又提出了新的资源请求,而该资源已被其它进程占有,此时请求进程阻塞,但又对自己已获得的其它资源保持不放。3)不剥夺条件:指进程已获得的资源,在未使用完之前,不能被剥夺,只能在使用完时由自己释放。4)环路等待条件:指在发生死锁时,必然存在一个进程——资源的环形链,即进程集合{P0,P1,P2,···,Pn}中的P0正在等待一个P1占用的资源;P1正在等待P2占用的资源,……,Pn正在等待已被P0占用的资源。
2016年03月15日 10点03分 14
level 9
戒者K💯 楼主
12、你对多线程是怎么理解的?
2016年03月15日 12点03分 15
1 2 尾页