Cpp_异常机制

C++异常处理机制

1
2
3
4
5
6
7
8
9
10
int bar(int a, int b)
{
return a / b;
}
int main()
{
int result = 0;
result = bar(10, 0);
return 0;
}

以上会产生除0异常:

如果是Release的程序,不处理异常,就会造成闪退。

throw

throw可以是任何东西,一个对象,一个指针,一个值。
只要throw了,那么程序就会中断、跳转,如果有try-catch就走catch,如果没有则进行下一条语句。

1
2
3
4
5
6
7
int bar(int a, int b)
{
if(b == 0)
{
throw e;
}
}

try-catch

类似于switch-case,但是case值不限制类型。可以通过catch(...)代表default。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
int result = 0;
try
{
result = bar(10, 0);
}
catch (const double& e)
{
std::cout << e << std::endl;
}
catch (const int& e)
{
std::cout << e << std::endl;
}
catch (...)
{
std::cout << "Unhandled Exception!" << std::endl;
}
std::cout << result << std::endl;
return 0;
}

noexcept关键字

声明该函数不抛出异常。

1
int bar(int a, int b) noexcept;

但是一旦实际运行时抛出了异常,程序就会直接挂掉。

异常库<stdexcept>

该库下,有两类errors:

  1. Logic Errors
  2. Runtime Errors

std::exception

基本上所有的exception类都继承于std::exception。如out_of_range

std::exception可以拿字符串构造,然后用what()方法打印这个异常信息。

1
2
3
4
5
6
7
8
9
#include <exception>
std::exception bar_exception{ "bar exception!" };

int bar(int a, int b)
{
if(b == 0)
throw bar_exception;
return a / b;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <exception>
int main()
{
int result = 0;
try
{
result = bar(10, 0);
}
catch (std::exception const& e)
{
std::cout << e.what() << std::endl;
}
catch (...)
{
std::cout << "Unhandled Exception!" << std::endl;
}
std::cout << result << std::endl;
return 0;
}

自定义exception

1
2
3
4
5
6
7
8
class BarException : public std::exception
{
public:
char const* what() const override
{
return "bar exception!";
}
};
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
#include <exception>
int bar(int a, int b)
{
if(b == 0)
throw BarException{};
return a / b;
}
int main()
{
int result = 0;
try
{
result = bar(10, 0);
}
catch (std::exception const& e) // 可以接受bar_exception
{
std::cout << e.what() << std::endl;
}
catch (...)
{
std::cout << "Unhandled Exception!" << std::endl;
}
std::cout << result << std::endl;
return 0;
}

实例

异常用在什么地方呢?不能通过函数的返回值来判断正确与否的时候。如下:

string的at(抛异常,检查边界)

有两种:

  1. at位置的字符不能修改

  2. at位置的字符可以修改

  3. 可能会out of range。

  4. 返回的都是引用类型,引用不能为空,所以不能通过返回值是否为空来判断是否错误了,所以得用异常机制。

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
33
34
35
36
37
38
39
40
#include<exception>
class OutOfRange : public std::exception
{
public:
OutOfRange() : std::exception{"MyString: out of range!"}
{
}
}
class MyString
{
public:
char & at(const size_t off)
{
size_t len = strlen(_str);
if(off > strlen - 1)
throw OutOfRange{};
return _str[off];
}
char const & at(const size_t off) const
{
size_t len = strlen(_str);
if(off > strlen - 1)
throw OutOfRange{};
return _str[off];
}
}
int main()
{
if(str)
{
try
{
std::cout << str.at(11) << std::endl;
}
catch (const std::exception &e)
{
std::cout << e.what() << std::endl;
}
}
}

string的[](不抛异常,不检查边界)

与at函数的不同在于,[]是不抛出异常的,因此不检查边界。如果越界,则行为未定义。

1
2
3
4
5
6
7
8
9
10
11
12
class MyString
{
public:
char& operator[](const size_t off) noexcept
{
return _str[off];
}
char const & operator[](const size_t off) const noexcept
{
return _str[off];
}
}

总结

  1. 异常用在什么地方呢?不能通过函数的返回值来判断正确与否的时候。比如返回值是引用,引用没有空值。
  2. 如果函数在内部自己处理了异常,即不会抛出异常,那么最好声明为noexcept。代表别人调用此函数时,别人就不用try-catch了。

刷题_无重复字符的最长子串

内容

原题名叫做:无重复字符的最长子串。给定一个字符串,找出其中不含有重复字符的最长子串的长度。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
示例 1:
输入: s = "abcabcbb"
输出: 3
解释: 因为无重复字符的最长子串是 "abc",所以其长度为 3。

示例 2:
输入: s = "bbbbb"
输出: 1
解释: 因为无重复字符的最长子串是 "b",所以其长度为 1。

示例 3:
输入: s = "pwwkew"
输出: 3
解释: 因为无重复字符的最长子串是 "wke",所以其长度为 3。
请注意,你的答案必须是 子串 的长度,"pwke" 是一个子序列,不是子串。

提示:
0 <= s.length <= 5 * 10^4
s 由英文字母、数字、符号和空格组成

分析

要判断

代码

解1_暴力+哈希

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
33
34
class Solution {
public:
int lengthOfLongestSubstring(std::string s) {
typedef std::unordered_map<char, int> CharIntMap;
CharIntMap charIntMap;
int s_len = s.length();
int longestLen = 0;
for (int i = 0; i < s_len; ++i)
{
for (int j = i; j < s_len; ++j)
{
if (charIntMap[s[j]] == 1)
{
if (j - i > longestLen)
{
longestLen = j - i;
}
charIntMap.clear();
break;
}
else
{
charIntMap[s[j]] = 1;
if (j - i > longestLen)
{
longestLen = j - i;
// this is wrong. should update longest
}
}
}
}
return longestLen;
}
};

如果字符串本来就没有重复的,我们应该也在else中给longest计数,但要注意,应该是j - i + 1与longest比较。

1
2
3
4
5
6
7
8
else
{
charIntMap[s[j]] = 1;
if (j - i + 1 > longestLen)
{
++longestLen;
}
}

效果

虽然正确。但是效率不是特别高,结果如下:

1
2
3
987/987 cases passed (654 ms)
Your runtime beats 5 % of cpp submissions
Your memory usage beats 5.02 % of cpp submissions (148.9 MB)

优化_利用哈希记录的已出现过的字符位置,加速滑动

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 Solution {
public:
int lengthOfLongestSubstring(std::string s) {
typedef std::unordered_map<char, int> CharIntMap;
CharIntMap charIntMap;
int s_len = s.length();
int longestLen = 0;
for (int i = 0; i < s_len; ++i)
{
int j = i;
if (charIntMap[s[j]] == 1)
{
if (j - i > longestLen)
{
longestLen = j - i;
}
charIntMap.clear();
break;
}
else
{
charIntMap[s[j]] = 1;
if (j - i > longestLen)
{
longestLen = j - i;
// this is wrong. should update longest
}
}
}
return longestLen;
}
};

解2_哈希记录已出现过的字符位置,加速滑动

Map表中的value应该存char的标记

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
33
34
class Solution {
public:
int lengthOfLongestSubstring(std::string s) {
typedef std::unordered_map<char, int> CharIntMap;
CharIntMap charIntMap;
int s_len = s.length();
int longestLen = 0;
for (int i = 0; i < s_len; ++i)
{
for (int j = i; j < s_len; ++j)
{
if (charIntMap[s[j]] == 1)
{
if (j - i > longestLen)
{
longestLen = j - i;
}
charIntMap.clear();
break;
}
else
{
charIntMap[s[j]] = 1;
if (j - i > longestLen)
{
longestLen = j - i;
// this is wrong. should update longest
}
}
}
}
return longestLen;
}
};

心得

这个题目属于

总结