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.进程 ...
随机推荐
- 磁盘操作系统 cmd命令
DOS CMD :磁盘操作系统 不区分大小写 **cd \ 根目录 cls 清空屏幕 dir 显示目录 d: 进入D盘 cd 进入目录命令 dir 查看当前目录的文件与目录 del 删除文件 del ...
- Window系统下搭建GIT本地服务器
转载:https://blog.csdn.net/qwer971211/article/details/71156055
- 百度地图 JavaScript API
最近有点懒 项目结尾了 完了好长时间 没有去总结项目中的问题 想了下还是写写吧 这是一个关于百度地图的 网页展示 <!DOCTYPE html><html><head ...
- 【python接口自动化框架-unittest】【一】unittest单元测试框架概念
一.unittst单元测试框架 概念参考:https://docs.python.org/2/library/unittest.html 使用方法:import unittest (引入unittes ...
- C-Language Functions
转自:https://www.postgresql.org/docs/9.6/xfunc-c.html 可以作为学习基于c编写pg extension 的资料 36.9. C-Language Fun ...
- 虚拟机安装及配置(centOs7)
准备工作 a)下载VMware workstation14 b)下载CentOS7 CentOS7 c)下载xshell.xftp 安装参考 分区设置 补充(解决网络IP问题,设置IP,service ...
- 公式推导:【BACF】
[BACF]: Kiani Galoogahi H, Fagg A, Lucey S. Learning Background-Aware Correlation Filters for Visual ...
- 如何在myeclipse中安装spket插件
在web开发中,经常会遇到自动提示,比如jquery.extjs等,在myeclipse写这些代码时需要自动提示,就需要安装spket插件,具体方法见下面 工具/原料 myeclipse spke ...
- c# 状态机实现
c#仿boost statechart的状态机.去年转到unity使用c#,statechart原来的风格蛮爽的,缺点是编译忒慢,在c#则编译根本不是问题. 不一样的地方首先是简单!因为没做一些东西如 ...
- 【RestTemplete】使用RestTemplete传Json或者 {} 报错--解决
https://jira.spring.io/browse/SPR-9220?focusedCommentId=76760&page=com.atlassian.jira.plugin.sys ...