jeudi 10 mai 2018

how to erase min element from one vector and add to another, while first becames empty?

how to erase min element from one vector and add to another while first becames empty?`
thats not working:/ it looks like selectsort algorithm

#include<iostream>
#include<vector>
using namespace std;
void print(vector<int>&vec) {
    for (int i = 0; i < vec.size(); i++)
        cout << vec[i] << endl;
}


int main()
{
    vector<int>v1{ 458,525,255,336,258 };
    int size = v1.size() - 1;
    vector<int>v2;
    int min = v1[0];
    int i;
    int ind = 0;
    while (v2.size() != size) {
        for (i = 1; i < v1.size(); i++)
        {
            if (v1[i] < min)
            {
                min = v1[i];
                ind = i;
            }
        }
        v2.push_back(min);
        v1.erase(v1.begin() + ind);
    }
    print(v1);
    cout << " " << endl;
    print(v2);

}

Aucun commentaire:

Enregistrer un commentaire