else if statement within two for loops

I have two vectors with different length which I would like to compare.

1
2
3
4
5
6
7
8
9
10
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;}
		
		}
	else{cout<<"Entrys not ident"<<v1[i]<<endl;}
		
		}


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?
Last edited on
What are you actually comparing?

1,2,3
with
1,2,3,4

Is this equal?

Better indentation helps.
1
2
3
4
5
6
7
8
9
10
11
12
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.
Last edited on
So perhaps simplify the code, by using another function to perform the inner search.
1
2
3
4
5
6
for (int i = 0; i < v1.size(); i++) {
  int numFound = findName(v1[i],v2);
  if ( numFound > 0 ) {
  } else {
  }
}

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool track;
for(int i = 0; i<v1.size();i++)//?
	{
          track = false;
	for(int j = 0; j<v2.size() && !track;j++)  
		{
		track |= (v1[i] == v2[j]); 
		
		}
            if(track) {cout<<"Entrys ident"<<v1[i]<<endl;}
	else{cout<<"Entrys not ident"<<v1[i]<<endl;}
		
		}
Last edited on
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 ||).
Last edited on
logical vs bitwise logical operator. || is logical, | is bitwise.

I didn't mean for you to plug that straight in, its an example of how you could do it. You will need to work it into your program.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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 true if 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 ?
Last edited on
@gamma
In your OP line 5 ends with a closing curly bracket '}' -- this is one too much IMO.
Can't you just dump each vector's contents into a set<string> and then use std::set_intersection?
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace 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
Registered users can post here. Sign in or register to post.