在引号中打印变量C ++ [英] Printing variable in quotation marks C++

查看:66
本文介绍了在引号中打印变量C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定:

string element = "hello";

我希望打印以下内容:

"hello" //with quotation marks

我发现了如何使用\"hello \"打印带引号的单词,但是我想使用变量名元素打印出"hello".

I found how to print words with quotation marks using \"hello\", but I would like to use my variable name element to print out "hello".

推荐答案

您可以获得的最接近的

string element = "hello";
std::cout << element

并打印

"hello"

是使用 std :: quoted 这样就可以编写代码

is to use std::quoted That would make the code

string element = "hello";
std::cout << std::quoted(element);

它将输出

"hello"


请注意, std :: quoted 的作用还不止于此,只需加上引号即可.如果您的字符串包含引号,它将对其进行修改并在其中添加 \ .那是


Do note that std::quoted does more then just add the out quotes. If your string contains quotes then it will modify those and add a \ in from of them. That means

string element = "hello \"there\" bob";
std::cout << std::quoted(element);

将打印

"hello \"there\" bob"

代替

"hello "there" bob"

这篇关于在引号中打印变量C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆