C++ 矢量加法运算

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3642700/
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-28 13:21:28  来源:igfitidea点击:

Vector addition operation

c++linuxvectorg++

提问by Arpit

I am trying to add two Vectors below is the code snippet :-

我想在下面添加两个向量是代码片段:-

#include <iostream>
#include <vector>
using namespace std;

int main()
{
        unsigned int i = 0;
        vector <float> v1;
        vector <float> v2;
        vector <float> v3;



        cout << "Filling the Numbers\n";
        for (i=5;i < 125 ; i = i + 5) {
            v1.push_back(i/10);
            v2.push_back(i/100);
        }

        cout << "Adding the numbers\n";
        for (i = 0; i < v1.size(); i++) {
                v3[i] = v1[i] + v2[i];
        }

        cout << "Printing the numbers\n";
                for (i = 0; i < v3.size() ; i++) {
                        cout << v3[i];
                }


        return 0;
}

The program is crashing at Line 18. It seems to me I need to do operator overloading for + operation. Please help.

程序在第 18 行崩溃。在我看来,我需要对 + 操作进行运算符重载。请帮忙。

回答by greyfade

To avoid the obvious pitfalls you encountered, you can do this as an alternative:

为了避免您遇到的明显陷阱,您可以这样做:

#include <algorithm> // for transform
#include <functional> // for plus

std::transform(v1.begin(), v1.end(), v2.begin(), std::back_inserter(v3), std::plus<float>());

Reference: https://en.cppreference.com/w/cpp/algorithm/transform

参考:https: //en.cppreference.com/w/cpp/algorithm/transform

回答by Kornel Kisielewicz

This line doesn't work, because there's no v3[i]allocated:

此行不起作用,因为没有v3[i]分配:

v3[i] = v1[i] + v2[i];

You have two choices, either use 'push_back'

你有两个选择,要么使用'push_back'

v3.push_back( v1[i] + v2[i] );

Or resize the array to the given size before hand:

或者事先将数组调整为给定的大小:

v3.resize( v1.size() );

If you push_back, it will be nice to preallocate the space anyway:

如果你 push_back,无论如何预先分配空间会很好:

v3.reserve( v1.size() );

And finally, you can try to read up on std::valarrayinstead, as those operations are already built into it!

最后,您可以尝试继续阅读std::valarray,因为这些操作已经内置于其中!

Edit:and yes, as Johannes noted, you have a problem with floating point division :>

编辑:是的,正如约翰内斯所指出的,浮点除法有问题:>

回答by Johannes Schaub - litb

First you need to do a floating point division

首先你需要做一个浮点除法

v1.push_back(i/10.0f);
v2.push_back(i/100.0f);

Then you need to have space for ivariables on v3or use push_back

然后你需要为i变量留出空间v3或使用push_back

v3.push_back(v1[i] + v2[i]);

回答by fredoverflow

v3[i] = v1[i] + v2[i];

v3[i] = v1[i] + v2[i];

You are assigning to elements that do not exist. Try v3.push_back(v1[i] + v2[i]);instead.

您正在分配给不存在的元素。试试吧v3.push_back(v1[i] + v2[i]);

Also, you probably want i/10.0instead of i/10, otherwise your results are rounded.

此外,您可能想要i/10.0代替i/10,否则您的结果将被四舍五入。

回答by Nils Pipenbrinck

You write into the v3 vector, but you haven't allocated any space for it.

您写入 v3 向量,但尚未为其分配任何空间。

Try to add:

尝试添加:

 v3.reserve (v1.size());

between your first and second loop.

在你的第一个和第二个循环之间。

回答by bramp

I think the problem is v3[i] doesn't work as the vector starts off having zero elements. What you want to do is either:

我认为问题是 v3[i] 不起作用,因为向量开始时​​元素为零。你想要做的是:

v3.push_back( v1[i] + v2[i] );

or preallocate the vector

或预先分配向量

v3.resize( v1.size() );

OR the final solution, which I would do is

或者我会做的最终解决方案是

v3.reserve( v1.size() );

for (i = 0; i < v1.size(); i++) {
    v3.push_back( v1[i] + v2[i] );
}

as this avoid resizing the vector again and again.

因为这样可以避免一次又一次地调整向量的大小。