c++17倍表达式打印函数,可高效支持20000元素

标签 c++ c++17 fold-expression

你好,如何才能更有效地打印像 20,000 元素这样的大元素? 我花了很长时间来编译, 我试着按照这个 static const array of values in Variadic Template C++

但我不能让它与折叠表达式一起工作,任何解决这个问题的想法

#include <iostream>


template<typename T = int , typename Comp=std::less<T>>
struct Facility
{
  template<T ... list> 
  struct List
  {
    static void print()
    {
      // store in static array to compile faster
      constexpr static T args[sizeof...(list)] = {list...}; 
      // use for idiom for each except the last, but this time in reserve way
      // assgn char* space to empty char first
      const char* space = "";
      if (sizeof...(list) == 0)
      {
        // simple just call space to cout , if not warning no variable used
        std::cout <<'"' <<"Empty List" <<'"' << space  <<std::endl;
      }
      else if  (std::is_same<T, char>::value)
      {
        std::cout << '"';
       ((std::cout << space << args), ...);// compile error
        // simple just call space to cout , if not warning no variable used
        std::cout << '"' << space<< std::endl;
      }
      else
      {
        std::cout << '"';
        // fold expression bracket,L side will execute first 
        // the first round space is empty and later on become space
        (((std::cout << space << list), space = " "), ...);
        std::cout << '"' << std::endl;
      }    
    }  
  }; 




template<int ... intlist>
using IntList = typename Facility<int>::List<intlist...>;

template<char ... charlist>
using CharList = typename Facility<char>::List<charlist...>;

template<short ... shortlist>
using ShortList = typename Facility<short>::List<shortlist...>;

template<unsigned short ... shortlist>
using UnsignedShortList = typename Facility<unsigned short>::List<shortlist...>;

template<long ... list>
using LongList = typename Facility<long>::List<list...>;

int main()
{
    std::cout << "********Testing Basic List Capabilities*********\n";  
    using List1 = IntList<1,2,3,4>;
    using List2 = IntList<9, 0, -1, -200>;
    using List3 = IntList<LONG20000LIST>;
    List1::print();
    List2::print();
    List3::print();

    using String1 = CharList<'a', 'b', 'c'>;
    using String2 = CharList<' ', 'w', 'o', 'r', 'l', 'd' >;
    using EmptyString = CharList<>;
    String1::print();
    String2::print();
    EmptyString::print();
    std::cout << "************Finished Testing *******************\n\n";
}

最佳答案

这是我的解决方案,感谢大家的帮助

static void print()
{
  // store in static array to compile faster
  std::vector<T> args = {list...};
  // use for idiom for each except the last, but this time in reserve way
  // assgn char* space to empty char first
  const char* space = "";
  if (sizeof...(list) == 0)
  {
    // simple just call space to cout , if not warning no variable used
    std::cout <<'"' <<"Empty List" <<'"' << space  <<std::endl;
  }
  else if  (std::is_same<T, char>::value)
  {
    std::cout << '"';
    for (auto const& elem : args)
    {
        std::cout << space << elem; 
    }
    std::cout << '"' << space<< std::endl;
  }
  else
  {
     std::cout << '"';
     for (auto const& elem : args)
     {
        std::cout << space << elem; 
        space = " ";
     }
     std::cout << '"' << std::endl;
  }    
}  

关于c++17倍表达式打印函数,可高效支持20000元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51438375/

相关文章:

c++ - QPropertyAnimation : Immedately jump to end of animation?

c++ - 如何使用 c++ libboost 运行进程并获取其输出?

c++ - 静态意味着内联或反之亦然或两者兼而有之?

c++ - 与给定类型相似的最广泛可能类型 - C++

c++ - 在 C++ 中转换 GetProcAddress 返回的指针

c++ - 如何从嵌套命名空间中引用外部 C++ 命名空间?

还包括现有变量的 C++17 结构化绑定(bind)

c++ - 用运算符折叠表达式 >>

c++ - C++17 中的折叠表达式 - 比较运算符的用例

C++最有效的方法迭代 vector 中的特定内容