Thank-you @Chervil for managing to fathom out what was intended and rewriting the code so that it is possible to understand the algorithm.
Can I ask if it is strictly necessary to erase any of vec_a? Once you have found a minimum to push into vec_b, couldn't you just start the search for the next minimum at the next element of vec_a? Would the following code work?
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
|
#include <vector>
#include <iostream>
#include <algorithm>
int main()
{
std::vector<int> vec_a;
std::vector<int> vec_b;
int count = 0;
int input = 0;
std::cout << "How many numbers? ";
std::cin >> count;
for (int g = 0; g<count; g++)
{
std::cin >> input;
vec_a.push_back(input);
}
std::vector<int>::iterator biter = vec_a.begin();
while (vec_a.end() - biter > 1)
{
biter = std::min_element(biter,vec_a.end());
vec_b.push_back(*biter);
biter++;
}
for (size_t g=0; g<vec_b.size(); g++)
{
std::cout << vec_b[g] << ' ';
}
std::cout << "\n Done \n";
}
|
My second question is for @Enot02. What is the purpose of this little algorithm?
With the line (from the corrected version by @chervil)
while (vec_a.size() > 1)
you always have the situation where a monotonically increasing sequence like
produces output which
inevitably loses the last element:
Is this intended? It looks very unnatural. If you made the simple change to
while (vec_a.size() >= 1)
(that is, change "greater than" to "greater than or equal to") then the output would be
(and similarly for the while statement in my own code).
So my question boils down to: is there some purpose to this intriguing algorithm?