使用关系运算符C ++比较字符串

时间:2018-11-07 19:51:11

标签: c++ string relational

比较字符串时如何...

dim nameTable = new NameTable(Of SomePropertyClass)()
Dim someProperty as SomePropertyClass = nameTable.Add(New SomePropertyClass(), "My Name")
Dim root as RootObject = New RootObject With { .SomeProperty = someProperty }

Dim settings = new JsonSerializerSettings()
settings.Converters.Add(new ObjectToNameConverter(Of SomePropertyClass)(nameTable))

Dim json as String = JsonConvert.SerializeObject(root, settings)
Console.WriteLine(json) ' Prints {"SomeProperty":"My Name"}
dim root2 as RootObject = JsonConvert.DeserializeObject(Of RootObject)(json, settings)
' Assert that the same instance of SomeProperty was used during deserialization
Assert.IsTrue(root2.SomeProperty Is root.SomeProperty) 
Assert.IsTrue(json.Equals("{""SomeProperty"":""My Name""}"))

这些陈述为什么是错误的?当将两个字符串相互比较时,规则是什么?

1 个答案:

答案 0 :(得分:2)

在C / C ++中对字符串使用关系运算符将只比较字符串的内存地址。显然"11""8"占用2个不同的内存区域,因此可能为假,除非存储"11"的地址恰好存储在大于{{1}的地址中},但这是随机的。

请记住,您可以使用"8",但是它是在比较字符串的ASCII码。由于string::compare(ASCII码49)小于"1"(ASCII码56),因此它仍然为false。您需要使用"8"将字符串转换为整数,然后比较整数。