繁体   English   中英

我可以在C ++中按> <比较字符串吗

[英]May I compare strings by > < in c++

恰如本主题所述:我可以在c ++中按><比较字符串。 我没有错误,但是不确定我将始终获得良好的结果吗?

string a = "aabbsd", b= "bsdds";
cout<<(a<b);

结果只是运气吗?

是的你可以。 没错。 唯一需要注意的是操作的复杂性是线性的。

这将触发词典比较。 来自cppreference

operator==,!=,<,<=,>,>=(std::basic_string)
C++  Strings library std::basic_string   

template< class T, class Alloc >
bool operator==( basic_string<T,Alloc>& lhs, basic_string<T,Alloc>& rhs );
(1) 

template< class T, class Alloc >
bool operator!=( basic_string<T,Alloc>& lhs, basic_string<T,Alloc>& rhs );
(2) 

template< class T, class Alloc >
bool operator<( basic_string<T,Alloc>& lhs, basic_string<T,Alloc>& rhs );
(3) 

template< class T, class Alloc >
bool operator<=( basic_string<T,Alloc>& lhs, basic_string<T,Alloc>& rhs );
(4) 

template< class T, class Alloc >
bool operator>( basic_string<T,Alloc>& lhs, basic_string<T,Alloc>& rhs );
(5) 

template< class T, class Alloc >
bool operator>=( basic_string<T,Alloc>& lhs, basic_string<T,Alloc>& rhs );
(6) 

比较两个字符串的内容。
1-2)检查lhs和rhs的内容是否相等,即lhs.size()== rhs.size(),并且lhs中的每个字符在同一位置的rhs中具有相等的字符。
3-6)从字典上比较lhs和rhs的内容。 比较由等效于std :: lexicographical_compare的函数执行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM