Python中文
首页
教程
问答
标签
搜索
登录
注册
python、c++、c#和j中字符串比较的差异
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>似乎字符串比较不能简单地由“==”运算符来完成,正如我从java和c的解释中所读到的那样:</p> <p>在Java中,我看到了这样的解释:</p> <pre><code>== tests for reference equality (whether they are the same object). .equals() tests for value equality (whether they are logically "equal"). </code></pre> <p>在c中,我看到了以下代码:</p> ^{pr2}$ <p>对我来说,“==”是检查地址,而.equal()只是检查值。在</p> <p>然而,我一直在python和c++中使用“==”,并且从未遇到过这样的错误</p> <p>在python中:</p> <pre><code>string1 = "helloworld" string2 = "helloworld" print(string1 == string2)// result true </code></pre> <p>在c++中:</p> <pre><code>while(getline(ifs, line2)){ stringstream ssm(line2); string from_stop; string to_stop; getline(ssm, from_stop, ','); getline(ssm, to_stop, ','); if(from_stop == to_stop){ adjList[from_stop].push_back(to_stop); } } </code></pre> <p>或者</p> <pre><code> bool stop124 = false; bool stopA24 = false; bool stop126 = false; for (int i = 0; i < adjVec.size(); i++) { if (adjVec[i] == "124") stop124 = true; else if (adjVec[i] == "A24") stopA24 = true; else if (adjVec[i] == "126") stop126 = true; } </code></pre> <p>这些代码将成功编译并获得正确的值比较结果。我知道c++中有一个strcmp()函数,但我很少使用它,也不知道在检查两个字符串的值是否相等时,应该使用它而不是==函数。在</p> <p>所以我的问题是,这是因为这些编程语言之间存在差异,还是我很幸运没有遇到这些错误?在</p>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>Python按字典顺序比较字符串,即使用字符的ASCII值,因此可以使用<code>==</code>运算符。Java创建了一个string对象,因此您必须使用<code>.equals()</code>方法来检查值。在</p> <p>所以要回答你的问题,语言是不同的。如果您试图比较C++中的2个字符串,我将使用^ {CD3}方法。在</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
无法使用Django/mongoengine连接到MongoDB(身份验证失败)
6 回答
无法使用Django\u mssql\u后端迁移到外部hos
2 回答
无法使用Django&Python3.4连接到MySql
7 回答
无法使用Django+nginx上载媒体文件
2 回答
无法使用Django1.6导入名称模式
9 回答
无法使用Django1.7和mongodb登录管理站点
5 回答
无法使用Djangoadmin创建项目,进程使用了错误的路径,因为我事先安装了错误的Python
8 回答
无法使用Djangockedi验证CBV中的字段
7 回答
无法使用Djangocketditor上载图像(错误400)
3 回答
无法使用Djangocron进行函数调用
9 回答
无法使用Djangofiler djang上载文件
2 回答
无法使用Djangokronos
4 回答
无法使用Djangomssql provid
4 回答
无法使用Djangomssql连接到带有Django 1.11的MS SQL Server 2016
9 回答
无法使用Djangomssq迁移Django数据库
3 回答
无法使用Djangonox创建用户
7 回答
无法使用Djangopyodb从Django查询SQL Server
9 回答
无法使用Djangopython3ldap连接到ldap
2 回答
无法使用Djangoredis连接到redis
9 回答
无法使用Django中的FK创建新表
7 回答