簡體   English   中英

C ++字符串聯與std :: string行為。 請解釋一下

[英]C++ character concatenation with std::string behavior. Please explain this

以下是關於c ++ std :: string的一些我無法理解的案例。

1。

string ans = ""+'a';
cout << ans << endl; //Prints _string::copy

2。

string ans="";
ans=ans+'a';
cout << ans << endl; //Prints a

3。

string ans="";
ans = ans + (5 + '0'); // Throws error

4。

string ans="";
ans += (5 + '0'); //works

5。

在代碼中,我有一行ans += to_string(q); q是單個數字整數。 該程序引發了運行時錯誤。

將其更改為ans+= (q+'0'); 並刪除了錯誤。

請幫助清除這個想法。

string ans = ""+'a';

“”是空字符串文字的地址。 'a'被解釋為整數,ASCII碼65.這會將65添加到文字字符串的地址,這會導致未定義的行為,可能是崩潰。

ans=ans+'a';

ans是一個std::string std::string定義了一個重載的+運算符。 實際上有幾個。 其中之一,特別是重載+參數是一個字符,並將字符附加到字符串。

ans = ans + (5 + '0'); // Throws error

5+'0'是一個被提升為int類型的表達式。 std::string沒有明確地使用int作為參數來重載+運算符。 這導致編譯錯誤。

ans += (5 + '0'); //works

std::string確實有一個不明顯的重載+=運算符,所以編譯得很好。

這個:

std::string ans = ""+'a';

不是你的想法,你實際執行的操作如下:

const char* p = "";
p = p + 97 /*97=='a'*/; // increase p pointer by `a` value, results in UB (pointer to random memory)
std::string ans = p; // p points to possibly unallocated memory (UB).

這沒什么意義。

如果你用clang編譯它,你會獲得很長的warnigns列表:

main.cpp:22:25: warning: adding 'char' to a string does not append to the string [-Wstring-plus-int]
    std::string ans = ""+'a';
                      ~~^~~~
main.cpp:22:25: note: use array indexing to silence this warning
    std::string ans = ""+'a';
                        ^
                      & [   ]
main.cpp:22:25: warning: adding 'char' to a string pointer does not append to the string [-Wstring-plus-char]
    std::string ans = ""+'a';
                      ~~^~~~
main.cpp:22:25: note: use array indexing to silence this warning
    std::string ans = ""+'a';
                        ^
                      & [   ]

字符串文字是一個字符數組。 它不是std::string的實例。 數組不能通過值傳遞給函數或運算符,而是在使用操作數時它們衰減為指向第一個字符的指針。

字符是編碼符號的數字。 '\\0'外,所有字符都具有非零值。

在表達式""+'a' ,字符串文字衰減到指針,然后'a'字符被解釋為非零整數。 該值將添加到指針中。 無論a的值如何(在常用的ASCII編碼中恰好是65),結果都超出了數組的范圍。 超出范圍的指針算法具有未定義的行為,輸出1.是未定義行為的結果。


程序2.具有明確定義和預期的行為。


 ans = ans + (5 + '0'); // Throws error 

沒有operator+接受參數std::stringint 右手參數是int因為5 + '0'char參數被提升為int因此兩個參數的類型相同。 這也是表達式的返回類型。

這是毛茸茸的地方。 有一個operator+接受charint可以轉換為char 但是,還有其他可能的轉換模糊不清。 這是clang顯示的錯誤:

error: invalid operands to binary expression ('string' (aka 'basic_string<char>') and 'int')

    ans = ans + (5 + '0'); // Throws error

          ~~~ ^ ~~~~~~~~~

./include/c++/6.1.0/bits/basic_string.h:4982:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'int')

    operator+(const basic_string<_CharT, _Traits, _Alloc>& __lhs, _CharT __rhs)

    ^

./include/c++/6.1.0/bits/basic_string.h:5036:5: note: candidate template ignored: deduced conflicting types for parameter '_CharT' ('char' vs. 'int')

    operator+(basic_string<_CharT, _Traits, _Alloc>&& __lhs,
^

......以及許多其他潛在的超載。


 ans += (5 + '0'); //works 

這是因為operator+=(char); 是一個明確的超載。


在代碼中,我有一行ans + = to_string(q); q是單個數字整數。 該程序引發了運行時錯誤。

在這里工作正常,沒有錯誤拋出。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM