首页 \ 问答 \ 删除两个向量C ++中的相似元素(Deleting like elements in two vectors C++)

删除两个向量C ++中的相似元素(Deleting like elements in two vectors C++)

我试图搜索两个相同的元素的矢量(每个任何大小),然后删除这两个元素。

我的实现如下:

for (int i = vec1.size() - 1; i >= 0; i--) {
     for (int j = 0; j < vec2.size(); j++) {
          if (vec1[i] == vec2[j]) {
             vec1.erase(vec1.begin() + i);
             vec2.erase(vec2.begin() + j);
          }
     }
}

然而,虽然这适用于大多数情况,但我遇到了一些它没有。 这是我通过这些向量迭代的方式,还是我只是这样做错了?


I am trying to search two vectors (each of any size) for elements that are identical and then delete both elements.

My implementation is as follows:

for (int i = vec1.size() - 1; i >= 0; i--) {
     for (int j = 0; j < vec2.size(); j++) {
          if (vec1[i] == vec2[j]) {
             vec1.erase(vec1.begin() + i);
             vec2.erase(vec2.begin() + j);
          }
     }
}

However, while this works for most cases, I am running into some where it doesn't. Is it the way I am iterating through these vectors or am I just going about this all wrong?


原文:https://stackoverflow.com/questions/50051153
更新时间:2022-05-11 07:05

最满意答案

实际上你根本不需要向后迭代。 在这种情况下,您的代码可以是:

for (int i = 0; i < vec1.size(); i++) {
     for (int j = 0; j < vec2.size(); j++) {
          if (vec1[i] == vec2[j]) {
             vec1.erase(vec1.begin() + i);
             vec2.erase(vec2.begin() + j);
          }
     }
}

但等等......我们擦除一个元素后会发生什么? 然后它之后的所有元素的索引都减少了1,所以我们将跳过下一个项目! 要解决这个问题,我们可以添加这个小修改:

             vec1.erase(vec1.begin() + i--);
             vec2.erase(vec2.begin() + j--);
                                       ^^^^

即使我们通过擦除来改变大小,这也会起作用,因为我们正在检查每个循环的vec2的大小! 但是如果我们最终删除vec1的最后一项呢? 我们不会再次比较它的大小,直到我们一直遍历vec2 ,这将是你的vec1 = {2}, vec2 = {2, 2, 2}示例中的一个问题。 为了解决这个问题,我们可以突破内循环并重复检查vec2

把它们放在一起(并将你的下标操作符改为.at()调用,这样我们就可以检查边界了)你会得到:

for (int i = 0; i < vec1.size(); i++) {
     for (int j = 0; j < vec2.size(); j++) {
          if (vec1.at(i) == vec2.at(j)) {
             vec1.erase(vec1.begin() + i--);
             vec2.erase(vec2.begin() + j--);
             break;
          }
     }
}

(在这里看到它: ideone


You don't actually need to iterate backwards at all. In which case your code can be:

for (int i = 0; i < vec1.size(); i++) {
     for (int j = 0; j < vec2.size(); j++) {
          if (vec1[i] == vec2[j]) {
             vec1.erase(vec1.begin() + i);
             vec2.erase(vec2.begin() + j);
          }
     }
}

But wait up...what happens after we erase an element? Then all of the elements after it have their indexes decreased by 1, so we'll skip the next item! To fix that we can add this small modification:

             vec1.erase(vec1.begin() + i--);
             vec2.erase(vec2.begin() + j--);
                                       ^^^^

This will work even when we change the size by erasing, because we're checking the size of the vec2 every loop! But what if we end up erasing the last item of vec1? We don't compare its size again until we've iterated all the way through vec2, which will be a problem in your vec1 = {2}, vec2 = {2, 2, 2} example. To fix that we can just break out of the inner loop and repeat the check on vec2.

Put it all together (and change your subscript operator into .at() calls so we'll have bounds checking) and you get:

for (int i = 0; i < vec1.size(); i++) {
     for (int j = 0; j < vec2.size(); j++) {
          if (vec1.at(i) == vec2.at(j)) {
             vec1.erase(vec1.begin() + i--);
             vec2.erase(vec2.begin() + j--);
             break;
          }
     }
}

(See it in action here: ideone)

相关问答

更多

相关文章

更多

最新问答

更多
  • 如何重新加载下拉列表(How to reload dropdownlist)
  • RStudio:在脚本中保留特殊字符(RStudio: keeping special characters in a script)
  • Powershell的“GetLatest”不会在新的TFS工作区上下载文件(“GetLatest” with Powershell doesn't download files on new TFS workspace)
  • 我如何让JS识别一个由字符组成的数组?(How do I get JS to recognise an array insted of characters?)
  • EF从存储过程中急切加载(EF eager loading from stored procedure)
  • 将输出文件添加到Python扩展(Adding output file to Python extension)
  • 淮北职业技术学院电脑应用专业咋样?
  • 更改默认扩展面板箭头的箭头样式(Change arrow style for default expansion panel arrow)
  • 芜湖计算机(计算机)培训机构(培训班,学校)哪家好
  • 致命错误:使用clang-llvm ASTMatcher时未找到'stddef.h'文件(fatal error: 'stddef.h' file not found when using clang-llvm ASTMatcher)
  • 内容的.NET缓存(Contentful .NET caching)
  • 客户端没有发生WCF回调(WCF callback is not happening in client)
  • 使用friend在全局范围内调用类成员函数会产生27个错误(Calling a Class member function in Global Scope using friend Gives 27 ERRORS)
  • 如何绑定到WPF中的另一个控件属性(How to Bind to Another Control Property in WPF)
  • 南华大学电脑专业,就业好不好
  • 是否存在泄密文件的官方(或常见)文件扩展名或后缀?(Is there an official (or common) file extention or suffix for deflated files?)
  • 在SVM python中只训练一次(Training only once in SVM python)
  • 淘汰自定义绑定光滑js无法正常工作(knockout custom binding for slick js not working)
  • 似乎无法正确地抓住网站“福布斯”(Can't seem to scrape the website “Forbes” properly)
  • 无法使用boto.rds2从describe_instance方法检索有关db实例的信息(Not able to retrieve information about db instances from the describe_instance method using boto.rds2)
  • 转换为英国日期格式问题(Convert to british date format issue)
  • 在表中列出不同的元组(10种方法)(List distinct tuples in a table(SQL query)(10 ways))
  • OrientDB查询比较(OrientDB query compare)
  • 全局变量有什么不好?(What is so bad about global variables? [duplicate])
  • 为什么JavaMail Transport.send()是一个静态方法?(Why is JavaMail Transport.send() a static method?)
  • 获取最近3个Instagram图像张贴在一个地方(Get last 3 instagram images posted in a place)
  • 使用libnfc格式化/读/写NDEF Mifare 1K卡(Format/Read/Write NDEF Mifare 1K Card using libnfc)
  • 阻止谷歌索引特定图像(Block Google from indexing a particular image)
  • 消息模板接收让Dispatcher没有订阅频道(Message Template receive gives Dispatcher has no subscribers for channel)
  • OpenShift:使用自定义节点版本(OpenShift: Use custom node version)