const char * vs string [英] const char* vs string

查看:83
本文介绍了const char * vs string的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

在哪种情况下使用const char *更好到字符串 (或者

甚至const string&)。

我的意思是,在STL中( http://www.sgi.com/tech/stl/hash_map.html )我看到:


hash_map< const char *,int,hash< const char *>,eqstrmonths;

months [" january"] = 31;


我认为这是用于函数调用(我的字符串),但是,

除了

之外真的有必要定义funcion(const char *)函数(const string&)在我的公共成员函数中?


我已经读过编译器在

这样的情况下做了太多的临时变量,但是这是真的吗?


那又怎样呢:


string mystring =" my string" + string1 +"是 + string2;


或in:


cout<<字符串<< "你好" << string2<< "我的名字是 << string3;


谢谢。

解决方案

Javier写道:


你好,

在哪种情况下使用const char *更好到字符串 (或

甚至const string&)。



很少。


唯一真正的原因是与C代码接口。


我的意思是,在STL中( http:// www.sgi.com/tech/stl/hash_map.html )我看到:


hash_map< const char *,int,hash< const char *>,eqstrmonths ;

个月[" january"] = 31;


我认为它适用于函数调用(my string),但是,

除了

函数(const string&)在公共成员函数中定义funcion(const char *)之外真的有必要吗?


我已经读过编译器在

这样的情况下做了太多的临时变量,但这是真的吗?


那又怎样呢? :


string mystring =" my string" + string1 +"是 + string2;


或in:


cout<<字符串<< "你好" << string2<< "我的名字是 << string3;



他们怎么样?我没看到这些链接到你的问题。


-

Ian Collins。


1月1日,12:12,Ian Collins< ian-n ... @ hotmail.comwrote:


Javier写道:
< blockquote class =post_quotes>
你好,

在哪种情况下使用const char *更好到字符串 (或

甚至const string&)。



很少。


唯一真正的原因是与C代码接口。


我的意思是,在STL中( http:// www.sgi.com/tech/stl/hash_map.html )我看到:


hash_map< const char *,int,hash< ; const char *>,eqstrmonths;

months [" january"] = 31;


我认为它适用于类似函数的调用(my string),但是,

在我的公共成员函数中除了

函数(const string&)之外还有必要定义funcion(const char *)吗?


我已经读过编译器在

这样的情况下做了太多的临时变量,但这是真的吗?


那么:


string mystring ="我的字符串 + string1 +"是 + string2;



在这种情况下,编译器是否会创建临时值?那很重要么?我是/ b $ b的意思是,写的更好吗?:


string mystring =" my string" ;;

mystring + = string1;

mystring + ="是';

mystring + = string2;


比:


string mystring =" my string" ; + string1 +"是 + string2;


>


或in:


cout<<字符串<< "你好" << string2<< "我的名字是 << STRING3;



同样的问题。


>

什么关于他们?我没看到这些链接到你的问题。


-

Ian Collins。



谢谢。


Javier写道:


>>


string mystring =" my string" + string1 +"是 + string2;



在这种情况下,编译器是否会创建临时值?那很重要么?我是/ b $ b的意思是,写的更好吗?:


string mystring =" my string" ;;

mystring + = string1;

mystring + ="是';

mystring + = string2;


比:


string mystring =" my string" ; + string1 +"是 + string2;



是的,第二个版本确实创建了临时版和额外的副本(因为
RVO可能会减少一些)。然而,第二个版本比第一个版本更清晰,看起来更自然,除非你这样做数千次

每秒,否则不应该花费额外费用(即除非您的代码

分析表明这是瓶颈的来源)。另外我认为在

的未来有rvalue引用和移动语义,无论如何都会消除开销




-

Dizzy


Hello,
in which cases is it better the use of "const char*" to "string" (or
even const string &).
I mean, in STL (http://www.sgi.com/tech/stl/hash_map.html) I see:

hash_map<const char*, int, hash<const char*>, eqstrmonths;
months["january"] = 31;

I think it is for the use in calls like function("my string"), but, is
it really necessary to define funcion(const char*) besides
function(const string &) in my public member functions?

I have read that the compiler does too much temporaries variables in
such cases, but is it true?

And what about in:

string mystring = "my string" + string1 + " is " + string2;

or in:

cout << string << " hello" << string2 << " my name is " << string3;

Thanks.

解决方案

Javier wrote:

Hello,
in which cases is it better the use of "const char*" to "string" (or
even const string &).

Seldom.

The only real reason is interfacing with C code.

I mean, in STL (http://www.sgi.com/tech/stl/hash_map.html) I see:

hash_map<const char*, int, hash<const char*>, eqstrmonths;
months["january"] = 31;

I think it is for the use in calls like function("my string"), but, is
it really necessary to define funcion(const char*) besides
function(const string &) in my public member functions?

I have read that the compiler does too much temporaries variables in
such cases, but is it true?

And what about in:

string mystring = "my string" + string1 + " is " + string2;

or in:

cout << string << " hello" << string2 << " my name is " << string3;

What about them? I don''t see how these link to your question.

--
Ian Collins.


On 1 jun, 12:12, Ian Collins <ian-n...@hotmail.comwrote:

Javier wrote:

Hello,
in which cases is it better the use of "const char*" to "string" (or
even const string &).


Seldom.

The only real reason is interfacing with C code.

I mean, in STL (http://www.sgi.com/tech/stl/hash_map.html) I see:

hash_map<const char*, int, hash<const char*>, eqstrmonths;
months["january"] = 31;

I think it is for the use in calls like function("my string"), but, is
it really necessary to define funcion(const char*) besides
function(const string &) in my public member functions?

I have read that the compiler does too much temporaries variables in
such cases, but is it true?

And what about in:

string mystring = "my string" + string1 + " is " + string2;

Does the compiler create temporaries in this case? Is it important? I
mean, is it better to write?:

string mystring = "my string";
mystring += string1;
mystring += " is ";
mystring += string2;

than:

string mystring = "my string" + string1 + " is " + string2;

>

or in:

cout << string << " hello" << string2 << " my name is " << string3;

Same question.

>
What about them? I don''t see how these link to your question.

--
Ian Collins.

Thanks.


Javier wrote:

>>

string mystring = "my string" + string1 + " is " + string2;


Does the compiler create temporaries in this case? Is it important? I
mean, is it better to write?:

string mystring = "my string";
mystring += string1;
mystring += " is ";
mystring += string2;

than:

string mystring = "my string" + string1 + " is " + string2;

Yes the second version does create temporaries and additional copies (tho
RVO might reduce some there). However, the second version is clearer than
the first one, seems more natural and unless you do that thousands of times
per second it shouldn''t matter the additional cost (ie unless your code
profiling points this as a source of bottleneck). Also I think that in the
future with rvalue references and move semantics that overhead will be
eliminated anyway.

--
Dizzy


这篇关于const char * vs string的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆