本文介绍了我应该返回std ::字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尽量使用 std :: string 而不是 char * 可能会降低性能太多。这是一个返回字符串的好方法(没有错误检查简洁)?

I'm trying to use std::string instead of char* whenever possible, but I worry I may be degrading performance too much. Is this a good way of returning strings (no error checking for brevity)?

std::string linux_settings_provider::get_home_folder() {
    return std::string(getenv("HOME"));
}

另外,一个相关的问题:当接受字符串作为参数时, const std :: string& const char *

Also, a related question: when accepting strings as parameters, should I receive them as const std::string& or const char*?

感谢。

推荐答案

返回字符串。

认为更好的抽象是值得的。在你可以衡量一个有意义的性能差异之前,我认为这是一个只存在于你的想象力的微优化。

I think the better abstraction is worth it. Until you can measure a meaningful performance difference, I'd argue that it's a micro-optimization that only exists in your imagination.

需要很多年才能得到一个好的字符串抽象成C ++。我不相信Bjarne Stroustroup,因为他保守的只为你使用的东西支付的名声而着名,将会允许一个明显的性能杀手。更高的抽象是好的。

It took many years to get a good string abstraction into C++. I don't believe that Bjarne Stroustroup, so famous for his conservative "only pay for what you use" dictum, would have permitted an obvious performance killer into the language. Higher abstraction is good.

这篇关于我应该返回std ::字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 08:00
查看更多