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.进程 ...
随机推荐
- CORS(跨域资源共享)的防御机制
一.为什么会出现CORS: 浏览器的同源策略给WEB开发人员带来了巨大的痛苦,信息的交互共享本来就是网络的意义.所以妥协之后出现了CORS. 二.技术原理: 1.简单跨域: (1)方法要求:只能是GE ...
- 《从Lucene到Elasticsearch:全文检索实战》学习笔记三
今天我给大家讲讲倒排索引. 索引是构成搜索引擎的核心技术之一,它在日常生活中是非常常见的,比如我看一本书的时候,我首先会看书的目录,通过目录可以快速定位到具体章节的页码,加快对内容的查询速度. 文档通 ...
- 第三章泛型集合ArrayList 和Hashtable
第三章泛型集集合 ArrayList 变量名 = new ArrayList(); //相当与一个容器 他的执行using 是 using System.Collections; 变量名.ADD( ...
- Centos7 下安装VMware tools
1:先在虚拟机点击安装VMware Tools 2:然后挂载 mount /dev/cdrom /mnt 3:进入/mnt,可以看到有 4:拷贝VMwareTools到其他 ...
- fullCalendar使用经验总结
fullCalendar日历控件官方网址: https://fullcalendar.io/ 1.需要引入的文件 <link href="~/assets/fullcalendar-3 ...
- ubuntu python3和python2切换脚本
最近在ubuntu上开发较多,有些工具只能在python2运行,而开发又是在python3上做的开发,所以写个脚本方便在python2和python3之间切换. 切换成python2的文件usepy2 ...
- git项目提交后执行添加忽略操作
需要删除文件暂存区中的忽略文件 git rm -r --cached 需要忽略的已提交文件或文件夹 eg: git rm -r --cached target/
- 黄聪:如何扩展Chrome DevTools来获取页面请求
1. Chrome DevTools Extension 熟悉React的同学,可能对React Developer Tools并不陌生, 刚看到的时候,我也觉得很神奇, 因为React De ...
- android 获取对权限的选择
一般是第三方软件拦截,再次提示给用户,确认权限的,如360等.(PS 没有设置权限的app 是会崩溃的 ,而是不是弹出权限确认,因为你都没设置这个权限)看了网上很多,确切说没有一个适合我的. 其实用 ...
- finstrument-functions
2017-12-03 23:59:16 参考 如何快速地在每个函数入口处加入相同的语句? https://www.zhihu.com/question/56132218 做个存档 scj@scjCom ...