如何比较字符串 [英] How to compare strings

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

问题描述

我想比较一个字符串,但实际上没有将其中一个定义为字符串,像这样,

I wanted to compare a string without actually defining one of them as a string, something like this,

if (string == 'add')

我必须声明'add'作为字符串或者是可能以类似的方式进行比较?

Do I have to declare 'add' as a string or is it possible to compare in a similar way?

推荐答案

:string类实现比较运算符,因此您可以使用 == ,如下所示:

In C++ the std::string class implements the comparison operators, so you can perform the comparison using == just as you would expect:

if (string == "add") { ... }

正确使用操作符重载是一个优秀的C ++特性。

When used properly, operator overloading is an excellent C++ feature.

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

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