C++ 向量::\u M\u范围\u检查异常

C++ 向量::\u M\u范围\u检查异常,c++,vector,C++,Vector,我想实现一些排序算法来比较它们,我坚持使用奇怪的异常 以下是Im排序的代码: //InsertionSort.hpp #ifndef SORT_INSERTIONSORT_HPP #define SORT_INSERTIONSORT_HPP #include <vector> class InsertionSort { public: explicit InsertionSort(std::vector<int> v) : m_vector(std::move

我想实现一些排序算法来比较它们,我坚持使用奇怪的异常

以下是Im排序的代码:

//InsertionSort.hpp
#ifndef SORT_INSERTIONSORT_HPP
#define SORT_INSERTIONSORT_HPP

#include <vector>

class InsertionSort
{
public:
    explicit InsertionSort(std::vector<int> v) : m_vector(std::move(v)){};
    ~InsertionSort() {};

    std::vector<int> save() const;
    void execute();

protected:
    void sort();

private:
    std::vector<int> m_vector;
};

#endif //SORT_INSERTIONSORT_HPP



//InsertionSort.cpp
#include <stddef.h>
#include "InsertionSort.hpp"

std::vector<int> InsertionSort::save() const
{
    return m_vector;
}

void InsertionSort::execute()
{
    sort();
}

void InsertionSort::sort()
{
    int x;
    size_t j;
    for (size_t i = 1; i < m_vector.size(); ++i)
    {
        x = m_vector[i];
        j = i;
        while ((j > 0) && (m_vector[j - 1] > x))
        {
            m_vector[j] = m_vector[j - 1];
            --j;
        }
        m_vector[j] = x;
    }
}
//InsertionSort.hpp
#ifndef分拣插入分拣水电站
#定义排序插入排序HPP
#包括
类插入排序
{
公众:
显式InsertionSort(std::vector v):m_向量(std::move(v)){};
~InsertionSort(){};
std::vector save()常量;
void execute();
受保护的:
无效排序();
私人:
std::向量m_向量;
};
#endif//SORT\u INSERTIONSORT\u水电站
//InsertionSort.cpp
#包括
#包括“InsertionSort.hpp”
std::vector InsertionSort::save()常量
{
返回m_向量;
}
void InsertionSort::execute()
{
排序();
}
void InsertionSort::sort()
{
int x;
尺寸j;
对于(size_t i=1;i0)&(m_向量[j-1]>x))
{
m_向量[j]=m_向量[j-1];
--j;
}
m_向量[j]=x;
}
}
和主要功能

#include <iostream>
#include <random>
#include <vector>
#include <chrono>
#include <memory>


#include "Sorting/InsertionSort.hpp"

int main()
{
    /* random numbers generation  */
    std::mt19937_64 random(std::chrono::system_clock::now().time_since_epoch().count());
    std::uniform_int_distribution<int> distribution(0, 1000);

    std::vector<int> vec;
    for(auto i = 0; i < 100; ++i)
    {
        vec.push_back(distribution(random));
    }

    /* actual sorting */
    std::unique_ptr<InsertionSort> is(new InsertionSort(std::move(vec)));

    is->execute();

    auto result = is->save();
    for(const auto &it : result)
    {
        try
        {
            std::cout << result.at(it) << " "; //here exception appears
        } catch(std::out_of_range exc)
        {
            std::cout << exc.what() << std::endl;
        }
    }
    std::cout << std::endl;

    return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括“排序/插入排序.hpp”
int main()
{
/*随机数生成*/
std::mt19937_64随机(std::chrono::system_clock::now();
标准:均匀分布(0,1000);
std::vec;
用于(自动i=0;i<100;++i)
{
向量推回(分布(随机));
}
/*实际分拣*/
std::unique_ptr是(新的InsertionSort(std::move(vec));
is->execute();
自动结果=is->save();
for(const auto&it:result)
{
尝试
{
std::cout
结果。at(it)
将在
it
的索引处显示向量的元素。如果
it
=
size()
at()
将抛出异常。要打印出向量的内容,只需使用

for(const auto &it : result)
{
    std::cout << it << " ";
}

您对向量的迭代不正确

两种可能的解决办法:

一,

for(size_t i=0;istd::这不是问题,但为什么要使用唯一的指针?是的,这是个好问题。我仍然需要学习如何区分何时应该使用指针还是使用智能指针。
//...
InsertionSort is(std::move(vec));

is.execute();

auto result = is.save();
//...
for (size_t i = 0; i < result.size(); ++i)
{
    std::cout << result.at(i) << " "; //i - is an index
}
for(const auto &value : result)
{
    std::cout << value << " ";
}