When I loop over a temporary std::string
(rvalue?) using a range-based for loop, there seems to be an extra character, the null terminator \0
.
When the string is not temporary (lvalue instead?), there is no extra character. Why?
std::map<char, int> m;
for (char c : "bar") m[c] = 0;
for (auto [c, f] : m) {
if (c == '\0') std::cout << "this is a null char, backward slash zero" << std::endl;
std::cout << c << std::endl;
}
Output:
this is a null char, backward slash zero
a
b
r
(note the empty line, where the \0
is being printed)
Compared to:
std::map<char,int> m;
std::string s = "bar";
for (char c : s) m[c] = 0;
for (auto [c, f] : m) {
if (c == '\0') std::cout << "this is a null char, backward slash zero" << std::endl;
std::cout << c << std::endl;
}
Output:
a
b
r
Aucun commentaire:
Enregistrer un commentaire