Skip to content

Instantly share code, notes, and snippets.

View liuguiyangnwpu's full-sized avatar
💭
I may be slow to respond.

GuiYang liuguiyangnwpu

💭
I may be slow to respond.
View GitHub Profile
@liuguiyangnwpu
liuguiyangnwpu / Learning STL 01.md
Last active August 2, 2016 07:10
How to write a class with STL
class Widget { ... };
vector<Widget> vw;
Widget bestWidget;
vector<Widget>::iterator it =find(vw.begin(), vw.end(), bestWidget);

you should write this

@liuguiyangnwpu
liuguiyangnwpu / Learning STL 02.md
Created August 2, 2016 07:45
How to free the pointer which allocated on the heap, use different method !
  • when using containers of newed pointers, remember to delete the pointers before the container is destroyed.
void doSomething() {
	vector<Widget*> vwp;
	for(int i=0;i<NUMS;++i) {
		vwp.push_back(new Widget);
	}
}
// Widgets are leaked here !
@liuguiyangnwpu
liuguiyangnwpu / STL_Heap_Struct.md
Last active August 3, 2016 11:25
使用STL容器构造堆数据结构
  • 使用STL中的优先级队列进行堆数据结构的建立
priority_queue<Type, Container, Functional>
Type - 数据类型
Container - 保存数据的容器
Functional - 用来指定数据之间的比较规则

STL中的默认的priority_queue是存储类型为vector的大顶堆,使用方式如下
priority_queue<int, vector<int>, less<int>> high_heap;
@liuguiyangnwpu
liuguiyangnwpu / Learning STL 03.md
Last active August 4, 2016 04:26
使用STL中的Map和Hash_Map和Unordered_Map的底层细节区别
  • unordered_map在迭代器遍历时候key,value出现的顺序是未定义
  • 在STL中的Map底层的实现是红黑树,其插入和查找效率较低,对于插入和查找效率较高的HashMapC11推荐使用unordered_map
  • 在MSDN中又这样的描述

Internally, the elements in the unordered_map are not sorted in any particular order with respect to either their key or mapped values, but organized into buckets depending on their hash values to allow for fast access to individual elements directly by their key values (with a constant average time complexity on average).

unordered_map containers are faster than map containers to access individual elements by their key, although they are generally less efficient for range iteration through a subset of their elements.

这段英文按我的理解就是unordered_map的快速插入和快速查找都是没有问题的,只不过对于迭代器遍历的速度恐怕会相比低效一些。

@liuguiyangnwpu
liuguiyangnwpu / Design_DataStruct.md
Last active August 4, 2016 04:52
在数据结构设计中的重要思路,替换原则
  • 对于一个单链表,我们假设这样一种情况,我们不知道单链表的头节点,我们只是知道单链表中某一个节点的指针,现在我们想删除这个节点,我们如何做到?

    • 第一个方案,我们假设一个Flag,确定当我们访问链表中的数据为Flag标志信息的时候,我们可以认为这个节点是我们已经被标志的删除节点;
    • 第二个方案(替换原则),我们可以将该节点的后继节点的信息拷贝到当前的节点上,然后将当前的节点的后继节点删除即可;
  • 在我做leetcode是遇到这样的一个问题,设计一个数据结构,实现插入、删除、随机获取一个数据的均需要咋O(1)的时间完成。

class RandomizedSet {
public:
    /** Initialize your data structure here. */
@liuguiyangnwpu
liuguiyangnwpu / TrieTree.md
Created August 14, 2016 09:33
使用C++创建TrieTree数据结构,也就是所说的字典树结构!
#include <iostream>
#include <vector>

using namespace std;

const int NodeMax = 26;
typedef struct _TrieNode {
    int cnt;
    struct _TrieNode* next[NodeMax];
@liuguiyangnwpu
liuguiyangnwpu / copyDirs.md
Created August 14, 2016 13:50
使用Python拷贝文件夹中的文件,并对文件进行些操作!
#!/usr/bin/env python
# coding=utf-8

import os
import pandas as pd

saveDir = "/home/fighter/imageretrievedata/for_train/"

def deal_cluster(filePaths):
@liuguiyangnwpu
liuguiyangnwpu / install_python.sh
Created November 7, 2016 06:32 — forked from andriisoldatenko/install_python.sh
Install local Python 2.7.10 on CentOS 7
TMP_PATH=~/tmp_install_python
# Versions section
PYTHON_MAJOR=2.7
PYTHON_VERSION=$PYTHON_MAJOR.10
mkdir $TMP_PATH && cd $TMP_PATH
# Update yum and libraries
yum -y update
@liuguiyangnwpu
liuguiyangnwpu / socket-python.md
Last active December 8, 2016 11:18
socket-python.md
  • server
#coding=utf-8

#创建SocketServerTCP服务器:
import SocketServer
from SocketServer import StreamRequestHandler as SRH
from time import ctime
@liuguiyangnwpu
liuguiyangnwpu / shutil_get_terminal_size.md
Created December 20, 2016 11:08
主要是解决pip 安装ipython 后启动不起来,发生了shutil_get_terminal_size ?
/usr/lib/python2.7/site-packages/IPython/utils/terminal.py

from backports.shutil_get_terminal_size import get_terminal_size as _get_terminal_size
from shutil_backports import get_terminal_size as _get_terminal_size