修改向量的元素(按值,按引用)函数 C++

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

Modify elements of vector (by value, by reference) Function C++

c++functionvectorpass-by-referencepass-by-value

提问by Hani Goc

I have a function where I have to modifiy the values of a vector. is it a good practice in C++ to return the vector?

我有一个函数,我必须修改向量的值。在 C++ 中返回向量是一个好习惯吗?

Function 1:

功能一:

vector<string> RemoveSpecialCharacters(vector<string> words)
{
    for (vector<string>::iterator it=words.begin(); it!=words.end(); )
    {
        if(CheckLength(*it) == false)
        {
            it = words.erase(it);
        }
        else{
            ++it;
        }
    }//end for

    return words;
}

Function 2:

功能二:

void RemoveSpecialCharacters(vector<string> & words)
{
    for (vector<string>::iterator it=words.begin(); it!=words.end(); )
    {
        if(CheckLength(*it) == false)
        {
            it = words.erase(it);
        }
        else{
            ++it;
        }
    }//end for
}

采纳答案by billz

Your two functions serve for two different purposes.

您的两个功能用于两个不同的目的。

  • Function 1: works as remove_copy. It will notmodify the existing container; it makes a copy and modifies that instead.

  • Function 2: works as remove. It will modify the existing container.

  • 功能 1:作为remove_copy. 它不会修改现有的容器;它复制并修改它。

  • 功能 2:作为remove. 它将修改现有容器。

回答by NPE

It's somewhat subjective. I personally would prefer the latter, since it does not impose the cost of copying the vector onto the caller (but the caller is still free to make the copy if they so choose).

这有点主观。我个人更喜欢后者,因为它不会强加将向量复制到调用者的成本(但如果他们愿意,调用者仍然可以自由制作副本)。

回答by Spook

In this particular case I'd choose passing by reference, but not because it's a C++ practice or not, but because it actually makes more sense (the function by its name applies modification to the vector). There seems to be no actual needto return data from the function.

在这种特殊情况下,我会选择通过引用传递,但不是因为它是否是 C++ 实践,而是因为它实际上更有意义(函数的名称将修改应用于向量)。似乎没有实际需要从函数返回数据。

But that also depends on purpose of the function. If you alwayswant to use it in the following way:

但这也取决于函数的目的。如果您总是想通过以下方式使用它:

vec = bow.RemoveSpecialCharacters(vec);

Then absolutely first option is a go. Otherwise, the second one seems to be more appropriate. (Judging by the function name, the first one seems to meto be more appropriate).

那么绝对第一个选择是去。否则,第二个似乎更合适。(从函数名来看,第一个在我看来更合适)。

In terms of performance, the first solution in modern C++11 world will be more-less a few assignments slower, so the performance impact would be negligible.

在性能方面,现代 C++11 世界中的第一个解决方案将更多 - 更少一些赋值更慢,因此性能影响可以忽略不计。

回答by Claudio

Go for option 2, modifying the vector passed as parameter.

选项2,修改作为参数传递的载体。

Side note: some coding practices suggest to pass through pointers arguments that may be changed (just to make it clear at a first glance to the developers that the function may change the argument).

旁注:一些编码实践建议传递可能会更改的指针参数(只是为了让开发人员第一眼就清楚该函数可能会更改参数)。

回答by EoD

The best practice is to pass by reference the vector.

最佳实践是通过引用传递向量。

Inside the function you edit it and you don't have to return it back (which in fact you are allocating a new memory space that is not needed).

在您编辑它的函数内部,您不必将其返回(实际上您正在分配一个不需要的新内存空间)。

If you pass it by reference the cost is much smaller and the vector is also edited out of the scope of the function

如果您通过引用传递它,成本会小得多,并且向量也被编辑出函数的范围

回答by Slava

Actually neither one is a good practice for C++, if by good practice to mean how it is done in C++ libraries. You basically reimplement what std::remove_copy_ifor std::remove_ifdoes, so good practice is to implement functions (or use existing ones), that work on range, not on container, either by value or by reference.

实际上,对于 C++ 来说,这两个都不是一个好的实践,如果通过好的实践来表示它是如何在 C++ 库中完成的。你基本上是重新实现什么std::remove_copy_ifstd::remove_if做什么,所以好的做法是实现功能(或使用现有的),在范围上工作,而不是在容器上,无论是按值还是按引用。

Again this depends on how you define term good practice.

同样,这取决于您如何定义 term good practice

回答by Pierre Emmanuel Lallemant

The first function will return a copy of the vector, so it will be slower than the second. You should use the second.

第一个函数将返回向量的副本,因此它会比第二个慢。你应该使用第二个。