How do I compare two values from two vectors and then update the vector values in C++?

时间:2021-10-05 02:05:36

Thanks for the help on the previous code, guys! I am now expanding the previous query to include this code: I am having the issue of no output from the for loops and if statements. I have to compare strings in two vectors and find if one is a subset of the other; if there is a subset, I must remove it. (the two vectors are actually identical...this is essentially comparing all elements within a vector against each other). Any guidance would be very much appreciated!

感谢前面代码的帮助,伙计们!我现在正在扩展上一个查询以包含此代码:我遇到了for循环和if语句没有输出的问题。我必须比较两个向量中的字符串,并找出一个是另一个向量的子集;如果有一个子集,我必须删除它。 (这两个向量实际上是相同的......这实质上是将向量中的所有元素相互比较)。任何指导将非常感谢!

 //headers: iostream, vector
 //namespace std 
 //main function

//each vector already has content (read from a file) stored in them. Both vectors hold         the same elements.

int c,j; string first_sequence, second_sequence;
vector<string>::iterator ivector1; vector<string>::iterator ivector2;
vector<string> sequence_1; vector<string> sequence_2; vector<string> header;

 //comparison of sequences within each element of vectors

size_t location,x,y,k,s;

k = sequence_1.size(); 
s = sequence_2.size();
for(ivector1 = sequence_1.begin(); ivector1< sequence_1.end(); ivector1++){
 for(ivector2= sequence_2.begin();ivector2<sequence_2.end(); ivector2++){
    *ivector1 = first_sequence; *ivector2 = second_sequence;    

    if(ivect1 != ivect2){
        x = first_sequence.size();
        y = second_sequence.size();

        if(x > y){
            location = first_sequence.find(second_sequence);
                                if (location != -1){
                                    cout << "Deleting the sequence with id: <"     << *ivector2 << endl;
                    sequence_2.erase(ivector2); *ivector2 = "gone";
                                     }  


                }   else if (y > x) {
            location = second_sequence.find(first_sequence);
                                if (location != -1){                             
                    cout << "Deleting the sequence with id: <"<< *ivector1 << endl;
                    sequence_1.erase(ivector1); *ivector1 = "gone";

                                    }   

                         }
    }
 }
}

return 0;
}

3 个解决方案

#1


0  

In every single loop, you're assigning the value of the iterators to the strings first_sequence and second_sequence. You're not ever using the values in the vectors themselves.

在每个循环中,您将迭代器的值分配给字符串first_sequence和second_sequence。你永远不会使用向量本身的值。

#2


2  

You get no output because none of the elements in vector1 are larger than the lowest value you have inserted into vector2. Therefore this if condition that you test

没有输出,因为vector1中的元素都没有大于插入vector2的最低值。因此,如果您测试的条件

  if (*ilist1 > *ilist2)

is never satisfied, hence no cout statement will run. I am willing to bet that if you change it to

永远不会满足,因此没有cout语句会运行。如果你改成它,我愿意打赌

 if (*ilist1 < *ilist2)

you will see output to the terminal/screen. 3 is always < 4.

你会看到输出到终端/屏幕。 3总是<4。

#3


0  

You are only ever testing if values from vector 1 are bigger than values from vector 2.

您只测试向量1的值是否大于向量2的值。

Your code is testing:

您的代码正在测试:

1 > 4
1 > 5
1 > 6
2 > 4
2 > 5
2 > 6
3 > 4
3 > 5
3 > 6

None of these are true, so nothing's getting displayed.

这些都不是真的,所以什么都没有显示出来。

#1


0  

In every single loop, you're assigning the value of the iterators to the strings first_sequence and second_sequence. You're not ever using the values in the vectors themselves.

在每个循环中,您将迭代器的值分配给字符串first_sequence和second_sequence。你永远不会使用向量本身的值。

#2


2  

You get no output because none of the elements in vector1 are larger than the lowest value you have inserted into vector2. Therefore this if condition that you test

没有输出,因为vector1中的元素都没有大于插入vector2的最低值。因此,如果您测试的条件

  if (*ilist1 > *ilist2)

is never satisfied, hence no cout statement will run. I am willing to bet that if you change it to

永远不会满足,因此没有cout语句会运行。如果你改成它,我愿意打赌

 if (*ilist1 < *ilist2)

you will see output to the terminal/screen. 3 is always < 4.

你会看到输出到终端/屏幕。 3总是<4。

#3


0  

You are only ever testing if values from vector 1 are bigger than values from vector 2.

您只测试向量1的值是否大于向量2的值。

Your code is testing:

您的代码正在测试:

1 > 4
1 > 5
1 > 6
2 > 4
2 > 5
2 > 6
3 > 4
3 > 5
3 > 6

None of these are true, so nothing's getting displayed.

这些都不是真的,所以什么都没有显示出来。