C++ 有什么好方法可以移除向量或deque中的元素吗?提供一些示例代码非常受欢迎

C++ 有什么好方法可以移除向量或deque中的元素吗?提供一些示例代码非常受欢迎,c++,stl,C++,Stl,例如,我好2个向量,说当前向量,要删除的向量。是否有一些好方法可以删除当前和当前两种格式中出现的元素 这是我的方法。我想它看起来不太好。所以请帮帮我。多谢各位 #include <iostream> #include <vector> using namespace std; int main(){ vector <double> current; current.push_back(1.1); current.push_back(2.1);

例如,我好2个向量,说当前向量,要删除的向量。是否有一些好方法可以删除当前和当前两种格式中出现的元素

这是我的方法。我想它看起来不太好。所以请帮帮我。多谢各位

#include <iostream>
#include <vector>
using namespace std;
int main(){
  vector <double> current;
  current.push_back(1.1);
  current.push_back(2.1); 
  current.push_back(3.1); 
  current.push_back(4.1); 
  current.push_back(5.1); 
  current.push_back(6.1); 
  current.push_back(7.1); 
  current.push_back(8.1);
  vector <double> to_delete;
  to_delete.push_back(2.1);
  to_delete.push_back(5.1);
  for(int i = 0;i<to_delete.size();i++){
    int loc = -1;
    for(int j = 0; j < current.size();j++)
    {
      if(current[j]==to_delete[i]){
        loc = j;
        break;
      }
    }
    if(loc >= 0){
      current.erase(current.begin()+loc);
    }
  }

  for(int i = 0;i < current.size();i++){
    cout << current[i]<< endl;
  }
}
#包括
#包括
使用名称空间std;
int main(){
矢量电流;
电流。推回(1.1);
电流。推回(2.1);
电流。推回(3.1);
电流。推回(4.1);
电流。推回(5.1);
电流。推回(6.1);
电流。推回(7.1);
电流。推回(8.1);
要删除的向量;
删除。向后推(2.1);
删除。向后推(5.1);
对于(int i=0;i=0){
当前。擦除(当前。开始()+loc);
}
}
对于(int i=0;icout最简单的方法可能是使用
std::set_difference
构造一个新的向量(必须对两个向量进行排序才能工作):

std::vector diff;
std::set_差异(current.begin(),current.end(),
要删除.begin(),要删除.end(),
标准:背部插入器(差异);

最简单的方法可能是使用
std::set_difference
构造一个新的向量(必须对两个向量进行排序才能工作):

std::vector diff;
std::set_差异(current.begin(),current.end(),
要删除.begin(),要删除.end(),
标准:背部插入器(差异);
您可以使用而不是手动循环来查找应删除元素的位置

std::vector<double>::iterator del =
      std::find(current.begin(), current.end(), to_delete[i]);
if (del != current.end())
    current.erase(del);
std::vector::iterator del=
std::find(current.begin()、current.end()、to_delete[i]);
如果(del!=current.end())
当前删除(del);
您可以使用而不是手动循环来查找应删除元素的位置

std::vector<double>::iterator del =
      std::find(current.begin(), current.end(), to_delete[i]);
if (del != current.end())
    current.erase(del);
std::vector::iterator del=
std::find(current.begin()、current.end()、to_delete[i]);
如果(del!=current.end())
当前删除(del);

首先,您应该注意,在您的特定示例中,
x.1
不是以基2浮点格式完全表示的,并且尝试比较这些值可能会由于编译器生成的代码而导致不平等,即使它看起来应该工作

<>下,是代码>向量真的是正确的容器吗?如果你需要这样的任意删除,你考虑使用<代码> SET>代码>还是<代码> MultStudio?< /P> 如果需要随机访问容器,则可以使用@Space\u C0wb0y建议的
set\u difference
方法或使用
std::remove

for(int i = 0;i<to_delete.size();i++)
{
    current.erase(std::remove(current.begin(), current.end(), to_delete[i]), current.end());
}

for(int i=0;i首先,您应该注意,在您的特定示例中,
x.1
不是以基二浮点格式完全表示的,尝试比较这些值可能会由于编译器生成的代码而导致不平等,即使它看起来应该工作

<>下,是代码>向量真的是正确的容器吗?如果你需要这样的任意删除,你考虑使用<代码> SET>代码>还是<代码> MultStudio?< /P> 如果需要随机访问容器,则可以使用@Space\u C0wb0y建议的
set\u difference
方法或使用
std::remove

for(int i = 0;i<to_delete.size();i++)
{
    current.erase(std::remove(current.begin(), current.end(), to_delete[i]), current.end());
}

for(int i=0;i这类东西有什么问题:

std::vector<double>::iterator iter
    = std::find_first_of( current.begin(), current.end(),
                          to_delete.begin(), to_delete.end() );
while ( iter != current.end() ) {
    iter = std::find_first_of( current.erase( iter ), current.end(),
                               to_delete.begin(), to_delete.end() );
}
(如果你必须这样做,这确实是一个有效的建议 在许多不同的地方,或者如果您可以使用boost::bind to 生成适当的IsElementOf,但这很困难,因为 编译器不知道从哪里开始计算
键入,因此您必须在某个地方显式指定。)

以下内容有什么问题:

std::vector<double>::iterator iter
    = std::find_first_of( current.begin(), current.end(),
                          to_delete.begin(), to_delete.end() );
while ( iter != current.end() ) {
    iter = std::find_first_of( current.erase( iter ), current.end(),
                               to_delete.begin(), to_delete.end() );
}
(如果你必须这样做,这确实是一个有效的建议 在许多不同的地方,或者如果您可以使用boost::bind to 生成适当的IsElementOf,但这很困难,因为 编译器不知道从哪里开始计算

类型,所以您必须在某个地方显式指定。)

没有检查代码的有效性,但方法对我来说很干净。没有检查代码的有效性,但方法对我来说很干净。这假定原始向量已排序(它们在示例中看起来是,但只是说“).1虽然您应该注意两个范围都必须先排序。+1,这比我开始使用std::remove编写的内容要好。如果感谢您提供了好的解决方案,对不起,我忘了提到两个向量都没有排序。@Monkey:您可以使用
std::sort(current.begin(),current.end())对向量进行排序;
。这假定原始向量已排序(在示例中,它们似乎是这样的,但只是说“).1虽然您应该注意两个范围都必须先排序。+1,这比我开始使用std::remove编写的内容要好。如果感谢您提供了好的解决方案,对不起,我忘了提到两个向量都没有排序。@Monkey:您可以使用
std::sort(current.begin(),current.end())对向量进行排序
。您可以使用
容器::常量引用
而不是
容器::value\u type const&
-它就是为了实现这一点。@Space\u C0wb0y很好。我承认我习惯于对类型和迭代器进行赋值,并且几乎忽略容器中的其他typedef。您可以使用
容器::常量引用
In不要使用
容器::value\u type const&
-它就是为了这个。@Space\u C0wb0y很好的观点。我承认我已经习惯了value\u type和迭代器,并且几乎忽略了容器中的其他typedef。谢谢,这正是我想要的。谢谢,这正是我想要的。谢谢你指出。我忘了提及它没有排序。下次我会把问题弄清楚。:->谢谢你指出。我忘了提到它没有排序。下次我会把问题弄清楚。:->