C++:减去向量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14175858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 18:04:11  来源:igfitidea点击:

C++: subtract vectors

c++

提问by Kifsif

I have two vectors. And I need to remove from vector1 what there is in vector2.

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

[EDIT: it isn't clear whether this means per-element subtraction as per the link below or set difference]

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

I use Visual Studio 2010.

我使用 Visual Studio 2010。

There seems to be a method: http://msdn.microsoft.com/en-us/library/system.windows.vector.subtract.aspx

似乎有一种方法:http: //msdn.microsoft.com/en-us/library/system.windows.vector.subtract.aspx

But it somehow doesn't work and even there is no code example.

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

Could you help me? If no standard method exists, maybe you could suggest how to organize it via loops? Thank you in advance.

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

#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;
}

回答by K-ballo

You should use std::set_difference: http://en.cppreference.com/w/cpp/algorithm/set_difference

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

First you will need to sortyour vectors, since set_differenceoperates on sortedranges. That is, unless they are sortedalready (like in your use case).

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

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

Then you call it like this:

然后你这样称呼它:

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

This will append to differencethose elements found in vector1that are not found in vector2.

这将追加到difference在发现这些元素vector1中未找到vector2

回答by Ivan Tkachenko

std::transform(vector1.begin(), vector1.end(), vector2.begin(), vector1.begin(), std::minus<int>())

std::transform(vector1.begin(), vector1.end(), vector2.begin(), vector1.begin(), std::minus<int>())

The 4th argument is the place of the result. It should work even if the size of vectors are different .

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

回答by cmc

If you don't want to use std::set_difference, you can do this:

如果你不想使用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();
    }
}

This will erase from aall values that are in b.

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

Good luck

祝你好运

回答by anh_ng8

I'd suggest switching to EigenVectors which has built-in arithmetic operations for vectors:

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

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

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

So you can use operators like +,-,*,/

所以你可以使用像 +,-,*,/ 这样的运算符

#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;
}

回答by Ulziisukh

/The simplest way/

/最简单的方法/

#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
}