C++ 我应该如何将 std::string 传递给函数?

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

How should I pass a std::string to a function?

c++

提问by nakiya

Should I always pass a std::string by const reference to a function if all that is done inside that function is to copy that string? Additionally, what is the difference (perf or otherwise) between passing by value and passing by reference? As I understand, one uses operator=and the other copy constructor. Is that the case?

如果在该函数内完成的所有操作都是复制该字符串,我是否应该始终通过对函数的 const 引用传递 std::string?此外,按值传递和按引用传递之间有什么区别(性能或其他)?据我了解,一个使用operator=另一个复制构造函数。是这样吗?

回答by Johan Kotlinski

Don't believe everything you read on the internet. It is better to pass by const reference. To give proof, I wrote a test program...

不要相信您在互联网上阅读的所有内容。最好通过 const 引用传递。为了证明,我写了一个测试程序......

test.cpp:

测试.cpp:

#include <ctime>
#include <iostream>
#include <string>

void foo(std::string s);
void bar(const std::string& s);

int main() {
    const std::string s("test string");

    clock_t start = clock();
    for (int it = 0; it < 1000000; ++it)
        foo(s);
    std::cout << "foo took " << (clock() - start) << " cycles" << std::endl;

    start = clock();
    for (int it = 0; it < 1000000; ++it)
        bar(s);
    std::cout << "bar took " << (clock() - start) << " cycles" << std::endl;
}

aux.cpp:

辅助.cpp:

#include <string>
std::string mystring;

void foo(std::string s) { mystring = s; }
void bar(const std::string& s) { mystring = s; }

Compiled with 'g++ -O3 test.cpp aux.cpp' and got the printout:

用'g++ -O3 test.cpp aux.cpp'编译并得到打印输出:

foo took 93044 cycles
bar took 10245 cycles

Passing by reference is faster by an order of magnitude.

通过引用传递速度快一个数量级。

回答by James McNellis

Should I always pass a std::string by const reference to a function if all that is done inside that function is to copy that string?

如果在该函数内完成的所有操作都是复制该字符串,我是否应该始终通过对函数的 const 引用传递 std::string?

No. If you are going to just copy the string inside of the function, you should pass by value. This allows the compiler to perform several optimizations. For more, read Dave Abraham's "Want Speed? Pass by Value."

不。如果您只想复制函数内部的字符串,则应该按值传递。这允许编译器执行多项优化。如需更多信息,请阅读 Dave Abraham 的“想要速度?传递价值”。

What is the difference (perf or otherwise) between passing by value and passing by reference? As I understand, one uses operator= and the other copy constructor. Is that the case?

按值传递和按引用传递之间有什么区别(性能或其他)?据我了解,一个使用 operator= 和另一个复制构造函数。是这样吗?

No, that is not at all the case. A reference is not an object; it is a reference to an object. When you pass by value, a copy of the object being passed is made. When you pass by reference, a reference to the existing object is made and there is no copy. A good introductory C++ bookwill explain these basic concepts in detail. It is critical to understand the basics if you want to develop software in C++.

不,事实并非如此。引用不是对象;它是一个对象的引用。当您按值传递时,会生成所传递对象的副本。当您通过引用传递时,将创建对现有对象的引用,并且没有副本。 一本很好的 C++ 入门书将详细解释这些基本概念。如果您想用 C++ 开发软件,了解基础知识至关重要。