LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 13

string_view substr and remove_prefix Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 13 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#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!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links