Mixture

身未动,心已远

导航

2014年4月1日 #

effective C++ 3 use const whenever possible

摘要: const一直是个我记不太清楚的东西,这一节内容还挺多,希望自己可以记住。首先是const指针的问题,到底指针是const,还是指针指向的东西是const。这里提供了一条很简单的法则,那就是看const出现在星号*的左边还是右边:const在*左边,那就是指向的东西是const,如果在*右边,那就是指针本身是个const。比如char test[] = "test";const char *p = test; //non-const pointer, const datachar* const p = test; //const pointer, non-const dat 阅读全文

posted @ 2014-04-01 10:05 parapax 阅读(209) 评论(0) 推荐(0) 编辑

STL: string:erase

摘要: leetcode: permutation sequence用了https://github.com/soulmachine/leetcode上面康托编码的思路:class Solution {public: string getPermutation(int n, int k) { string s(n,'0'); for(int i=1;i<=n;i++) { s[i-1]+=i; } string res = ""; k = k-1; int base = getFatorial(n... 阅读全文

posted @ 2014-04-01 06:49 parapax 阅读(278) 评论(0) 推荐(0) 编辑

STL: 从reverse到iterator

摘要: leetcode:Next Permutationclass Solution {public: void nextPermutation(vector &num) { auto rfirst = num.rbegin(); auto rlast = num.rend(); auto pivot = next(rfirst); while(pivot!=rlast&&*pivot>=*prev(pivot)) { ++pivot; } if(pivot==rlast) { ... 阅读全文

posted @ 2014-04-01 06:03 parapax 阅读(211) 评论(0) 推荐(0) 编辑

STL: remove

摘要: leetcode:Remove Elementcode from:https://github.com/soulmachine/leetcodeclass Solution {public: int removeElement(int A[], int n, int elem) { return distance(A,remove(A,A+n,elem)); }};又是STL一句话搞定。distance之前已经学过了,正好复习一下:distance(InputIterator first, InputIterator last), 返回的是两个迭代器的距离。remov... 阅读全文

posted @ 2014-04-01 05:45 parapax 阅读(189) 评论(0) 推荐(0) 编辑

STL: vector range constructor, set::insert

摘要: 思路来自https://github.com/soulmachine/leetcode,是4Sum这道题class Solution {public: vector > fourSum(vector &num, int target) { vector > res; if(num.size() > > twoSumBuf; sort(num.begin(),num.end()); for(int i=0;i(i,j)); } } set > setRes; for(si... 阅读全文

posted @ 2014-04-01 05:17 parapax 阅读(312) 评论(0) 推荐(0) 编辑