Skip to content

Commit

Permalink
fixed empty vector crash bug.
Browse files Browse the repository at this point in the history
  • Loading branch information
pezy committed Dec 8, 2017
1 parent 6bb4485 commit 434a789
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Strategy/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class BubbleSortStrategy : public ISortStrategy {
}
private:
void _BubbleSort(std::vector<int>& vec) {
if (vec.empty()) return;
using size = std::vector<int>::size_type;
for (size i = 0; i != vec.size(); ++i)
for (size j = 0; j != vec.size()-1; ++j)
Expand All @@ -33,6 +34,7 @@ class QuickSortStrategy : public ISortStrategy {
}
private:
void _QuickSort(std::vector<int>& vec) {
if (vec.empty()) return;
using size = std::vector<int>::size_type;
auto partition = [&vec](size low, size high) {
int pivot = vec[high];
Expand Down Expand Up @@ -62,7 +64,7 @@ class Sorter {

int main()
{
std::vector<int> vec{1, 5, 4, 3, 2, 8};
std::vector<int> vec{};

Sorter::Sort(vec, std::make_shared<BubbleSortStrategy>());
for (int i : vec) std::cout << i << " ";
Expand Down

0 comments on commit 434a789

Please sign in to comment.