0

I am currently writing a code to intersect two sets that are text files. I am new to C++ so there is bad coding. Please tell me what i am missing or mistype in this code. Thanks for helping me.

set<string> file1;
set<string> file2;
set<string> ofile;

string data1[] = { "X.txt"};
string data2[]={"Y.txt"};

file1.insert(data1,data1);
file2.insert(data2, data2);

cout<< "file1 is" << file1<<endl;

set_intersection(file1.begin(),file1.end(),file2.begin(),file2.end(),
    inserter(file1, file1.begin()));

 cout<<"file1 * file2 is" << ofile<< endl;
4
  • What are file1 and file2? Are you trying to read your input data from the files x.txt and y.txt? Commented Mar 16, 2015 at 19:33
  • Initially I am using files that have data and i am trying to make the files into a set then use set_intersection method. Commented Mar 16, 2015 at 19:37
  • So, to try and clarify a bit. You want the intersection of the filenames, or you want the intersection of the data from inside the two files? There's no reading from any file in your code, just two filenames. Commented Mar 16, 2015 at 19:53
  • I would like to intersect the data inside these two files. Commented Mar 16, 2015 at 20:04

1 Answer 1

2

Assuming you want to get integer data from the files x.txt and y.txt, you could do something like this:

typedef int T;

std::ifstream in1("x.txt");
std::ifstream in2("y.txt");

std::set<T> set1{ std::istream_iterator<T>(in1), 
                  std::istream_iterator<T>() };

std::set<T> set2{ std::istream_iterator<T>(in2),
                  std::istream_iterator<T>() };

std::set_intersection(set1.begin(), set1.end(), 
                      set2.begin(), set2.end(),
                      std::ostream_iterator<T>(std::cout, "\n"));

If the contents of the files are something other than integers, you can change the type of T to suit (e.g., if you have a set of words, you might want to use std::string instead of int).

As it stands right now, this just writes the result to standard output. You might want to change that to put the output in some other collection instead.

This also currently puts the initial input data into std::sets. Despite the name set_intersection the data doesn't really need to be in an std::set. It really just needs to be stored in sorted order. You could, for example, put the data into std::vectors, then sort them and (if necessary) use std::unique to ensure unique elements in each vector. I bring this up primarily because it'll typically run faster and use (substantially) less memory.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for explaining, i realize there is so many ways to approach this problem and i got overwhelmed.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.