1

Background: General Goal take two sorted files with strings separated by white space, and bring them together in to one sorted file.

Current Goal, to see if i can use the merge function or a similar function to combine two sorted vectors.

I was using http://www.cplusplus.com/reference/algorithm/merge/, to guide my use of the function merge. However, I get the error "No matching function call to 'merge'".

I'm not sure if the merge function will actually do what I am wanting with strings, but I trying to use it to see if it does.

#include <iostream>
#include <fstream>  
#include <vector>
#include <algorithm>    // std::merge, std::sort
#include <string>
using namespace std;

ifstream r_data_file(const string oname)
{
    ifstream ist {oname};
    if (!ist) {
        string error = "cannot open " + oname;
        throw runtime_error(error);
    }
    return ist;
}

void get_words(ifstream& ist, vector<string>& Po)
{
    for (string p; ist >> p;) {
        Po.push_back(p);
    }
}

int main ()
{
    vector<string> file1;
    vector<string> file2;
    ifstream ist = r_data_file("wordlist_1.txt");
    get_words(ist, file1);
    ifstream ist_2 = r_data_file("wordlist_2.txt");
    get_words(ist_2, file2);
    vector<string> file_1n2(file1.size()+file2.size());
    merge(file1, file1.end(), file2, file2.end(), file_1n2);

}

I would appreciate your thoughts, cheers!

1
  • The last parameter to std::merge should be an iterator, as in file_1n2.begin() Commented Mar 1, 2016 at 6:14

1 Answer 1

4

You cannot simply use file1, file2 and file_1n2 as simple pointers(maybe your confusion comes because you use plain arrays in that way). Here merge uses stl iterators, not just a pointer. To fix the problem, use:

merge(file1.begin(), file1.end(), file2.begin(), file2.end(), file_1n2.begin());
2
  • thank you! Does that mean cplusplus.com/reference/algorithm/merge is also wrong?
    – user4643144
    Commented Mar 1, 2016 at 6:31
  • @Jingles No, because in their example first and second are plain arrays, which are used that way(and you use vectors) Commented Mar 1, 2016 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.