Question 13
Back to Exercise
What is the output of the following C++ program on a 64-bit system?
#include <iostream>
#include <string_view>
int main() {
std::string_view sv = "key=value;";
auto eq = sv.find('=');
std::string_view key = sv.substr(0, eq);
std::string_view val = sv.substr(eq + 1);
val.remove_suffix(1);
std::cout << key << '|' << val << '|' << sv.size() << '|' << val.size() << '\n';
return 0;
}
- A key|value|10|5 Correct
- B key|value;|10|6
- C key=|value|10|5
- D key|value|9|5
Correct Answer: Option A
substr on a view returns another view of the same characters – no copy. remove_suffix(1) drops the ';' from val only; sv still has all 10 characters.
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!