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.进程 ...
随机推荐
- CocosCreator引擎修改与定制
1.CCGame.js 修改部分数据脚本的加载时机,避免首屏卡顿 // Load game scripts var jsList = config[CONFIG_KEY.jsList]; if (js ...
- python中表示False的一些内置对象
By default, an object is considered true unless its class defines either a __bool__() method that re ...
- 第一天Python
一.开发语言 高级语言:Python Java.PHP 高级语言--字节码(PHP适用于写网页) 低级语言:C.汇编--机器码(底层开发,根本,效率低) 二.Python种类 三.安装
- 【Python】hasattr() getattr() setattr() 使用方法详解
本文转自 https://www.cnblogs.com/cenyu/p/5713686.html hasattr(object, name)判断一个对象里面是否有name属性或者name方法,返回B ...
- awk字符串操作(字符串链接、传入传出shell变量)
1.awk基础 awk的环境变量及其意义 https://blog.csdn.net/snowpay/article/details/52451718 linux awk命令详解 https:// ...
- google的protobuf简单介绍
google的protobuf是一种轻便高效的结构化数据存储格式,在通信协议和数据存储等领域中使用比较多.protobuf对于结构中的每个成员,会提供set系列函数和get系列函数. 但是,对于使用来 ...
- Python安装与Pycharm使用入门
一.安装Python 1.Linux下安装 一般系统默认已安装2.6.6版本,升级成2.7版本, 但 2.6 不能删除,因为系统对它有依赖,epel源里最新的也是2.6版本,所以以源代码的方式安装2. ...
- docker 学习(九) docker部署静态网站
一: dockerfile, 把Dockerfile和myfolder放在一个目录下: FROM httpd:2.4 COPY ./myfolder/ /usr/local/apache2/htdo ...
- java使用jdbc连接oracle(其他数据库类似)
最基本的Oracle数据库连接代码: 1.右键项目->构建路径->配置构建路径,选择第三项“库”,然后点击“添加外部Jar”,选择“D:\Oracle\app\oracle\product ...
- vue展示dicom文件,医疗系统。
环境:vue.webpack.constone 资料来源及文件:https://github.com/GleasonBian/CornerstoneVueWADO 需要下载的模块:cornerston ...