vector param within function?

i have tried to make a function that does the following: given two ranked lists of items (strings), it checks to make sure that each item appears somewhere in both lists. here was the function i created to do this:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void listcheck(vector<string> testvec, vector<string> testvectwo)
{
   int z;
   vector<int> answer;
   for( int i = 0; i < testvec.size(); i++ ) 
   {
     for( int p = 0; p < testvec.size(); p++ ) 
     {
          z=testvec[i]==testvectwo[p];
          z+=z;
          if (z>0){
             answer.push_back(z);      
             break;
             }
             
}
}
for (int t=0; t< answer.size(); t++)
{
cout << answer[t] << " " << endl;
}
}



i declared tihs with void so i could use cout to check whether i get all 1's returned or 1's and 0's. so if i compare a list like (apples, bananas, grapes, pears) to (pears, grapes, bananas, apples) i should get all 1's due to complete commonality...

i have no idea why this doesn't work with defined vectors (listone, listtwo) when i call this function with the statement

 
listcheck(listone, listtwo);


I am not even sure if I can call on vectors in this way inside of functions. Please help me.
You function works perfectly except for
z+=z
There is no need for this line.

I just make a quick project to test the function here is my source.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
    vector<string> v1(4);
    vector<string> v2(4);
    
    
    v1[0] = "apples";
    v1[1] = "bananas";
    v1[2] = "grapes";
    v1[3] = "pears";
    
    v2[0] = "pears";
    v2[1] = "grapes";
    v2[2] = "bananas";
    v2[3] = "apples";
    
    listcheck(v1, v2);

    return 0;
}


I got all ones or matches.
I believe you could also do this in 1 line of code.

Not compiled:

 
set<string>( list1.begin(), list1.end() ) == set<string>( list2.begin(), list2.end() )



Topic archived. No new replies allowed.