Reputation: 3
Basically, I have a vector of chars and an vector of lines, I want to compare the chars to the lines and if every single char in the vector can find a match in the vector of lines. it prints the position, for example. if the vector of lines is ABCDEF,HIJKL and the vector of chars is C,E,F,C the program prints 0,2 0,4 0,5 0,2
I've tried doing this myself but i've run into 2 problems, one. the vector of chars gets printed out of order, and two. if there is an element in the vector of chars that appears twice then it only prints the position once.
https://i.sstatic.net/v6McM.png
That is the output the message file is THIS IS A TEST MESSAGE, this is the file that gets put into a char vector.
the book file is
ABCDE FG
HIJKLMNO
PQRSTUVWXYZ
and each line gets put into the vector of lines
int main(int argc, char *argv[])
string str;
vector <string> bookVector;
vector <char> messageVector;
char c;
ifstream message(argv[2]);
ifstream book(argv[1]);
ofstream outputFile(argv[3], ios::in | ios::binary);
while (getline(book, str))
{
bookVector.push_back(str);
}
while (message.get(c))
{
messageVector.push_back(c);
}
for (int i = 0; i < bookVector.size(); i++)
for (int x = 0; x < bookVector[i].size(); x++)
if (find(messageVector.begin(), messageVector.end(),
bookVector[i][x]) != messageVector.end()) {
cout << "found a match at " << bookVector[i][x] <<
" at positions "<<i<<","<<x<< endl;
}
}
Upvotes: 0
Views: 1153
Reputation: 857
You could do something like this:
#include<iostream>
#include<string>
#include <vector>
int main()
{
std::vector<std::string> book = { {"ABC"},{"DEF"},{"GHI"}, {"BFG"}, {"HELLO"} };
std::vector<char> chars = { 'B', 'G', 'H' };
for (int j = 0; j < chars.size(); j++)
{
for (int i = 0; i < book.size(); i++)
{
size_t offset = 0;
while ((offset = book[i].find(chars[j], offset)) != std::string::npos)
{
std::cout << "Found " << chars[j] << " at " << i << "," << offset << std::endl;
++offset;
}
}
}
std::cin.get();
return 0;
}
Output generated from the above example:
Found B at 0,1
Found B at 3,0
Found G at 2,0
Found G at 3,2
Found H at 2,1
Found H at 4,0
Upvotes: 1