排序在向量中具有奇数索引的数字

时间:2015-01-09 16:52:32

标签: c++ sorting vector console-application windows-applications

我想在vector中对具有奇数索引的数字进行排序(索引从0开始)。

例如,如果我输入这个数字; 1 6 5 7 3 2 0,程序必须返回:1 2 5 6 3 7 0

我的代码

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int eded, n, _temp;
    size_t i;

    cout << "Nece eded daxil edeceksiniz?" << endl << ">>> ";
    cin >> n;

    vector<int> v_eded;
    v_eded.reserve(n); // n qeder bosh yer ayiriram vektor-da

    cout << "Ededleri daxil edin:" << endl;

    for (int i = 0; i < n; i++)
    {
        cin >> eded;
        v_eded.push_back(eded);
    }

    for (i = 1; i < v_eded.size(); i+=2)
    {
        if (v_eded[i] > v_eded[i+2])
        {
            _temp = v_eded[i+2];
            v_eded[i+2] = v_eded[i];
            v_eded[i] = _temp;
        }
    }

    for (auto j : v_eded)
        cout << j << " ";

    return 0;
}

它编译时没有任何警告和/或错误。插入eded后,我按Enter,程序就会出现此错误:

enter image description here

我无法确定问题所在。请解释一下我的错误以及如何解决这个问题。

最好的问候。

3 个答案:

答案 0 :(得分:1)

您使用

进行了超出限制的访问权限
if (v_eded[i] > v_eded[i+2])

只有i < v_eded.size(),而不是i + 2

答案 1 :(得分:1)

这应该或多或少地做到:

    #include <boost/iterator_adaptors.hpp>

    using namespace std;

    template<class Iter>
    struct by_2_iterator
    : boost::iterator_adaptor<by_2_iterator<Iter>, Iter>
    {
        by_2_iterator(Iter it, Iter limit)
        : by_2_iterator::iterator_adaptor_(it)
        , _limit(limit)
        {}

    private:

        struct enabler {};  // a private type avoids misuse
        friend class boost::iterator_core_access;

        void advance(typename by_2_iterator<Iter>::difference_type n)
        {
            std::advance(this->base_reference(), n * 2);
        }

        void increment()
        {
            auto dist = _limit - this->base_reference();
            if (dist == 1) {
                ++(this->base_reference());
                _was_half = true;
            }
            else {
                this->advance(1);
                _was_half = false;
            }
        }

        void decrement()
        {
            if (_was_half) {
                --(this->base_reference());
                _was_half = false;
            }
            else {
                this->advance(-1);
            }
        }

    private:
        Iter _limit;
        bool _was_half = false;
    };

    template<class Iter>
    by_2_iterator<Iter>
    make_by_2(Iter it, Iter e) {
        return by_2_iterator<Iter>(it, e);
    }


    BOOST_AUTO_TEST_CASE(play_2sort)
    {

        std::vector<int> v { 1, 6, 5, 7, 3, 2, 0 };

        auto b = make_by_2(begin(v)+1, end(v));
        auto e = make_by_2(end(v), end(v));

        std::sort(b, e);

        std::copy(begin(v), end(v), ostream_iterator<int>(cout, ", "));
        cout << endl;



    }

答案 2 :(得分:0)

我刚编辑了代码,并使用了简单的*select sort*算法。

#include <iostream>
#include <vector>

using namespace std;

int main()
{
    int eded, n, _temp, min_i;
    size_t i, j;

    cout << "Nece eded daxil edeceksiniz?" << endl << ">>> ";
    cin >> n;

    vector<int> v_eded;
    v_eded.reserve(n); // n qeder bosh yer ayiriram vektor-da

    cout << "Ededleri daxil edin:" << endl;

    for (int i = 0; i < n; i++)
    {
        cin >> eded;
        v_eded.push_back(eded);
    }

    for (i = 1; i <= n; i += 2) // or you can replace " n " with v_eded.size()
    {
        min_i = i;
        for (j = i; j < n; j += 2)
        {
            if (v_eded[j] < v_eded[min_i])
                min_i = j;

            _temp = v_eded[i];
            v_eded[i] = v_eded[min_i];
            v_eded[min_i] = _temp;
        }
    }

    cout << endl << endl;
    for (auto v : v_eded)
        cout << v << "  ";

    return 0;
}

感谢。