As a result, I get the error "else without previous if" which is probably caused by the for loops.
So is there a way to compare the two vectors without manipulating them?
for (int i = 0; i < v1.size(); i++) //?
{
for (int j = 0; j < v2.size(); j++) {
if (v1[i] == v2[j]) {
cout << "Entrys ident" << v1[i] << endl;
}
}
// Do you now see why this is an out of place else?
else {
cout << "Entrys not ident" << v1[i] << endl;
}
}
As would making sure the control structure is in place beforehand.
1 2 3 4 5 6 7 8
for (int i = 0; i < v1.size(); i++) //?
{
for (int j = 0; j < v2.size(); j++) {
if (v1[i] == v2[j]) {
} else {
}
}
}
I'm comparing numbers and characters like
hd12459, jh3039,po3232
with
jk2321,jh3039,jdp233.
I know why it is an out of place else, I just don't know how to fix it.
As a result I want to print something like:
"hd12459 is not found in v2"
"jh3039 is found in v2"
...
So if I put the else inside the second for loop it will print some values multiple times:
Lets asume i is 0 and j is 0:
Therefore the result is:
"hd12459 is not found in v2"
In the next step i ist still 0 but j is 1 which brings the same result:
"hd12459 is not found in v2."
I'm looking for a way to print 8 (or whatever number) times : "...is found/not found in v2" when 8 is the size of the vector v1.
There may be a way to do it with a break or something but another easy way would be to track it in a boolean.
This would be a very nimbly way but your code did not work for me. I get the first phrase (Entrys ident) but not the value. It also stops running after ther first time it executes the loop.
I also don't understand what track |= (v1[i] == v2[j]); does since I have never seen the | operator itself (just ||).
what it says:
for all of v1
{
track is false.
for all of v2 up to UNTIL IT FOUND A MATCH (take the track term out to make it do all of the matches)
{
track = track OR (v1 == v2 )
that is, if track is false, it becomes trueif v1 == v2 for the current entry.
}
after finding a match or not, print the result (found or not found, or whatever)
}
if you want to get multiples in the inner loop it looks like this.
bool track;
for(int i = 0; i<v1.size();i++)//?
{
track = false;
for(int j = 0; j<v2.size() ;j++)
{
if(v1[i] == v2[j])
{
track = true;
cout<<"Entrys ident"<<v1[i]<<endl;
}
}
if(!track)
{cout<<"Entrys not ident"<<v1[i]<<endl;}
}
does that cover it?
or do you see how the idea works at least? you should be able to adjust it to your need ?
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
usingnamespace std;
int main()
{
vector<string> A = { "hd12459", "jh3039", "po3232" };
vector<string> B = { "jk2321" , "jh3039", "jdp233" };
for ( auto e : A ) cout << e << ( find( B.begin(), B.end(), e ) != B.end() ? " is " : " is not " ) << "found in B\n";
}
hd12459 is not found in B
jh3039 is found in B
po3232 is not found in B