简体   繁体   English

合并2个不同长度的向量并事先对其进行排序,而无需使用sort函数

[英]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? 如何编写此代码,使其在合并前进行排序而不使用sort函数?

#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循环更改为以下内容,以确保que1countque2count不会超过其字符串大小:

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. 也尝试对que1que2使用某些矢量功能。 You're only using vector functionalities for queMerge right now. 您现在仅对queMerge使用矢量功能。

This can be helpful: http://www.cplusplus.com/forum/beginner/98971/ 这可能会有所帮助: http : //www.cplusplus.com/forum/beginner/98971/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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