簡體   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