C++面试

1、以下三条输出语句分别输出什么?【基础】

char str1[] = "abc";
char str2[] = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char* str5 = "abc";
const char* str6 = "abc";
cout << boolalpha << (str1==str2) << endl; //输出什么?
cout << boolalpha << (str3==str4) << endl; //输出什么?
cout << boolalpha << (str5==str6) << endl; //输出什么?

答:输出为:false、false、true。

2、以下反向遍历array 数组的方法有什么错误?【基础】

vector<int> array;
array.push_back(1);
array.push_back(2);
array.push_back(3);
//反向遍历array 数组:
for(vector<int>::size_type i=array.size()-1; i>=0; --i){
cout << array[i] << endl;
}

答:for 循环中的变量i 的类型不应定义为vector::size_type,
因为该类型为无符号数值类型,故循环条件将恒成立,为死循环,应将其类型定义为有符号的int 类型。

3、以下代码有什么问题?【基础】

cout << (true ? 1 : "1") << endl;

答:运算符中两个可选值的类型不同。
4、以下代码有什么问题?【基础】

typedef vector<int> IntArray;
IntArray array;
array.push_back(1);
array.push_back(2);
array.push_back(2);
array.push_back(3);
//删除array 数组中所有的2
for(IntArray::iterator itor=array.begin(); itor!=array.end();
++itor){
if(2==*itor) {
array.erase(itor);
}
}

答:for 循环中的if 语句后的array.erase(itor)语句,它将迭代器itor 所指向的元素删除后会自动下移一位,故应在其后加上语句:itor–;

5、以下代码中的两个sizeof 用法有问题吗?【基础】

void upperCase(char str[]){ //将str 中的小写字母转换成大写字母
for(int i=0; i<sizeof(str)/sizeof(str[0]); ++i){
if('a'<=str[i] && str[i]<='z')
str[i] -= ('a'-'A');
}
}
int main(){
char str[] = "aBcDe";
cout << "str 字符串长度为:" << sizeof(str)/sizeof(str[0]);
cout << endl;
upperCase(str);
cout << str << endl;
return 0;
}

答:在upperCase 方法中,for 循环的sizeof(str)的值将总是4,所以该方法
只能将参数中的字符串的前四个字符转换成大写字母。

6、以下代码能够编译通过吗?为什么?【基础】

unsigned int const size1 = 2;
char str1[size1];
unsigned int temp = 0;
cin >> temp;
unsigned int const size2 = temp;
char str2[size2];

答:能;

7、以下代码有什么问题?【基础】

struct Test{
Test(int){}
Test(){}
void fun(){}
};
void main(void){
Test a(1);
a.fun();
Test b();
b.fun();
}

答:main 函数的返回类型应为int;不能对b 调用fun()方法。

8、以下代码中的输出语句输出0 吗?为什么?【基础】

struct CLS{
int m_i;
CLS(int i):m_i(i){ }
CLS(){ CLS(0);}
};
int main(){
CLS obj;
cout <<obj.m_i << endl;
}

答:输出不是0;

9、C++中的空类,默认产生哪些类成员函数?【基础】

答:空类中默认包含的成员函数如下:

class Empty{
public:
Empty(); //缺省构造函数
Empty( const Empty& ); //拷贝构造函数
~Empty(); //析构函数
Empty& operator=( const Empty& ); //赋值运算符
Empty* operator&(); //取址运算符
const Empty* operator&() const; //取址运算符const
};

10、统计一篇文章中单词个数。【基础】

答:代码如下:

include<iostream>
#include<fstream>
using namespace std;
int main(){
ifstream fin("t.txt");
if(!fin){
cout<<"can't open file"<<endl;
return -1;
}
int count = 0;
char buf[256];
memset(buf, 0, 256);
while(1){
fin2>>buf;
if(fin2.eof())
break;
count++;
}
cout<<"The number of the words is : "<<count<<endl;
fin2.close();
return 0;
}

11、写一个函数,完成内存之间的拷贝。【中等难度】

答:代码如下:

void* mymemcpy(void* dest, const void* src, size_t count){
char* pdest = static_cast<char*>(dest);
const char* psrc = static_cast<const char*>(src);
if(pdest>psrc && pdest<psrc+count){ //能考虑到这种情况就行了
for(size_t i=count-1; i!=-1; --i){
pdest[i] = psrc[i];
}
}else{
for(size_t i=0; i<count; ++i){
pdest[i] = psrc[i];
}
}
return dest;
}
int main(){
char str[] = "0123456789";
mymemcpy(str+1, str+0, 9);
cout << str << endl; //将输出"0012345678"
return 0;
}

12、非C++内建类型A 和B,在哪几种情况下B 能隐式转化为A?【较难】

答:a)class B : public A{……}//B 公有继承自A,可以是间接继承的
b)class B{operator A();}//B 实现了隐式转化为A 的转化
c)class A{ A(const B&);}//A 实现了non-explicit 的参数为B 构造函数(可以有其他带带默认值的参数)
d)A& operator= (const A&);//赋值操作,虽不是正宗的隐式类型转换,但也可以勉强算一个

13、以下代码有什么问题?【较难】

void char2Hex(char c){ //将字符以16 进制显示
char ch = c/0x10 + '0';
if(ch>'9') ch += ('A'-'9'-1);
char cl = c%0x10 + '0';
if(cl>'9') cl += ('A'-'9'-1);
cout << ch << cl << ' ';
}
int main(){
char str[] = "I love 中国";
for(size_t i=0; i<strlen(str); ++i)
char2Hex(str[i]);
cout << endl;
return 0;
}

答:
13、以下两条输出语句分别输出什么?【较难】

float a = 1.0f;
cout << (int)a << endl;
cout << (int&)a << endl;
cout << boolalpha << ((int)a==(int&)a) << endl; //输出什么
float b = 0.0f;
cout << (int)b << endl;
cout << (int&)b << endl;
cout << boolalpha << ((int)b==(int&)b) << endl;//输出什么

答:第一处输出false,第二处输出true。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值