LearnFrenzy
Sign in Take a free test

Modern C++ Features (C++11 to C++23) · Predict the Output · Question 14

std::println with containers and ranges (C++23) Answer, explanation & discussion

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

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

main.cpp
#include <map>
#include <print>
#include <string>
#include <vector>

int main() {
    std::vector<int> v{1, 2, 3};
    std::map<std::string, int> m{{"b", 2}, {"a", 1}};
    std::println("{}", v);
    std::println("{}", m);
    std::println("{:>5}|{:<5}|", "x", 3.5);
    std::print("no newline ");
    std::println("then one");
    return 0;
}
  • A [1, 2, 3] {"a": 1, "b": 2} x|3.5 | no newline then one Correct
  • B 1 2 3 a 1 b 2 x|3.5 | no newline then one
  • C [1, 2, 3] {"b": 2, "a": 1} x | 3.5| no newline then one
  • D Compilation error
Correct Answer: Option A

C++23 can format ranges directly: vectors print as [..] and maps as {key: value} (strings quoted). println adds a newline, print does not.

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