【问题标题】:C++: subtract vectorsC++:减去向量
【发布时间】:2019-09-13 21:54:31
【问题描述】:

我有两个向量。我需要从vector1中删除vector2中的内容。

[编辑:不清楚这是否意味着根据下面的链接进行逐元素减法或设置差异]

我使用 Visual Studio 2010。

好像有个方法: http://msdn.microsoft.com/en-us/library/system.windows.vector.subtract.aspx

但它不知何故不起作用,甚至没有代码示例。

你能帮帮我吗?如果不存在标准方法,也许您可​​以建议如何通过循环组织它?提前谢谢你。

#include "stdafx.h";
#include <vector>;
#include <iostream>

using namespace std;

int main ()
{
  vector<int> vector1;
  vector<int> vector2;

  for (int i = 0; i < 10; i++)
  {
vector1.push_back (i);
  }

  for (int i = 0; i < 6; i++)
  {
    vector2.push_back (i);
  }

  myvector1 = Subtract(vector1, vector2); 

  return 0;
}

【问题讨论】:

  • 你找到的方法是针对.NET的。这意味着要将它与 C++ 一起使用,您必须切换到 C++/CLI。
  • 似乎对您链接的函数的作用(二维向量减法)以及您可能想要的(设置差异或逐元素数字减法)存在严重误解。是哪个?
  • @Timbo:“我需要从 vector1 中删除 vector2 中的内容”。这是固定的差异,没有误解。
  • @K-ballo 你可能是对的。但是,很明显,链接方法根本不这样做。
  • @Timbo:我猜 OP 只是感到困惑,因为 .Net Vector 是几何向量而不是容器,而且他显然在使用容器......

标签: c++


【解决方案1】:

你应该使用std::set_differencehttp://en.cppreference.com/w/cpp/algorithm/set_difference

首先你需要sort你的vectors,因为set_difference排序范围内运作。也就是说,除非它们已经排序(就像在您的用例中一样)。

std::sort(vector1.begin(), vector1.end());
std::sort(vector2.begin(), vector2.end());

那你这样称呼它:

std::vector<int> difference;
std::set_difference(
    vector1.begin(), vector1.end(),
    vector2.begin(), vector2.end(),
    std::back_inserter( difference )
);

这会将在vector1 中找到但在vector2 中找不到的元素附加到difference

【讨论】:

    【解决方案2】:

    std::transform(vector1.begin(), vector1.end(), vector2.begin(), vector1.begin(), std::minus&lt;int&gt;())

    第四个参数是结果的位置。即使向量的大小不同,它也应该可以工作。

    【讨论】:

    • 只是为了向正在快速浏览的人澄清一下:这是每个元素减法而不是设置差异。
    【解决方案3】:

    如果你不想使用std::set_difference,你可以这样做:

    // substracts b<T> to a<T>
    template <typename T>                                                                                            
    void
    substract_vector(std::vector<T>& a, const std::vector<T>& b)                                                     
    {
        typename std::vector<T>::iterator       it = a.begin();
        typename std::vector<T>::const_iterator it2 = b.begin();
    
        while (it != a.end())
        {
            while (it2 != b.end() && it != a.end())
            {
                if (*it == *it2)
                {
                    it = a.erase(it);
                    it2 = b.begin();
                }
    
                else
                    ++it2;
            }
            if (it != a.end())
                ++it;
    
            it2 = b.begin();
        }
    }
    

    这将从a 中删除b 中的所有值。

    祝你好运

    【讨论】:

    • 这也不能回答问题。这将执行设置差异操作。 OP 要求逐个元素减法。
    【解决方案4】:

    我建议切换到具有内置向量算术运算的 EigenVectors:

    http://eigen.tuxfamily.org/dox-devel/group__TutorialMatrixArithmetic.html

    因此您可以使用 +、-、*、/ 等运算符

    #include <iostream>
    #include <Eigen/Dense>
    using namespace Eigen;
    int main()
    {
      Matrix2d a;
      a << 1, 2,
           3, 4;
      MatrixXd b(2,2);
      b << 2, 3,
           1, 4;
      std::cout << "a + b =\n" << a + b << std::endl;
      std::cout << "a - b =\n" << a - b << std::endl;
      std::cout << "Doing a += b;" << std::endl;
      a += b;
      std::cout << "Now a =\n" << a << std::endl;
      Vector3d v(1,2,3);
      Vector3d w(1,0,0);
      std::cout << "-v + w - v =\n" << -v + w - v << std::endl;
    }
    

    【讨论】:

      【解决方案5】:

      /最简单的方法/

      #include<stdio.h>
      main()
      {
       int A,B,C;
       printf("enter the two numbers 1st 2nd=");
       scanf("%d",&A);
       scanf("%d",&B);
       C=A-B;
       printf("Result=");
       printf
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-10-19
        • 1970-01-01
        • 1970-01-01
        • 2021-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-06-11
        相关资源
        最近更新 更多