随笔分类 -  C++

摘要:条款7辨别使用()与{}创建对象的差别基础知识 目前已知有如下的初始化方式:int x(0);int y = 0;int z{0};int z = {0}; // the same as above 在以“=”初始化的过程中没有调用赋值运算,如下例所示:Widget w1; // default... 阅读全文
posted @ 2015-08-19 22:45 Azurewing 阅读(258) 评论(0) 推荐(0)
摘要:条款6当推断意外类型时使用显式的类型初始化语句基础知识 当使用std::vector的时候,类型推断会出现问题:std::vector features(const Widget& w);// OKbool highPriority = features(w)[5];processWidget(w... 阅读全文
posted @ 2015-08-18 22:48 Azurewing 阅读(312) 评论(0) 推荐(0)
摘要:条款5相对显式类型声明,更倾向使用auto基础知识 auto能大大方便变量的定义,可以表示仅由编译器知道的类型。templatevoid dwim(It b, It e) { while(b != e) { //typename std::iterator_traits::va... 阅读全文
posted @ 2015-08-18 21:08 Azurewing 阅读(277) 评论(0) 推荐(0)
摘要:条款四知道如何看待推断出的类型基础知识有三种方式可以知道类型推断的结果:IDE编辑器编译器诊断运行时输出使用typeid()以及std::type_info::name可以获取变量的类型信息,但是存在一些问题,代码如下:templatevoid f(const T& param) { usin... 阅读全文
posted @ 2015-08-14 21:07 Azurewing 阅读(202) 评论(0) 推荐(0)
摘要:条款三 了解decltype基础知识提供一个变量或者表达式,decltype会返回其类型,但是返回的内容会使人感到奇怪。以下是一些简单的推断类型:const int i = 0; // decltype(i) -> const intbool f(const Widget& w); // declt... 阅读全文
posted @ 2015-08-13 16:41 Azurewing 阅读(233) 评论(0) 推荐(0)
摘要:条款二 了解auto类型推断基础知识除了一处例外,auto的类型推断与template一样。存在一个直接的从template类型推断到auto类型推断的映射三类情况下的推断如下所示:// case 1const auto& rx = x; // rx -> int// case 2auto&& ur... 阅读全文
posted @ 2015-08-12 09:13 Azurewing 阅读(302) 评论(0) 推荐(0)
摘要:条款一 了解模板类型推断基本情况首先定义函数模板和函数调用的形式如下,在编译期间,编译器推断T和ParamType的类型,两者基本不相同,因为ParamType常常包含const、引用等修饰符templatevoid f(ParamType param); // 函数模板形式f(expr); // ... 阅读全文
posted @ 2015-08-11 22:36 Azurewing 阅读(456) 评论(0) 推荐(0)
摘要:1. 运算符重载2. 计算时间:一个运算符重载示例3. 友元1. 友元有三种:友元函数友元类友元成员函数4. 重载运算符:作为成员函数还是非成员函数5. 再谈重载:一个矢量类6. 类的自动转换和强制转换1. 类型转换1 int *p = 10; // type ***2 int *q = (in... 阅读全文
posted @ 2015-03-07 21:07 Azurewing 阅读(207) 评论(0) 推荐(0)
摘要:1. 过程性编程和面向对象编程2. 抽象和类1. 使用类对象的程序都可以直接访问公有部分,但只能通过公有成员函数(或友元函数)来访问对象的私有成员2. 可以在类声明之外定义成员函数,并使其成为内联函数3. 类的构造函数和析构函数1. 接受一个参数的构造函数允许使用赋值语法将对象初始化为一个值4. t... 阅读全文
posted @ 2015-03-06 14:58 Azurewing 阅读(154) 评论(0) 推荐(0)
摘要:1. 单独编译1.1 头文件中常包含的内容:函数原型使用#define或const定义的符号常量结构声明类声明模板声明内联声明1.2 只需将源代码文件加入到项目中,而不用加入头文件。这是因为用#include管理头文件。1.3 避免多次包含同一个头文件1 #ifndef COORDIN_H_2 #d... 阅读全文
posted @ 2015-03-05 13:09 Azurewing 阅读(170) 评论(0) 推荐(0)
摘要:代码: 1 class Solution { 2 public: 3 int atoi(string str) { 4 int num = 0; 5 int sign = 1; 6 const int n = str.size(); 7 ... 阅读全文
posted @ 2015-03-04 15:50 Azurewing 阅读(125) 评论(0) 推荐(0)
摘要:代码: 1 class Solution { 2 public: 3 void nextPermutation(vector &num) { 4 5 const auto first = num.begin(); 6 const auto last = nu... 阅读全文
posted @ 2015-03-04 15:25 Azurewing 阅读(142) 评论(0) 推荐(0)
摘要:代码:暴力算法 1 class Solution { 2 public: 3 int strStr(char *haystack, char *needle) { 4 if (!*needle) 5 return 0; 6 int al... 阅读全文
posted @ 2015-03-03 19:46 Azurewing 阅读(167) 评论(0) 推荐(0)
摘要:代码: 1 1 class Solution { 2 2 public: 3 3 int removeElement(int A[], int n, int elem) { 4 4 int index = 0; 5 5 for (int i = 0;... 阅读全文
posted @ 2015-03-03 10:25 Azurewing 阅读(176) 评论(0) 推荐(0)
摘要:代码:class Solution {public: bool isValid(string s) { string left = "([{"; string right = ")]}"; stack stk; for(auto c : ... 阅读全文
posted @ 2015-03-03 00:11 Azurewing 阅读(115) 评论(0) 推荐(0)
摘要:代码: 1 class Solution { 2 public: 3 bool isPalindrome(string s) { 4 transform(s.begin(), s.end(), s.begin(), ::tolower); 5 auto lef... 阅读全文
posted @ 2015-03-02 22:58 Azurewing 阅读(150) 评论(0) 推荐(0)
摘要:1. History of the C++ Standards1.1 History of the C++ StandardsC++98 -> C++03 -> TR1 -> C++11 -> C++14(书中没有,貌似是最新标准)1.2 Common Questions about the C++... 阅读全文
posted @ 2015-03-02 16:42 Azurewing 阅读(142) 评论(0) 推荐(0)
摘要:算法渣,现实基本都参考或者完全拷贝[戴方勤(soulmachine@gmail.com)]大神的leetcode题解,此处仅作刷题记录。 1 class Solution { 2 public: 3 vector > fourSum(vector &num, int target) ... 阅读全文
posted @ 2015-03-02 14:16 Azurewing 阅读(182) 评论(0) 推荐(0)
摘要:算法渣,现实基本都参考或者完全拷贝[戴方勤(soulmachine@gmail.com)]大神的leetcode题解,此处仅作刷题记录。 1 class Solution { 2 public: 3 int threeSumClosest(vector &num, int target) {... 阅读全文
posted @ 2015-03-01 21:16 Azurewing 阅读(140) 评论(0) 推荐(0)
摘要:算法渣,现实基本都参考或者完全拷贝[戴方勤(soulmachine@gmail.com)]大神的leetcode题解,此处仅作刷题记录。早先AC,现今TLEclass Solution {public: vector > threeSum(vector &num) { vecto... 阅读全文
posted @ 2015-03-01 20:44 Azurewing 阅读(146) 评论(0) 推荐(0)