可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I want to sort numbers in vector which has odd-index (index starts from 0
).
For example, if I enter this numbers; 1 6 5 7 3 2 0
, program must returns this: 1 2 5 6 3 7 0
.
My code
#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; }
It compiles without any warning and/or error. After inserting eded
, I'm pressing Enter
and program gives me this error:
I couldn't determine what's the problem. Please, explain me what's the wrong and how to solve this.
Best regards.
回答1:
You have out of bound access with
if (v_eded[i] > v_eded[i+2])
where only i < v_eded.size()
, not i + 2
回答2:
this should more or less do it:
#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; }
回答3:
I just edited my code and I used simple *select sort*
algorithm.
#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; }
Thanks.