繁体   English   中英

如何比较两个向量中的两个值,然后用C ++更新向量值?

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

感谢前面代码的帮助,伙计们! 我现在正在扩展上一个查询以包含此代码:我遇到了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;
}

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

  if (*ilist1 > *ilist2)

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

 if (*ilist1 < *ilist2)

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

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

您的代码正在测试:

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM