c++ 面试题(C/C++/STL)
1,智能指针:auto_ptr(c++11 已经弃用),unique_ptr(用于取代 auto_ptr), shared_ptr, weak_ptr
http://www.cnblogs.com/TenosDoIt/p/3456704.html(值得一看)
https://blog.csdn.net/zhourong0511/article/details/80315961(优缺点分析)
// classTest.cpp : 定义控制台应用程序的入口点。
// #include "stdafx.h"
#include <iostream>
#include <string>
#include <memory> using namespace std; class Test
{
public:
Test(string s);
~Test();
string& getStr();
void setStr(string s);
void print(); private:
string str;
}; Test::Test(string s)
{
str = s;
cout << "Test creat" << endl;
}
Test::~Test()
{
cout << "Test delete:" << str << endl;
}
string& Test::getStr() {
return str;
}
void Test::setStr(string s) {
str = s;
}
void Test::print() {
cout << str << endl;
} // unique_ptr 使用
unique_ptr<Test> fun() {
return unique_ptr<Test>(new Test(""));
} int _tmain(int argc, _TCHAR* argv[])
{
cout << "==========unique_ptr===========" << endl; unique_ptr<Test> ptest(new Test(""));
unique_ptr<Test> ptest2(new Test(""));
ptest->print(); //
ptest2 = move(ptest); // ptest == null,ptest2 原内容释放,ptest2 接管ptest
if (ptest == nullptr)
cout << "ptest == nullptr" << endl;
Test* p = ptest2.release(); // 可能有返回值,返回当前指向的内存空间
p->print(); //
ptest.reset(p);
ptest->print();
ptest2 = fun();
ptest2->print();
cout << "===========shared_ptr===========" << endl;
shared_ptr<Test> sptest(new Test(""));
shared_ptr<Test> sptest2(new Test(""));
cout << sptest2->getStr()<< endl;
cout << sptest2.use_count() << endl;
sptest = sptest2; // "456"引用次数 +1,"123" 引用次数 -1(此时为 0 ,被销毁)
sptest->print(); //
cout << sptest2.use_count()<< endl; // 此时引用数为2
cout << sptest.use_count() << endl;// 指向资源的引用数 为 2
sptest.reset(); //引用数 -1
sptest2.reset(); //引用数 -1 ,此时为 0 ,被销毁
cout << "done" << endl;
system("pause");
return ;
}
smart pointer
2,智能指针的不足和缺陷:
https://www.zhihu.com/question/61008381
3,C++11 新特性:
http://www.cnblogs.com/George1994/p/6684989.html(值得一看)
4,虚继承,虚函数表:
https://www.cnblogs.com/fanzhidongyzby/archive/2013/01/14/2859064.html(值得一看)
5,C 内存模型:
https://blog.csdn.net/anyaas/article/details/17099377
C++ 内存模型:
https://www.cnblogs.com/findumars/p/5929831.html?utm_source=itdadao&utm_medium=referral(值得一看)
6,如果在一个函数内定义一个 static 对象,什么时候执行构造和析构函数,如果这个函数从没被调用,会怎么被析构?
#include <iostream>
#include <string>
#include <memory> using namespace std; class Test {
public:
Test() {
cout << "Constructor is executed" << endl;
}
~Test() {
cout << "Destructor is executed" << endl;
}
}; void myfunc() {
static Test obj;
} int main()
{
cout << "main() starts" << endl;
myfunc();
cout << "main() terminates" << endl; system("pause");
return ;
}
/* 输出结果
main() starts
Constructor is executed
main() terminates
请按任意键继续. . .
Destructor is executed
*/
static obj in function
分析:执行函数时,首先调用构造函数,但函数结束并不会调用析构函数,因为此对象 是 static 并不会被销毁,可以看到,在主函数退出之后,执行了析构函数。
#include <iostream>
#include <string>
#include <memory> using namespace std; class Test {
public:
Test() {
cout << "Constructor is executed" << endl;
}
~Test() {
cout << "Destructor is executed" << endl;
}
}; //
void myfunc() {
static Test obj;
} int main()
{
cout << "main() starts" << endl;
// myfunc(); // 并不调用函数
cout << "main() terminates" << endl; system("pause");
return ;
}
/* 输出结果
main() starts
main() terminates
请按任意键继续.
*/
not invoke function
分析:当不调用函数时,构造函数和析构函数都不会执行(有点奇怪。)
7,C++ 11 左值和右值
https://blog.csdn.net/chy19911123/article/details/46013499
8,STL 中容器的线程安全问题
https://stackoverflow.com/questions/5912539/stl-map-find-thread-safe
9,为什么不要在构造函数/析构函数中调用虚函数?
https://blog.csdn.net/xiaoqi2008/article/details/39371191
10,malloc设计的系统调用?
https://blog.csdn.net/Always__/article/details/50990838
11,构造函数为什么不能是虚函数?
https://blog.csdn.net/jiadebin890724/article/details/7951520
12,内存对齐以及如何关闭内存对齐?
https://blog.csdn.net/a1205137569/article/details/48968699
13,不用 static 怎么计数一个 const 函数被调用了几次?(一般用 static,全局变量来进行计数)
https://blog.csdn.net/sillyboard/article/details/594244(用了 mutable)
14,函数模板与类模板的区别?
https://blog.csdn.net/tingzhushaohua/article/details/75331860
15,C++构造函数参数列表与直接在构造函数中进行初始化有什么区别?
https://www.zhihu.com/question/60546182
16,const 和 define 的区别?
https://blog.csdn.net/yi_ming_he/article/details/70405364
17,类里面成员变量为什么不能用 memset() 来进行设置?会有什么问题?
https://blog.csdn.net/sulongvc/article/details/6064013
18,什么情况下函数里面的参数变量不一样也能实现多态?
还未得到合理解答。
c++ 面试题(C/C++/STL)的更多相关文章
- STL笔试面试题总结(干货)(转)
STL笔试面试题总结 一.STL有哪些组件? STL提供六大组件彼此此可以组合套用: 1.容器容器就是各种数据结构,我就不多说,看看下面这张图回忆一下就好了,从实现角度看,STL容器是一种class ...
- C/C++ 经典面试题汇总
面试题1:变量的声明和定义有什么区别 ? 为变量分配地址和存储空间的称为定义,不分配地址的称为声明.一个变量可以在多个地方声明,但是只在一个地方定义.加入extern修饰的是变量的声明,说明此变量将在 ...
- C++目录
C++ lambda表达式 C++中如何设计一个类只能在堆或者栈上创建对象,面试题 C++之STL总结精华笔记 指针强制类型转换的理解 关于指针类型和指针类型转换的理解 C++继承种类 C++ 单例模 ...
- 面试题总结(三)、《STL源码剖析》相关面试题总结
声明:本文主要探讨与STL实现相关的面试题,主要参考侯捷的<STL源码剖析>,每一个知识点讨论力求简洁,便于记忆,但讨论深度有限,如要深入研究可点击参考链接,希望对正在找工作的同学有点帮助 ...
- 《STL源码剖析》相关面试题总结
原文链接:http://www.cnblogs.com/raichen/p/5817158.html 一.STL简介 STL提供六大组件,彼此可以组合套用: 容器容器就是各种数据结构,我就不多说,看看 ...
- STL标准库面试题(转)
一.vector的底层(存储)机制 二.vector的自增长机制 三.list的底层(存储)机制 四.什么情况下用vector,什么情况下用list 五.list自带排序函数的排序原理 六.deque ...
- [C++] STL相关面试题
(1) 为何map和set的插入删除效率比用其他序列容器高? 因为map和set的内部数据结构是红黑树,它的插入和删除不需做内存的拷贝和移动.(红黑树的插入和删除是log(n)的). (2) 为何每次 ...
- C++常考面试题汇总
c++面试题 一 用简洁的语言描述 c++ 在 c 语言的基础上开发的一种面向对象编程的语言: 应用广泛: 支持多种编程范式,面向对象编程,泛型编程,和过程化编程:广泛应用于系统开发,引擎开发:支持类 ...
- C/C++ 笔试题
/////转自http://blog.csdn.net/suxinpingtao51/article/details/8015147#userconsent# 微软亚洲技术中心的面试题!!! 1.进程 ...
随机推荐
- macbook 下hadoop伪分布式安装
1 准备原材料 1.1 jdk 1.8.0_171(事先安装并配置环境变量HAVA_HOME,PATH) 1.2 Hadoop 2.8.3 2 免密登陆配置(否则安装过程需要不断输入密码) 2.1 ...
- 基于python的unittest测试框架集成到jenkins(Mac)
1.jenkins部分 1.1 安装jenkins jenkins下载地址:https://jenkins.io/download/ 安装步骤,疯狂点击下一步 1.2 打开jenkins服务 在浏览器 ...
- 解决Visual Studio禁止使用strlen函数的问题
问题描述: 在学习C++的复制构造函数以及复制赋值运算符的重载时,需要用到使用C风格的字符串作为引入,由于我用的是VS2015(社区版),在编译时出错.编译器提醒strcpy函数是不安全的,建议改用s ...
- oracle-rman-3
http://blog.csdn.net/leshami/article/details/6032525 rman概述及体系结构 http://blog.itpub.net/23513800/view ...
- java多线程同步器
Java中多线程开发时,离不开线程的分工协作,常用的多线程的同步器有如下几种: 1.CountDownLatch 应用场景:等待一组线程任务完成后在继续执行当前线程. 用法:定义一个CountDown ...
- select 查询
使用as给字段起别名,例如:select name as 姓名 from student; 模糊匹配(like) "_":一个占位符.例子:select * from studen ...
- mvc ajax访问后台时session过期无法跳转到Login页面问题解决
public class BaseController : Controller { protected User UserInfo { set { Session["UserInfo&qu ...
- Firebug 没死,活在 Firefox DevTools 中
伯乐在线转注:2016年12月7日有一条<Firebug 宣布停止开发更新>的资讯,不少朋友误认为以后用不到 Firebug 了.其实在 2015 年 Firebug 已经在着手整合到 F ...
- SoundManager 2 / API Demo and Code Examples
http://www.schillmania.com/projects/soundmanager2/
- 实验-12-JSP简单入门
参考资料 JSP实验参考文件 主要看实验任务书 实验1. 第一个HTML页面与Tomcat 实验内容:任务书中的JSP-实验1. 1.1 EclipseJEE的使用 新建Tomcat Server 新 ...