C++笔试面试题目集合 迷南。 2022-09-17 05:30 169阅读 0赞 **1. 非C++内建型别A和B,在哪几种情况下B能隐式转化为A?\[C++中等\]** **答:** **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&); //赋值操作,虽不是正宗的隐式类型转换,但也可以勉强算一个** **2. cout << (true?1:"1") <<endl; 有什么错误?** **本语句中 ? : 运算符的后面两个变量或值的类型要求为一样的类型,因此出现编译错误。** **3.** **C++中的空类,默认产生哪些类成员函数?** class Empty { public: Empty(); // 缺省构造函数 Empty( const Empty&); // 拷贝构造函数 ~Empty(); // 析构函数 Empty& operator=(const Empty& ); // 赋值运算符 Empty* operator&(); // 取址运算符 const Empty* operator&() const; // 取址运算符 const }; 4. 代码的结果: 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; // 输出什么? 结果: 1 1065353216 false 0 0 true 5. 以下代码中的输出语句输出0吗,为什么? struct CLS { int m_i; CLS( int I ) : m_i(i) {} CLS() { CLS(0); } }; CLS obj; cout << obj.m_i << endl; 不能,因为m\_i没有初始化,会输出一个很大的数,在构造函数中调用带有初始化列表的构造函数时并不能使得初始化列表起作用。 6. 写一个函数,完成内存之间的拷贝 void*mymemcpy( void *dest, const void *src, size_t count ) { char* pdest = static_cast<char*>(dest ); const char* psrc = static_cast<constchar*>( 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(void ) { char str[] = "0123456789"; mymemcpy( str+1, str+0, 9 ); cout << str << endl; system( "Pause" ); return 0; } 7. 栈的push、pop序列是否一致 (转载) #include <iostream> #include <stack> using namespace std; bool Match(char * strPush, char * strPop) { if(strPush == NULL || strPop == NULL) { return false; } int len1 = strlen(strPush); int len2 = strlen(strPop); if( len1 != len2) return false; stack<char> s; int index = 0; for(int i = 0; i < len1; i++) { s.push( strPush[i]); while( !s.empty() && index < len2 && s.top() == strPop[index]) { index ++; s.pop(); } } if(s.empty()) return true; else return false; } int main() { char str1[] = { '1', '2', '3', '4', '5', '\0'}; char str2[] = { '4', '5', '3', '2', '1', '\0'}; if(Match(str1, str2)) { cout << "str1 and str2 are match!" << endl; } else { cout << "str1 and str2 are not match!" << endl; } char str3[] = { '1', '2', '3', '4', '5', '\0'}; char str4[] = { '4', '5', '3', '2', '1', '1', '\0'}; if(Match(str3, str4)) { cout << "str3 and str4 are match!" << endl; } else { cout << "str3 and str4 are not match!" << endl; } char str5[] = { '1', '2', '3', '4', '5', '1', '\0'}; char str6[] = { '4', '5', '3', '2', '1', '\0'}; if(Match(str5, str6)) { cout << "str5 and str6 are match!" << endl; } else { cout << "str5 and str6 are not match!" << endl; } char str7[] = { '1', '6', '3', '4', '5', '\0'}; char str8[] = { '4', '5', '3', '2', '1', '\0'}; if(Match(str7, str8)) { cout << "str7 and str8 are match!" << endl; } else { cout << "str7 and str8 are not match!" << endl; } return 0; } 8. 递归反转一个栈,要求不得重新申请一个同样的栈,空间复杂度O(1) —— 转载 博主说: 算法思想:汉诺塔的思想,非常复杂,玩过九连环的人都想得通的 static void ReverseStack(ref Stack stack) { if (stack.Count == 0) return; object top = stack.Pop(); ReverseStack(ref stack); if (stack.Count == 0) { stack.Push(top); return; } object top2 = stack.Pop(); ReverseStack(ref stack);//p1 stack.Push(top); ReverseStack(ref stack);//p2 stack.Push(top2); } 请问两个问题: 第一个: 注释p1,p2两处的递归用来干什么?顺序可以点到吗? 第二个:如果做到只用一个堆栈就搞定反转的? [![未命名图片 lp 2][lp 2]][lp 2_lp 2] 9. 不适用额外的空间为栈进行排序—— —— 转载 延伸第8题的思路,有如下的思路 #include <iostream> #include <stack> using namespace std; void Sort(stack<int> &s) { if (s.size() == 0) return; int top = s.top(); s.pop(); Sort(s); if (s.size() == 0) { s.push(top); return; } int top2 = s.top(); s.pop(); if ((int)top > (int)top2) { s.push(top); Sort(s); s.push(top2); } else { s.push(top2); Sort(s); s.push(top); } } int main() { stack<int> s; s.push(1); s.push(3); s.push(2); s.push(9); s.push(5); s.push(7); s.push(4); Sort(s); while(!s.empty()) { cout << s.top() << " "; s.pop(); } cout << endl; return 0; } 10. 查找二叉树中两个节点的最低公共父节点 思路:首先将节点的路径逆序(从节点到根节点)压入栈中,然后对比栈,以查找最低功能父节点。 #include <iostream> #include <stack> using namespace std; typedef struct _BiNode { int value; struct _BiNode * pLeft; struct _BiNode * pRight; }BiNode, *BiTree; bool GetPositionByNode(BiNode * root, BiNode * node, stack<BiNode*> &nodeStack) { if (root == NULL) return false; if (root == node) { nodeStack.push(root); return true; } if ( GetPositionByNode(root->pLeft, node, nodeStack) || GetPositionByNode(root->pRight, node, nodeStack)) { nodeStack.push(root); return true; } return false; } BiNode * FindParentNode(BiNode * root, BiNode * node1, BiNode * node2) { stack<BiNode *> stack1; GetPositionByNode(root, node1, stack1); stack<BiNode *> stack2; GetPositionByNode(root, node2, stack2); BiNode * tempNode = NULL; while (stack1.top() == stack2.top()) { tempNode = (BiNode *)stack1.top(); stack1.pop(); stack2.pop(); } return tempNode; } 11. C++中不能被重载的运算符 运算符中,除了 ‘.’,‘.\*’, ‘::’, ‘? :’,‘sizeof’, ‘typeid’ 运算符,其他的基本都可以进行重载。 **By Andy @ 2013-9-28** [lp 2]: http://images.cnblogs.com/cnblogs_com/titer1/201209/20120918151713712.png [lp 2_lp 2]: /images/20220828/70968dcd091c454bb8d26d7e0b8611b4.png
还没有评论,来说两句吧...