字符串截取

IP格式字符串截取前两段

本算法可以适用于192.178.1.1或者1.20.0.7557等格式字符串

1
=LEFT(C1,IFERROR(FIND(".",C1,FIND(".",C1)+1)-1,100))
阅读全文 »

标题

Markdown语法的标志可以使用#表示,共支持6级标题

1
2
3
4
5
6
# 一级标题
## 二级标题
### 三级标题
#### 四级标题
##### 五级标题
###### 六级标题

为了美观,也可以采用下面这种对称形式

1
2
3
4
5
6
# 一级标题 #
## 二级标题 ##
### 三级标题 ###
#### 四级标题 ####
##### 五级标题 #####
###### 六级标题 ######

列表

无序列表 使用星号(*)、加号(+)或是减号(-)作为列表标记,三种标记符产生的效果完全相同,比如:

1
2
3
* Red
* Blue
* Green

上述文字产生的列表如下:

阅读全文 »

常用全局配置

git配置使用git config来完成。

配置用户信息

要配置成自己的真实用户名(姓名全拼)和邮箱

1
2
git config --global user.name "John Doe"
git config --global user.email "johndoe@example.com"

配置别名

比如git checkout通过配置别名,可以使用git co来达到git checkout的效果

1
2
3
4
git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status

配置编辑器vim

1
git config --global core.editor vim
阅读全文 »

Kernels 2.6.16 and newer provide a mechanism to have the kernel drop the page cache and/or inode and dentry caches on command, which can help free up a lot of memory. Now you can throw away that script that allocated a ton of memory just to get rid of the cache…

To use /proc/sys/vm/drop_caches, just echo a number to it.

To free pagecache:

1
echo 1 > /proc/sys/vm/drop_caches

To free dentries and inodes:

1
echo 2 > /proc/sys/vm/drop_caches

To free pagecache, dentries and inodes:

1
echo 3 > /proc/sys/vm/drop_caches

This is a non-destructive operation and will only free things that are completely unused. Dirty objects will continue to be in use until written out to disk and are not freeable. If you run “sync” first to flush them out to disk, these drop operations will tend to free more memory.

参考网站:

阅读全文 »

对于类的普通成员函数和静态成员函数,定义函数指针的方法是不同的,如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
class Test {
public:
void func_normal() { //普通成员函数
std::cout << __func__ << std::endl;
}
static void func_static() { //静态成员函数(static)
std::cout << __func__ << std::endl;
}
};

//g++ -std=c++11 to enable std::function
static void foo(std::function<void()> pf)
{
pf();
}

int main(int argc, char* argv[])
{
/* normal function pointer */
void (Test::*pfunc_n)() = NULL;
pfunc_n = &Test::func_normal;
Test t;
(t.*pfunc_n)();
foo(std::bind(&Test::func_normal, &t));

/* static function pointer */
void (*pfunc_s)() = NULL;
pfunc_s = &Test::func_static;
pfunc_s();

return 0;
}

如果一个非静态成员函数指针作为函数参数,则只能把对象传入函数中,然后通过对象调用函数指针。在C++11中引入了function,其本质也是把类传入了函数。

阅读全文 »

类的构造

1
User(int id, string name, string phone);

可以采用如下格式来进行类实例的创建:

1
2
3
4
5
6
7
User user = User(1, "Jone", "123456");
User user(1, "Jone", "123456");
User* user = new User(1, "Jone", "123456");

User user = { 1, "Jone", "123456" }; //C++ 11
User user{ 1, "Jone", "123456" }; //C++ 11
User* user = new User{ 1, "Jone", "123456" }; //C++ 11

如果User类有默认构造函数,则下面方式可以直接构造一个类:

1
2
User user;  //此处不是变量的声明,而是类实例的创建
User* user = new User; //此处也是使用默认构造函数

对于只有一个参数的构造函数,可以采用赋值的方式创建一个类实例:

1
A a = 10;  //构造函数为 A(int id);

这种形式容易带来问题,可以使用explicit来修饰只有一个参数的构造函数,就能禁用这种类的构造写法。

构造函数分类

阅读全文 »

内存分段示意图

栈(stack)

栈又称堆栈, 是用户存放程序临时创建的局部变量,也就是说我们函数括弧“{}”中定义的变量(但不包括static声明的变量,static意味着在数据段中存放变量)。除此以外,在函数被调用时,其参数也会被压入发起调用的进程栈中,并且待到调用结束后,函数的返回值也会被存放回栈中。由于栈的先进后出特点,所以栈特别方便用来保存/恢复调用现场。从这个意义上讲,我们可以把堆栈看成一个寄存、交换临时数据的内存区。

堆(heap)

堆是用于存放进程运行中被动态分配的内存段,它的大小并不固定,可动态扩张或缩减。当进程调用malloc等函数分配内存时,新分配的内存就被动态添加到堆上(堆被扩张);当利用free等函数释放内存时,被释放的内存从堆中被剔除(堆被缩减)

BSS段(bss segment)

BSS(Block Started by Symbol)属于静态内存分配,通常是指用来存放程序中未初始化的全局变量和静态变量的一块内存区域。特点是可读写。

BSS段在磁盘上不是真的占用变量大小的空间,它仅仅记录了变量所需要的大小(占位符),也就是说,在该段中记录了所有未初始化全局变量与局部静态变量的大小总和,至于每个变量的大小则存储在符号表的size属性中。当可执行文件加载运行前,会为BSS段中的变量分配足够的空间并全部自动清零(因此,才有未初始化的全局变量的值为0的说法)。

BSS段主要是为了节省可执行文件在磁盘上所占的空间,对未初始化的大型数组的节省效率比较明显。

默认情况下,编译器会把初始化值为0的变量(比如static int a = 0)放在BSS段。但可以通过“#pragma explicit_zero_data on”把初始值为零的变量放到data段,而不是bss段。

阅读全文 »

%0 c hello.c i hello.i c->i 预处理 -E s hello.s i->s 编译 -S o hello.o s->o 汇编 -c e hello o->e 链接

示例如下:

1
2
3
4
gcc -E hello.c -o hello.i
gcc -S hello.i -o hello.s
gcc -c hello.s -o hello.o
gcc hello.s -o hello
阅读全文 »

C++


Python


Android


阅读全文 »

Conditional Logic on Files

  • ‘-a’ file exists.
  • ‘-b’ file exists and is a block special file.
  • ‘-c’ file exists and is a character special file.
  • ‘-d’ file exists and is a directory.
  • ‘-e’ file exists (just the same as -a).
  • ‘-f’ file exists and is a regular file.
  • ‘-g’ file exists and has its setgid(2) bit set.
  • ‘-G’ file exists and has the same group ID as this process.
  • ‘-k’ file exists and has its sticky bit set.
  • ‘-L’ file exists and is a symbolic link.
  • ‘-n’ string length is not zero.
  • ‘-o’ Named option is set on.
  • ‘-O’ file exists and is owned by the user ID of this process.
  • ‘-p’ file exists and is a first in, first out (FIFO) special file or named pipe.
  • ‘-r’ file exists and is readable by the current process.
  • ‘-s’ file exists and has a size greater than zero.
  • ‘-S’ file exists and is a socket.
  • ‘-t’ file descriptor number fildes is open and associated with a terminal device.
  • ‘-u’ file exists and has its setuid(2) bit set.
  • ‘-w’ file exists and is writable by the current process.
  • ‘-x’ file exists and is executable by the current process.
  • ‘-z’ string length is zero.

示例:

1
2
3
4
5
6
7
if [ -f ${mypath} ];then
echo "file exist"
fi

if [ "${mypath}" != "" ] && [ -f ${mypath} ];then
echo "mypath is not empty and file exist"
fi

参考网站:

阅读全文 »
0%