在 C++ 中将变量名转换为字符串
我想将一些数据输出到文件中。例如,假设我有两个双精度向量:
vector<double> data1(10);
vector<double> data2(10);
是否有一种简单的方法可以将其输出到文件,以便第一行包含标题“data1”和“data2”,后跟实际内容。该函数 输出数据将传递到各种不同的数组,因此对名称进行硬编码 标题的内容是不可能的 - 理想情况下我想转换变量名称 到某个字符串,然后输出该字符串,后跟向量数组的内容。但是,我不确定如何将变量名“data1”转换为字符串, 或者事实上,如果它可以很容易地完成(通过阅读论坛,我的猜测是它不能) 如果这不可能,另一种选择可能是使用关联 诸如地图之类的容器或更简单地说是“对”容器。
pair<vector<double>,string> data1(10,'data1');
欢迎任何建议!
I'd like to output some data to a file. For example assume I have two vectors of doubles:
vector<double> data1(10);
vector<double> data2(10);
is there an easy way to output this to a file so that the first row contains the headings 'data1' and 'data2' followed by the actual contents. The function which
outputs the data will be passed various different arrays so hardcoding the name
of the heading is not possible - ideally I'd like to convert the variable name
to some string and then output that string followed by the contents of the vector array. However, I'm not sure how to convert the variable name 'data1' to a string,
or indeed if it can easily be done (from reading the forums my guess is it can't)
If this is not possible an alternative might be to use an associative
container such as map or perhaps more simply a 'pair' container.
pair<vector<double>,string> data1(10,'data1');
Any suggestions would be welcome!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
您可以使用预处理器“stringify”
#
来执行您想要的操作:(对
printf
感到抱歉,我从未掌握
的窍门>.但这应该足够了。)You can use the preprocessor "stringify"
#
to do what you want:(Sorry for
printf
, I never got the hang of<iostream>
. But this should be enough.)试试这个:
//在函数中
try this:
//in functions
我也有同样的问题。经过一些实验后,我创建了以下宏,将变量、字段、函数、方法和类型的名称转换为字符串。
该代码使用逗号运算符和 void 转换来强制编译器检查变量、函数等是否确实存在。好处是它也可以很好地处理未初始化的变量。我在 VC 和 GCC 上测试了它,并使用我发现的所有迂腐选项,没有任何警告消息。
当我编写从输入流读取数据的解析器时,我使用这样的代码,如果解析的变量超出范围,它会抛出一个异常,该异常的变量名称未通过有效性检查。
I had the same problem. After a little bit of experimentation I created following macros that convert names of variables, fields, functions, methods and types to strings.
The code uses comma operator and void conversion to force compiler to check if variable, function, etc. really exists. The nice thing is that it works well with uninitialized variables too. I tested it on both VC and GCC with all pedantic options I found out without any warning messages.
I use such code when I write parsers that reads data from input stream and if parsed variable is out of bounds it throws an exception with name of variable that failed my validity checks.
稍微改编自 @sarnold 的答案,对于 C++:
使用此的示例程序:
Slightly adapted from @sarnold's answer, for C++:
An example program which uses this:
您可以使用预处理器,有一个 stringify 标记,但它只能从源中获得,不能从函数中获得(您将获得参数名称)。
You can use the preprocessor, there's a stringify token, but it's only available from the source, not to a function (you'd get the argument name).
我也有类似的追求。在 Qt 中,我厌倦了在写入
qDebug()
时不断地将变量名称写为字符串而不自动完成。经过对不同宏和函数的大量试验和错误后,我发现这个宏效果很好:
示例用法:
输出:
我猜这个(或非常类似的东西)将适用于
std::cout
作为出色地。聚会有点晚了,但我希望这可以帮助任何人!
I had a similar quest. In Qt, I got tired of constantly writing the variable name as a string without autocomplete when writing to
qDebug()
.After a lot of trial and error with different macros and functions, I found that this macro works great:
Example usage:
Output:
I guess this (or something very similar) will work for
std::cout
as well.A bit late to the party, but I hope this can help anyone out there!
我认为明显的答案是使执行输出的函数将标题文本作为字符串参数。
I'd have thought the obvious answer is to make the function that performs the output take the heading text as a string parameter.
对于这种情况,我制作了 nameof() 宏。它返回变量、类型或成员的 std::string 名称。它的工作方式类似于 C# 中的 nameof()。
例如:
For this case I have made nameof() macro. It returns a std::string name of a variable, type or member. It works like nameof() in C#.
For Example: