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

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

      • <bdo id='aa54B'></bdo><ul id='aa54B'></ul>
      <legend id='aa54B'><style id='aa54B'><dir id='aa54B'><q id='aa54B'></q></dir></style></legend>
    1. 在 C++ 中迭代结构

      时间:2023-07-01
      <i id='8qy7a'><tr id='8qy7a'><dt id='8qy7a'><q id='8qy7a'><span id='8qy7a'><b id='8qy7a'><form id='8qy7a'><ins id='8qy7a'></ins><ul id='8qy7a'></ul><sub id='8qy7a'></sub></form><legend id='8qy7a'></legend><bdo id='8qy7a'><pre id='8qy7a'><center id='8qy7a'></center></pre></bdo></b><th id='8qy7a'></th></span></q></dt></tr></i><div id='8qy7a'><tfoot id='8qy7a'></tfoot><dl id='8qy7a'><fieldset id='8qy7a'></fieldset></dl></div>

        • <bdo id='8qy7a'></bdo><ul id='8qy7a'></ul>
              <tbody id='8qy7a'></tbody>

          1. <legend id='8qy7a'><style id='8qy7a'><dir id='8qy7a'><q id='8qy7a'></q></dir></style></legend>
          2. <small id='8qy7a'></small><noframes id='8qy7a'>

          3. <tfoot id='8qy7a'></tfoot>

                本文介绍了在 C++ 中迭代结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个结构

                typedef struct A{一个;国际b;字符 * c;}aA;

                我想遍历结构的每个成员并打印其值.类似的东西:

                void print_struct_value(struct *A){对于结构 A 的每个成员cout<<结构名称.成员名称"<<价值";}

                如何在 C++ 中做到这一点??

                解决方案

                也许你可以使用 Boost Fusion/Phoenix 把一些东西串起来:

                nooli> rel="rar>!

                #include #include #include 使用 boost::phoenix::arg_names::arg1;#include <字符串>#include 结构A{一个;国际b;std::string c;};BOOST_FUSION_ADAPT_STRUCT(A, (int,a)(int,b)(std::string,c));int main(){const A obj = { 1, 42, "The Answer To LtUaE" };boost::fusion::for_each(obj, std::cout << arg1 << "
                ");}

                <块引用>

                更新:boost 的最新版本可以使用 C++11 类型推导:

                BOOST_FUSION_ADAPT_STRUCT(A,a,b,c);

                输出:

                142LtUaE 的答案

                I have a structure

                typedef struct A
                {
                    int a;
                    int b;
                    char * c;
                }aA;
                

                I want to iterate over each an every member of the structure and print its value. Something like:

                void print_struct_value(struct *A)
                {
                    for each member of struct A
                    cout << "struct name . member name" << "value";
                }
                

                How can this be done in C++ ??

                解决方案

                Perhaps you can string something together using Boost Fusion/Phoenix:

                See it live on Coliru!

                #include <boost/fusion/adapted/struct.hpp>
                #include <boost/fusion/include/for_each.hpp>
                #include <boost/phoenix/phoenix.hpp>
                using boost::phoenix::arg_names::arg1;
                
                #include <string>
                #include <iostream>
                
                struct A
                {
                    int a;
                    int b;
                    std::string c;
                };
                
                BOOST_FUSION_ADAPT_STRUCT(A, (int,a)(int,b)(std::string,c));
                
                int main()
                {
                    const A obj = { 1, 42, "The Answer To LtUaE" };
                
                    boost::fusion::for_each(obj, std::cout << arg1 << "
                ");
                }
                

                Update: Recent versions of boost can use C++11 type deduction:

                BOOST_FUSION_ADAPT_STRUCT(A,a,b,c);
                

                Output:

                1
                42
                The Answer To LtUaE
                

                这篇关于在 C++ 中迭代结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  1. <legend id='ZewSx'><style id='ZewSx'><dir id='ZewSx'><q id='ZewSx'></q></dir></style></legend>

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

                        <tbody id='ZewSx'></tbody>

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