Skip to content

Commit

Permalink
adjust posts
Browse files Browse the repository at this point in the history
  • Loading branch information
feilongjiang committed Jan 23, 2023
1 parent cbd4c41 commit 09bde47
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 61 deletions.
18 changes: 9 additions & 9 deletions _posts/2017-04-15-method-returns-array-ptr-or-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ tags: [c++, array]
下面就介绍几种方法:

## 方法一: 使用类型别名
```C++
```c++
typedef int arrT[10]; // arrT是一个类型别名,表示的类型
// 是含有10个整型的数组
using arrT = int[10]; // arrT的等价声明
Expand All @@ -19,7 +19,7 @@ arrT* func(int i); // func返回一个指向含有10个整数的数组
## 方法二: 声明一个返回数组指针的函数
要想在声明`func`时不适用类型别名,我们必须牢记被定义的名字后面数组的维度:
```C++
```c++
int arr[10]; // arr是一个含有10个整数的数组
int *p1[10]; // p1是一个含有10个指针的数组
int (*p2)[10] = &arr; // p2是一个指针,他只想含有10个整数的数组
Expand All @@ -31,7 +31,7 @@ int (*p2)[10] = &arr; // p2是一个指针,他只想含有10个整数的数
类似于其他数组的声明,`Type`表示元素的类型,`dimension`表示数组的大小。`(*function(parameter_list))`两段的括号必须存在,就像定义`p2`时两段必须有括号一样。如果没有这对括号,函数返回的类型将是指针的数组。

举个具体点的例子,下面这个func函数的声明没有使用类型别名:
```C++
```c++
int (*func(int i)) [10];
```
可以按照以下的顺序来逐层理解该声明的含义:
Expand All @@ -44,15 +44,15 @@ int (*func(int i)) [10];
## 方法三: 使用尾置返回类型
在C++11新标准中海油可以简化上述`func`声明的方法,就是使用**尾置返回类型(trailing return type)**。任何函数的定义都能使用尾置返回,但是这样形式对于返回类型比较复杂的函数最有效,比如返回类型是数组的指针或者数组的引用。位置返回类型跟在形参列表后面并以一个->符号开头。为了表示函数真正的返回类型跟在形参列表之后,我们在本应该出现返回类型的地方放置一个`auto`:
```C++
```c++
// func接受一个int类型的实参,返回一个指针,该指针指向含有10个整数的数组
auto func(int i) -> int(*) [10];
```
因为我们把函数的返回类型放在了形参列表之后,所以可以清楚地看到func函数返回的是一个指针,并且该指针指向了含有10个整数的数组。

## 方法四: 使用decltype
还有一种情况,如果我们知道函数返回的指针将指向哪个数组,就可以使用decltype关键字声明返回类型。例如,下面的函数返回一个指针,该指针根据参数`i`的不同指向两个已知数组中的某一个:
```C++
```c++
int odd[] = {1, 3, 5, 7, 9};
int even[] = {0, 2, 4, 6, 8};
// 返回一个指针,该指针指向含有5个整数的数组
Expand All @@ -65,11 +65,11 @@ decltype(odd) *addPtr(int i)
## 练习
### 1. 编写一个程序,使其返回数组的引用并且该数组包含10个string对象。不要使用尾置返回类型、decltype或者类型别名。
```C++
string (&func(string (&arr)[10])) [10];
```c++
string (&func(string (&arr)[10])) [10];
```
### 2. 为上一题的函数再写三个声明,一个使用类型别名,另一个使用尾置返回类型,最后一个使用decltype关键字。
```C++
```c++
// 使用类型别名
using arrT = string[10];
arrT& func1(arrT& arr);
Expand All @@ -82,7 +82,7 @@ string arrS[10];
decltype(arrS) &func3(arrT& arr);
```
### 3. 修改arrPtr函数,使其返回数组的引用
```C++
```c++
int odd[] = {1, 3, 5, 7, 9};
int even[] = {0, 2, 4, 6, 8};
decltype(odd) &arrPtr(int i)
Expand Down
8 changes: 1 addition & 7 deletions _posts/2017-06-15-cpp-explicit.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ tags: [c++]
在介绍explicit关键字之前,先来了解一下什么是隐式的类类型转换,C++ Primer第五版中的描述如下:
> 如果构造函数只接受一个实参,则它实际上定义了转化为次类类型的隐式转换规则,有时我们把这种构造函数称作**转换构造函数(converting constructor)**
<!--more-->
也就是说编译器允许将构造函数参数类型通过隐式的转换,转化为一个类类型,以便为参数获取正确的类型。

## 转换示例
Expand Down Expand Up @@ -78,9 +77,4 @@ Bar(Foo(42))
## 参考资料
[1]: [Stack OverFlow - What does the explicit keyword mean?](https://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-mean/121163#121163)
End~
---
1: [Stack OverFlow - What does the explicit keyword mean?](https://stackoverflow.com/questions/121162/what-does-the-explicit-keyword-mean/121163#121163)
2 changes: 1 addition & 1 deletion _posts/2017-10-09-bianry-tree.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ comments: true

# 二叉树节点定义

```C++
```c++
/* Definition for a binary tree node*/
struct TreeNode {
int val;
Expand Down
86 changes: 42 additions & 44 deletions _posts/2017-12-29-leetcode-ip-to-cidr.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,40 @@ Given a start IP address `ip` and a number of ips we need to cover `n`, return a

A CIDR block is a string consisting of an IP, followed by a slash, and then prefix length. For example: "123.45.67.89/20". That prefix length "20" represents the number of common prefix bits in the specified range.

<!--more-->

### Example 1

> **Input:** ip = "255.0.0.7", n = 10
**Output:** ["255.0.0.7/32", "255,0.0.8/29", "255.0.0.16/32"]
**Explanation:**
The initial ip address, when convered to binary, look like this (spaces added for clarity):
255.0.0.7 -> 1111111 00000000 00000000 00000111
The address "255.0.0.7/32" specifies all address with a common prefix of 32 bits to the given address,
ie. just this one address.

> The address "255.0.0.8/29" specifies all address with a common prefix of 29 bits to the given address:
255.0.0.8 -> 11111111 00000000 0000000 00001000
Address with common prefix of 29 bits are:
11111111 00000000 00000000 00001000
11111111 00000000 00000000 00001001
11111111 00000000 00000000 00001010
11111111 00000000 00000000 00001011
11111111 00000000 00000000 00001100
11111111 00000000 00000000 00001101
11111111 00000000 00000000 00001110
11111111 00000000 00000000 00001111

> The address "255.0.0.16/32" specifies all address with a common prefix of 32 bits to the given address,
ie. just 11111111 00000000 00000000 00010000

> **Input:** ip = "255.0.0.7", n = 10
> **Output:** ["255.0.0.7/32", "255,0.0.8/29", "255.0.0.16/32"]
> **Explanation:**
> The initial ip address, when convered to binary, look like this (spaces added for clarity):
> 255.0.0.7 -> 1111111 00000000 00000000 00000111
> The address "255.0.0.7/32" specifies all address with a common prefix of 32 bits to the given address,
> ie. just this one address.
>
> The address "255.0.0.8/29" specifies all address with a common prefix of 29 bits to the given address:
> 255.0.0.8 -> 11111111 00000000 0000000 00001000
> Address with common prefix of 29 bits are:
> 11111111 00000000 00000000 00001000
> 11111111 00000000 00000000 00001001
> 11111111 00000000 00000000 00001010
> 11111111 00000000 00000000 00001011
> 11111111 00000000 00000000 00001100
> 11111111 00000000 00000000 00001101
> 11111111 00000000 00000000 00001110
> 11111111 00000000 00000000 00001111
>
> The address "255.0.0.16/32" specifies all address with a common prefix of 32 bits to the given address,
> ie. just 11111111 00000000 00000000 00010000
>
> In total, the answer specifies the range of 10 ips starting with the address 255.0.0.7 .
> There were other representations, such as:
["255.0.0.7/32", ""255.0.0.8/30", "255.0.0.12/30", "255.0.0.16/32"],
but our answer was the shortest possible.

> Also note that a representation beginning with say, "255.0.0.7/30" would be incorrect,
because it includes address like 255.0.0.4 = 11111111 00000000 00000000 00000100
that are outside the specified range.
>
> There were other representations, such as:
> ["255.0.0.7/32", ""255.0.0.8/30", "255.0.0.12/30", "255.0.0.16/32"],
> but our answer was the shortest possible.
>
> Also note that a representation beginning with say, "255.0.0.7/30" would be incorrect,
> because it includes address like 255.0.0.4 = 11111111 00000000 00000000 00000100
> that are outside the specified range.
### Note

Expand All @@ -63,20 +61,19 @@ that are outside the specified range.

比如我们以`255.0.0.7`开始,覆盖30个地址,那么就有:
> 255.0.0.7/32
只有一个IP地址 剩余30 - 1 = 29

> 只有一个IP地址 剩余30 - 1 = 29
>
> 接下来的IP地址是 255.0.0.8,最后8位是: 00001000,还有3位可以更改
则有255.0.0.8/29 可以覆盖8个IP,剩余29 - 8 = 21

> 则有255.0.0.8/29 可以覆盖8个IP,剩余29 - 8 = 21
>
> 8个地址后的IP是255.0.0.16,最后8位是: 00010000,还有4位可以更改
则有255.0.0.16/28 可以覆盖16个IP,剩余21 - 16 = 5


> 则有255.0.0.16/28 可以覆盖16个IP,剩余21 - 16 = 5
>
> 接下来的IP地址是255.0.0.32,最后8位是: 00100000,还有5位可以更改,而剩余的要覆盖的IP只有5个,因此要小于5,否则覆盖的IP范围就会超过给定的个数,因此是4个,即对最后两位进行更改
则有255.0.0.32/30 可以覆盖4个IP,剩余5 - 4 = 1

> 则有255.0.0.32/30 可以覆盖4个IP,剩余5 - 4 = 1
>
> 那么最后一个要覆盖的就是255.0.0.36/32,剩余0个要覆盖
至此可以得到最少的CIDR。
> 至此可以得到最少的CIDR。
### 实现代码

Expand Down Expand Up @@ -153,4 +150,5 @@ vector<string> ipToCIDR(string ip, int range)
}
```
1: https://baike.baidu.com/item/%E6%97%A0%E7%B1%BB%E5%9F%9F%E9%97%B4%E8%B7%AF%E7%94%B1/240168?fr=aladdin&fromid=3695195&fromtitle=CIDR
## REF
1: [Classless Inter-Domain Routing](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing)
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ title: LeetCode 19. Remove Nth Node From End of List
date: 2018-01-09 18:46:44
category: LeetCode
tags: [leetcode, c++]
math: true
---

## Problem
Expand Down

0 comments on commit 09bde47

Please sign in to comment.