比较字符串和“”字符串字面量 [英] compare string and "" string literal

查看:106
本文介绍了比较字符串和“”字符串字面量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将字符串对象与"进行比较是否正确和安全,引用空字符串的一对

引号?如果字符串对象:s ="" ;;

s包含一个''\''?使用std :: string :: size或

std :: string :: empty来处理这种情况会更好吗?谢谢。


string s ="" ;;

if(s =="");

if(s.size == 0);

if(s.empty());

解决方案

< blockquote>


On 1 ??24è?,é???10ê±41·?,lovecreatesbea ... @ gmail.com

< lovecreatesbea ... @ gmail.comwrote:


将字符串对象与",一对
进行比较是否正确和安全
引号引用空字符串?如果字符串对象:s ="" ;;

s包含一个''\''?使用std :: string :: size或

std :: string :: empty来处理这种情况会更好吗?谢谢。


string s ="" ;;

if(s =="");

if(s.size == 0);

if(s.empty());



(对不起前一篇文章中的错误)


比较字符串对象和是否正确和安全 ;,一对

引号引用空字符串?如果字符串对象:s ="" ;;

包含一个''\ 0''?使用std :: string :: size或

std :: string :: empty来处理这种情况会更好吗?


string s =" ;" ;;

if(s =="");

if(s.size == 0);

if (s.empty());


lovecreatesbea ... @ gmail.coma¨|暴击:


>


比较字符串对象与",引用一对

引号是否正确和安全空字符串?



是的,编译器创建一个用"初始化的临时字符串。

执行比较。


如果字符串对象:s ="" ;;

包含一个''\ 0''?



这是实现depedant,唯一的要求是

c_str()方法返回一个0终止的POD char *。 br />


使用std :: string :: size或

std :: string :: empty更好地处理这种情况?



除非你有特别的理由不这样做,否则

std :: string :: empty更清晰。


>

string s ="" ;;

if(s =="");



花费中间对象和调用比较函数,除非

有某种优化(?)。


if(s.size == 0);



值得注意的是std :: string :: size复杂度在

容器的大小中可能是线性的;但它应该适用于主字符串实现。


if(s.empty());



它可能相当于a.size()== 0但预计会在

摊销的常数时间内运行;如果确实有效,可能会更快。

Michael


2007年1月24日星期三08:53:19 +0100,Michael DOUBEZ写道:


> lovecreatesbea ... @ gmail.coma¨|暴击:


>比较一个字符串对象与",引用空字符串的一对引号是否正确和安全?


是的,编译器会创建一个用"初始化的临时字符串。
进行比较。



编译器不会创建临时字符串。一些实现

在内部创建一个临时字符串,但这是一个不同的问题。


祝福,

Roland Pibinger


Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?If the string object: s = ""; does
s contain a single ''\''? Is it better to use std::string::size or
std::string::empty to deal with this condition? Thank you.

string s = "";
if (s == "");
if (s.size == 0);
if (s.empty());

解决方案



On 1??24è?, é???10ê±41·?, "lovecreatesbea...@gmail.com"
<lovecreatesbea...@gmail.comwrote:

Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?If the string object: s = ""; does
s contain a single ''\''? Is it better to use std::string::size or
std::string::empty to deal with this condition? Thank you.

string s = "";
if (s == "");
if (s.size == 0);
if (s.empty());

(sorry for the mistakes in my previous post)

Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string? If the string object: s = ""; does
s contain a single ''\0''? Is it better to use std::string::size or
std::string::empty to deal with this condition?

string s = "";
if (s == "");
if (s.size == 0);
if (s.empty());


lovecreatesbea...@gmail.com a ¨|crit :

>

Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?

Yes, the compiler creates a temporary string initialized with "" to
perform the comparison.

If the string object: s = ""; does
s contain a single ''\0''?

This is implementation depedant, the only requirement is that the
c_str() method returns a POD char* which is 0 terminated.

Is it better to use std::string::size or
std::string::empty to deal with this condition?

Unless you have a specific reason to do otherwise, the
std::string::empty is the clearer.

>
string s = "";
if (s == "");

Costs a intermediary object and a call to a comparison function unless
there is some kind of optimisation (?).

if (s.size == 0);

It is to note that std::string::size complexity may be linear in the
container''s size; but it should work fine with main string implementations.

if (s.empty());

It may be equivalent to a.size() == 0 but it is expected to run in
amortized constant time; it may be faster if that does count.
Michael


On Wed, 24 Jan 2007 08:53:19 +0100, Michael DOUBEZ wrote:

>lovecreatesbea...@gmail.com a ¨|crit :

>Is it correct and safe to compare a string object with "", a pair of
quotation marks quoted empty string?


Yes, the compiler creates a temporary string initialized with "" to
perform the comparison.

The compiler does not create a temporary string. Some implementations
internally create a temporary string but that is a different question.

Best wishes,
Roland Pibinger


这篇关于比较字符串和“”字符串字面量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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