简体   繁体   中英

Merging 2 vectors of different lengths and sorting them beforehand WITHOUT the sort function

Here is my code so far. Everything works but the sorting and merging. How can I write this so that it sorts before merging without the use of the sort function?

#include <iostream>
#include <vector>

using namespace std;

int main() {
    vector < string > que1;
    vector < string > que2;
    vector < string > queMerge;
    string input;

    cout << "Enter queues" << endl;
    while(input != "ENDQ"){ //This while loop fills up the first vector with the values until the first "ENDQ" is reached
        cin >> input;
        que1.push_back(input);
    }
    que1.pop_back();//This statement removes the "ENDQ" input from the vector changing the vector size to 1 less

    input = ""; //Sets input to nothing so that it can loop through the second while loop for the second queue
    while(input != "ENDQ"){//This while loop fills up the second vector with the values until the first "ENDQ" is reached
        cin >> input;
        que2.push_back(input);
    }
    que2.pop_back();//This statement removes the "ENDQ" input from the vector changing the vector size to 1 less

Here is where I start to have issues with the code and the sorting process.

    int que1count = 0;
    int que2count = 0;
    for (int i = 0; i < (que1.size() + que2.size()); ++i) {
        if(que2.at(que2count) > que1.at(que1count)){
            queMerge.push_back(que2.at(que2count));
            que2count++;
        }
        else{
            queMerge.push_back(que1.at(que1count));
            que1count++;
        }
    }

Everything else from here on out works just fine.

    cout << "que1: " << que1.size() << endl;
    for (int i = 0; i < que1.size(); ++i) {
        cout << que1[i] << endl;
    }
/*
 * The for loop iterates through the first queue and prints out the values indicated at i until the size of the
 * queue is reached
 */

    cout << endl;

    cout << "que2: " << que2.size() << endl;
    for (int i = 0; i < que2.size(); ++i) {
        cout << que2[i] << endl;
    }
/*
 * The for loop iterates through the second queue and prints out the values indicated at i until the size of the
 * queue is reached
 */

    cout << endl;

    cout << "queMerge: " << queMerge.size() << endl;
    for (int i = 0; i < queMerge.size(); ++i) {
        cout << queMerge[i] << endl;
    }
/*
 * The for loop iterates through the queMerge and prints out the values indicated at i until the size of the
 * queue is reached having already been sorted alphabetically earlier in the program
 */

    return (0);
}

To resolve the out-of-range error, simply change the for loop to the one below to make sure that que1count and que2count never goes above their string sizes:

for (int i = 0; i < (que1.size() + que2.size()) && que2count < que2.size() && que1count < que1.size(); ++i)

Now this will compile. However, your merge loop still doesn't do what you intend it to. Try using some vector functionalities for que1 and que2 too. You're only using vector functionalities for queMerge right now.

This can be helpful: http://www.cplusplus.com/forum/beginner/98971/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM