码迷,mamicode.com
首页 > 编程语言 > 详细

我要好offer之 C++大总结

时间:2014-08-19 16:18:04      阅读:359      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   使用   os   io   strong   

0. Google C++编程规范

英文版:http://google-styleguide.googlecode.com/svn/trunk/cppguide.xml

中文版:http://zh-google-styleguide.readthedocs.org/en/latest/google-cpp-styleguide/contents/

 

1. C++函数的林林总总

2. Effective C++学习笔记

(1) 习惯c++,const特性

(2) 构造、析构、赋值、copy and swap

(3) RAII资源管理

(4) 传值or传引用、禁止返回局部对象指针、实现swap

(5) 少转型、异常安全、inline、编译依赖

(6) 继承本质、接口继承、实现继承

 

3. Effective STL学习笔记

(1) 容器:分类、区间操作优于单元素循环操作、容器不是线程安全

(2) vector优于数组、string优于char*、vector的reverse函数、swap空容器技巧

(3) 关联容器:不要使用[]操作,c++11标准hash容器std::unordered_map

(4) 迭代器: 提供越界检查、连续型容器使用 distance在 迭代器切换 idx下标

(5) 算法:多用标准库算法、各种排序相关算法、各种二分查找相关算法

(6) 函数对象:推荐陈硕大大: std::function std::bind替代虚函数

(7) 多使用STL,容器函数优于算法库函数,list的sort函数

 

4. C++ std::string 代码实现

陈硕大大 std::string实现

class Mystring {
    public:
        Mystring() : data_(new char[1]) {
            *data = \0;
        }

        Mystring(const char* str) : data_(new char[strlen(str) + 1]) {
            strcpy(data_, str);
        }

        Mystring(const Mystring& str) : data_(new char[str.size() + 1]) {
            strcpy(data_, str.c_str());
        }

        ~Mystring() {
            delete[] data_;
        }
        
        // 重载赋值,采用copy and swap手法,旧式写法
        Mystring& operator=(const Mystring& str) {
            Mystring tmp(str);
            swap(tmp);
            return *this;
        }

        // 重载赋值,采用copy and swap手法,新式写法
        Mystring& operator=(Mystring& str) {
            swap(str);
            return *this;
        }

        int size() const {
            return (int)strlen(data_);
        }
        const char* c_str() const {
            return data_;
        }

        void swap(Mystring& str) {
            std::swap(data_, str.data_);
        }
    private:
        char* data_;
};

 

5. C++ 智能指针 代码实现

智能指针类与普通指针一样,但它借由自动化内存管理保证了安全性,避免了诸如悬挂指针、内存泄露和分配失败等问题。

智能指针有好几种实现方式,STL和Boost库里都有实现,比如使用句柄类和引用计数方式。

我们现在使用引用计数定义智能指针,智能指针类将一个计数器与类指向的对象相关联。使用计数跟踪该类有多少个对象共享同一指针。

使用计数为0时,删除对象。使用计数有时也称为引用计数(reference count)。

 

使用一个计数变量,并将其置一,每新增一个对象的引用,该变量会加一,移除一个引用则减一

即当对象作为另一对象的副本而创建时,复制构造函数复制指针并增加与之相应的使用计数的值

当对一个对象进行赋值时(=操作符),覆写=操作符,这样才能将一个旧的智能指针覆值给另一指针,旧的引用计数减一,新的智能指针的引用计数则加一

template<typename T>
class SmartPointer {
    public:
        SmartPointer<T>(T* ptr) {
            ref = ptr;
            ref_count = (unsigned*)malloc(sizeof(unsigned));
            *ref_count = 1;
        }

        SmartPointer<T>(SmartPointer<T*>& sptr) {
            ref = sptr.ref;
            ref_count = sptr.ref_count;
            ++(*ref_count);
        }
                
        //  覆写=运算符,这样才能将一个旧的智能指针赋值给另一指针,旧的引用计数减一,新的智能指针的引用计数则加一
        SmartPointer<T>& operator=(SmartPointer<T*> sptr) {
            if (this == &sptr) {
                *this;
            }
            // 若已赋值为某个对象,则移除引用
            if (*ref_count > 0) {
                remove();
            }

            ref = sptr.ref;
            ref_count = sptr.ref_count;
            ++(*ref_count);
            return *this;
        }

        ~SmartPointer<T>() {
            remove();
        }

        T* GetValue() {
            return ref;
        }

    private:
        void remove() {
            --(*ref_count);
            if (*ref_count == 0) {
                delete ref;
                free(ref_count);
                ref = NULL;
                ref_count = NULL;
            }
        }

        T* ref;
        unsigned* ref_count;
};

 

6. C++ 单例模式 代码实现

c++ singleton神文:http://www.cnblogs.com/loveis715/archive/2012/07/18/2598409.html

笔者博文:http://www.cnblogs.com/wwwjieo0/p/3768889.html

 

7. C++ 实现不被继承的类

笔者博文:http://www.cnblogs.com/wwwjieo0/p/3812342.html

 

8. 陈硕大大的C++博文学习 每篇都是经典:D

https://cloud.github.com/downloads/chenshuo/documents/CppPractice.pdf

我要好offer之 C++大总结,布布扣,bubuko.com

我要好offer之 C++大总结

标签:style   blog   http   color   使用   os   io   strong   

原文地址:http://www.cnblogs.com/wwwjieo0/p/3921767.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!