简体   繁体   English

同时检查两个向量中值的出现

[英]Checking occurrence of values within two vectors simultaneously

My goal is find how many times a complex number exists in the two vectors. 我的目标是找出两个向量中复数存在多少次。

wr_den : vector with real part
wi_den : vector with imaginary part
ordem_den[0] : vectors number of elements (in this case is 3)

Example: 例:

wr_den[0] = 1 wi_den[0] = 1
wr_den[1] = 1 wi_den[1] = 1
wr_den[2] = 1 wi_den[2] = 0

Result: 结果:

index 0: 2
index 1: 2
index 2: 1

My code 我的密码

for (it = 0; it < ordem_den[0]; it++)
  {
    times  = 0;
    for(contador = 0; contador < ordem_den[0]; contador++)
     {   
      p = wr_den[it];
      x = wr_den[contador];

      y = wi_den[it];
      t = wi_den[contador];
      if ((p == x) && (t == y))
       {
        times++;
       }
     }
       }

A std::multiset would probably be the thing here, assuming that the perceived problem is O(n^2) time of current code. 假设感知到的问题是当前代码的O(n ^ 2)时间,则std :: multiset可能就是这里的事情。

Just first iterate throught the index positions, putting those numbers in the set. 只是首先遍历索引位置,然后将这些数字放入集合中。 Then iterate again, now checking number of occurrences of each number at each given index position. 然后再次进行迭代,现在检查每个给定索引位置上每个数字的出现次数。

You don't specify the number types used. 您没有指定使用的数字类型。 Be aware that non-integer floating point values may not compare as equal even though they look equal on output. 请注意,即使它们在输出上看起来相等,非整数浮点值也可能不会相等。 That may or may not be a problem. 这可能是问题,也可能不是问题。

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

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