![](https://img-blog.csdnimg.cn/20201014180756925.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
c++
hxj_rain
流浪不是理想主义。
展开
-
Number of Islands (孤岛的数量)
问题: Given a 2d grid map of '1's (land) and '0's (water), count the number of islands. An island is surrounded by water and is formed by connecting adjacent lands horizontally or vertically. You原创 2016-06-27 21:58:53 · 1449 阅读 · 0 评论 -
STL stack 的实现
stack 结构的特点: stack 是一种先进先出(First In Last Out) 的数据结构,它只有一个出口,stack 允许新增元素,移除元素,取的顶端的元素。stack 以底部容器完成其所有的工作,而具有这种 “修改某物接口,形成另一种风貌”之性质者,称为 adapter (配接器)。所以 stack 往往不被称为 container ,而往往被称为 container adapte原创 2016-12-15 17:33:47 · 427 阅读 · 0 评论 -
STL List 的实现
list 数据结构的实现1.双链表,始终存在着一个空节点。 2.迭代器的实现,内嵌型别为 bidirectional_iterator 3.节点的结构体 4.list 的实现 5.为节点单独写的空间配置器 simple_alloc #include <iostream>#include <memory>using namespace std;template<class _Tp, cla原创 2016-12-14 23:40:54 · 297 阅读 · 0 评论 -
STL heap 实现
先引用 《STL 源码剖析》(作者: 侯捷)的原话:heap 并不属于 STL 的容器组件, 它是一个幕后英雄 ,扮演 priority queue 的助手。 binary heap 是一种 完全二叉树(completely binary tree),也就是说,整棵 binary tree 除了最底层的叶子节点以外,都是填满的 。而最底层的叶子节点由左到右又不得有空隙。 完全二叉树没有任何的原创 2016-12-17 16:37:31 · 331 阅读 · 0 评论 -
effective c++ 条款13
为了防止资源泄露,请使用RAII (resource Acquisition is initialization),他们在构造函数中或的资源并在析构函数中 释放资源。应用模板(类): shared_ptr ; shared_ptr 的构造函数已经声明为 explicit ,防止隐式类型转换。用例:#include<iostream>#include <string >#include <me原创 2016-12-08 16:37:19 · 427 阅读 · 0 评论 -
const 的简要介绍
使用const使把握一个 原则不能让一个指向非常量的指针或者引用 来指向一个常量的对象贴代码: 常量的赋值 int i =10;//正确 const int b=i;//正确 const int *bb = &i;//正确 int const *bbb= &i;//正确 const int &bbbb = i;//正确 常量给其他的类型赋值 (容原创 2016-10-12 21:27:43 · 301 阅读 · 0 评论 -
allocator 类的使用
前段时间,重写vector 时就曾经知道,内存分配和构造函数要分开,析构函数和释放内存要分开,但是没有当回事。今天忽然又意识到这个问题。所以了解了一下allocator类。allocator 可以解决 new和delete在上述问题上的尴尬。代码如下: allocator<string > alloc; //分配string的alloctor对象 auto const p= alloc原创 2016-10-12 19:42:14 · 263 阅读 · 0 评论 -
使用 STL vector中的注意事项以及重写vector时的注意事项
使用 STL vector 时应注意的问题:第一:如果可用空间已经满了,那么下一次往里面压数据会重新分配空间,就会造成迭代器的失效。第二:erase成员函数的使用注意事项,其实是对这个指针指向的空间的重新赋值,并且返回这个指针。/* 删除第5个元素 */vector vec(20,0);for (int i=0;i<vec.size();) if(i==5) era原创 2016-07-15 23:16:18 · 946 阅读 · 0 评论 -
实现vector容器
背景:在c++ STL中 每一种容器都有其自己对应的迭代器实现。迭代器也成为了算法和容器之间的桥梁。今天先模拟一下vector 容器,以及对应的迭代器。想法:一、vector实质是可变长的数组(空间连续),所谓的可变长其实是伪可变长。为了实现可变长,vector的工作实质:1、初始分配空间大小时,分配按实际需求分配。2、加入分配空间已经使用满了,再执行一次push_back会发原创 2016-07-14 15:00:14 · 539 阅读 · 0 评论 -
Maximum Subarray(求数组的子数组之和的最大值)
问题:求数组的子数组之和的最大值算法:1、求得数组 的第一个正数的下标 和 最后一个正数的下标2、通过下标判断,如果该数组的值都是负值,那么从数组中取最大值输出。否则进行下一步。3、将相邻的正数合并,相邻的负数合并。形成正负相间的数组4、将第一个正数和下一个负数相加,如果结果大于零,那么加到下一个正数里面,如果小于零,那么不加到下一个正数里面。遍历整个正负相间的原创 2016-06-24 15:20:10 · 548 阅读 · 0 评论 -
Summary Ranges
问题描述:Given a sorted integer array without duplicates, return the summary of its ranges.For example, given [0,1,2,4,5,7], return ["0->2","4->5","7"].算法:1、如果数组为空,则返回空,如果数组只有一个值,则返回该值得字符串形式原创 2016-06-25 15:18:49 · 377 阅读 · 0 评论 -
Container With Most Water
问题描述:Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i原创 2016-06-25 16:16:17 · 230 阅读 · 0 评论 -
STL queue 的实现
queue 是一种先进先出(First In First Out) 的数据结构,它有两个出口。queue 允许新增元素,移除元素,从最低端加入元素,取的顶端的元素。类似于 stack ,queue 也是一种 container adapter。#include <iostream>#include <list>using namespace std;template<class T, class原创 2016-12-15 17:36:50 · 787 阅读 · 0 评论