如何在构建字符串后使用命名占位符对多行原始字符串使用sprintf
how to use sprintf on multiline raw string using named placeholders after building the string?
在Python、Perl或Ruby中,可以构建一个多行原始字符串(有时称为HERE-DOCUEMNT),然后在构建字符串后,使用命名参数替换字符串中的某些部分(使用%s
的令牌)。类似于在字符串上使用sprintf
,只是使用名称来帮助指向原始字符串中的这些位置。
这是非常有用的。我只能在C++中完成上面的部分操作,但不知道是否可以使用命名占位符进行字符串替换。
我将在Python中展示一个非常简单的例子,然后在C++中展示我的尝试
s=r"""
begin{document}
title{%(title)s}
This was written on %(date)s
And also updated on %(date)s and so on"""
print s % {"title": "main report","date": "1/1/2015"}
上面打印
begin{document}
title{main report}
This was written on 1/1/2015
And also updated on 1/1/2015 and so on
在C++中,我能做的最好的事情是:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
using namespace std;
int main()
{
char buffer [500];
string s =
R"(
begin{document}
title{%s}
This was written on %s
And also updated on %s and so on
)";
snprintf(buffer,500,s.c_str(),"main report","1/1/2015","1/1/2015");
cout<<buffer<<endl;
return 0;
}
现在
>g++ -Wall -std=c++0x ./t1.cpp
>./a.out
begin{document}
title{main report}
This was written on 1/1/2015
And also updated on 1/1/2015 and so on
有两个问题:如何在不必手动分配正确大小的缓冲区的情况下最好地执行sprintf
,以便能够保存新字符串(在替换后)?
第二个问题,是否可以像Python中的示例那样进行命名替换?这样,如果原始字符串中有许多占位符,并且其中一些占位符可能在不同的位置重复,如上例中的date
,则可以同时替换所有占位符。
Q2:不是命名替换,而是将重复项放入变量中,并在需要时使用该变量。
Q1:C++没有用于std::string
的内置格式字符串格式化程序。你可以安装一个插件,比如Boost或Poco来获得其中一个。但是像您那样使用snprintf
并没有什么问题。
另一种方法是在不格式化的情况下构建字符串:
string title = "main report";
string date = "1-01-2015";
string s = "(n\begin{document}n\title{" + arg + "}nn"
"This was written on " + date + "nn"
"And also updated on " + date + " and so onn)";
如果你想坚持使用snprintf
,可以调用它来确定缓冲区大小:snprintf(NULL, 0, formatstring, args......);
将返回要打印的字节数(所以多分配1)。在你的情况下,我不会打扰你,500已经足够了,如果你真的很担心,你可以检查snprintf
的返回值,如果超过500,就会触发错误。
有一个来自facebook的C++库,可以让你进行一些类似printf的命名格式化:
std::map<std::string, std::string> m { {"what", "answerswer"}, {"value", "42"} };
std::cout << vformat("The only {what} is {value}", m);
输出The only answer is 42
- 有没有办法从非C/C++文件中读取C++原始字符串文字的内容
- 如何在连接器 C++ 中将原始字节转换为字符串
- 将以 null 结尾的字节字符串转换为原始字符串文本
- 是否可以在原始字符串文本中插入转义序列?
- 如何找到修改后的字符串与原始字符串相等的时间
- 如何在c++中打印原始std::string/c样式的字符串
- 是否存在像C++中那样带有分隔符的C#原始字符串
- 反转由空格分隔的字符串元素将返回比原始字符串更大的字符串
- C++ 原始字符串 Unicode 文字
- 编码大于原始文本:如何获取零和一的字符串并将它们作为实际字节写入文件
- 在对原始字符串进行一些处理后返回(或转换)原始字符串
- 打印可以重复以获取原始字符串的最小字符串
- 如何从字符串变量为正则表达式构建原始字符串
- 如何制作包含原始字符串终止符C++原始字符串
- 如何在 C++ VS 中使用原始字符串文字(编码问题)
- 如何将变量用作原始 JSON 字符串中的数据?
- 什么是原始字符串?
- 转义 R "()" 在 C++ 中的原始字符串中
- 原始字符串比普通字符串快
- C++多行字符串原始文字