Why this odd behavior on my code

Правка en3, от LM10xLeoMessi, 2025-02-12 10:39:53

i was writing this code and faced this unknown behavior.

void solve(){
    string s = "a";
    for(int i = 0; i < s.size() &mdash; 1; i++){
        cout << "1st for loop\n";
        if(s[i] == s[i + 1]){
            cout << s[i] << s[i + 1] << '\n';
            return;
        }
    }
    for(int i = 0; i < s.size() &mdash; 2; i++){
        cout << "2nd for loop " << i << '\n';
        if(s[i] != s[i + 1] and s[i] != s[i + 2] and s[i + 1] != s[i + 2]){
            cout << s[i] << s[i + 1] << s[i + 2] << '\n';
            return; 
        }
    }
    cout << -1;
}

shouldn't it print -1? then why it enters into second for loop as the size of my string is 1? Can anyone help me to understand this behavior?

История

 
 
 
 
Правки
 
 
  Rev. Язык Кто Когда Δ Комментарий
en4 Английский LM10xLeoMessi 2025-02-12 10:41:03 16
en3 Английский LM10xLeoMessi 2025-02-12 10:39:53 20
en2 Английский LM10xLeoMessi 2025-02-12 10:39:26 18
en1 Английский LM10xLeoMessi 2025-02-12 10:39:00 758 Initial revision (published)