Reputation:
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!
Upvotes: 1
Views: 3743
Reputation: 522
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());
Upvotes: 4