Help on comparing two vectors

#include<iostream>
#include<fstream>
#include<string>
#include<vector>
#include<algorithm>
#include<iterator>
#include<cctype>
#include<sstream>

using namespace std;

int main()
{
fstream inFile;
fstream stopwords;
inFile.open("reut2-012.sgm", ios::in);
ofstream outFile("outfile.txt", ios::out);
stopwords.open("stopwords.txt", ios::in);

typedef std::istream_iterator<char> istream_iterator;
std::ifstream file("reut2-000.sgm");
std::vector<char> input;
file >> std::noskipws;
std::copy(istream_iterator(file), istream_iterator(), std::back_inserter(input));

typedef std::istream_iterator<char> istream_iterator;
std::ifstream file1("stopwords.txt");
std::vector<char> input1;
file1 >> std::noskipws;
std::copy(istream_iterator(file1), istream_iterator(), std::back_inserter(input1));
vector<string> whatiwant;
int x;
int y;
x= input.capacity();
y= input1.capacity();

//NOT Matching up. Getting (core dumped)

while(!inFile.eof())
{
for(int w,i = 0; w < x; w++)
{
if(input1.at(i) != input.at(w))
cout<<input.at(w);

else
i++;




}
inFile.close();
outFile.close();
break;
}
return 0;
}
Please post a sample of your input files.
If you're getting "core dumped", you are supposed to load that core in the debugger and see what line caused the problem (and, while you're there, examine the values held by each of your variables).

There are quite a few problems: wrong iterators, redundancies, "while not eof".. here's a cleaned-up version with all the dead code removed and the usable core spruced up:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>

int main()
{
    std::ifstream file("reut2-000.sgm");
    std::istreambuf_iterator<char> beg(file), end;
    std::vector<char> input(beg, end);

    std::ifstream file1("stopwords.txt");
    std::istreambuf_iterator<char> beg1(file1);
    std::vector<char> input1(beg1, end);

    int x = input.size();
    int y = input1.size();
    for(int i=0, w = 0; w < x && i < y; ++w)
        if( input1[i] != input[w] )
            std::cout << input[w];
        else
            ++i;
}

Last edited on
Topic archived. No new replies allowed.