<tfoot id='yYENv'></tfoot>

    <i id='yYENv'><tr id='yYENv'><dt id='yYENv'><q id='yYENv'><span id='yYENv'><b id='yYENv'><form id='yYENv'><ins id='yYENv'></ins><ul id='yYENv'></ul><sub id='yYENv'></sub></form><legend id='yYENv'></legend><bdo id='yYENv'><pre id='yYENv'><center id='yYENv'></center></pre></bdo></b><th id='yYENv'></th></span></q></dt></tr></i><div id='yYENv'><tfoot id='yYENv'></tfoot><dl id='yYENv'><fieldset id='yYENv'></fieldset></dl></div>

    <small id='yYENv'></small><noframes id='yYENv'>

      <legend id='yYENv'><style id='yYENv'><dir id='yYENv'><q id='yYENv'></q></dir></style></legend>
      • <bdo id='yYENv'></bdo><ul id='yYENv'></ul>

      将变量名转换为 C++ 中的字符串

      时间:2023-05-24
            <bdo id='YSzbL'></bdo><ul id='YSzbL'></ul>
              <legend id='YSzbL'><style id='YSzbL'><dir id='YSzbL'><q id='YSzbL'></q></dir></style></legend>
              <i id='YSzbL'><tr id='YSzbL'><dt id='YSzbL'><q id='YSzbL'><span id='YSzbL'><b id='YSzbL'><form id='YSzbL'><ins id='YSzbL'></ins><ul id='YSzbL'></ul><sub id='YSzbL'></sub></form><legend id='YSzbL'></legend><bdo id='YSzbL'><pre id='YSzbL'><center id='YSzbL'></center></pre></bdo></b><th id='YSzbL'></th></span></q></dt></tr></i><div id='YSzbL'><tfoot id='YSzbL'></tfoot><dl id='YSzbL'><fieldset id='YSzbL'></fieldset></dl></div>
              <tfoot id='YSzbL'></tfoot>
                  <tbody id='YSzbL'></tbody>

              • <small id='YSzbL'></small><noframes id='YSzbL'>

              • 本文介绍了将变量名转换为 C++ 中的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我想将一些数据输出到文件中.例如假设我有两个双精度向量:

                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); 
                

                是否有一种简单的方法可以将其输出到文件中,以便第一行包含标题data1"和data2",然后是实际内容.的功能输出数据将传递各种不同的数组,因此对名称进行硬编码标题是不可能的 - 理想情况下我想转换变量名称到某个字符串,然后输出该字符串,后跟向量数组的内容.但是,我不确定如何将变量名 'data1' 转换为字符串,或者确实如果它可以轻松完成(从阅读论坛我猜它不能)如果这是不可能的,替代方法可能是使用关联容器,例如地图或更简单的配对"容器.

                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');  
                

                欢迎提出任何建议!

                推荐答案

                您可以使用预处理器stringify"# 做您想做的事:

                You can use the preprocessor "stringify" # to do what you want:

                #include <stdio.h>
                
                #define PRINTER(name) printer(#name, (name))
                
                void printer(char *name, int value) {
                    printf("name: %s	value: %d
                ", name, value);
                }
                
                int main (int argc, char* argv[]) {
                    int foo = 0;
                    int bar = 1;
                
                    PRINTER(foo);
                    PRINTER(bar);
                
                    return 0;
                }
                
                
                name: foo   value: 0
                name: bar   value: 1
                

                (对不起,printf,我从来没有掌握 的窍门.但这应该足够了.)

                (Sorry for printf, I never got the hang of <iostream>. But this should be enough.)

                这篇关于将变量名转换为 C++ 中的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:前面为 0 的 C++ int 更改整个值 下一篇:const 引用的字面初始化

                相关文章

                最新文章

                  <bdo id='kCOE2'></bdo><ul id='kCOE2'></ul>

                    <i id='kCOE2'><tr id='kCOE2'><dt id='kCOE2'><q id='kCOE2'><span id='kCOE2'><b id='kCOE2'><form id='kCOE2'><ins id='kCOE2'></ins><ul id='kCOE2'></ul><sub id='kCOE2'></sub></form><legend id='kCOE2'></legend><bdo id='kCOE2'><pre id='kCOE2'><center id='kCOE2'></center></pre></bdo></b><th id='kCOE2'></th></span></q></dt></tr></i><div id='kCOE2'><tfoot id='kCOE2'></tfoot><dl id='kCOE2'><fieldset id='kCOE2'></fieldset></dl></div>
                    <legend id='kCOE2'><style id='kCOE2'><dir id='kCOE2'><q id='kCOE2'></q></dir></style></legend>

                    <small id='kCOE2'></small><noframes id='kCOE2'>

                  1. <tfoot id='kCOE2'></tfoot>