c++ - c++17折叠表达式打印功能,可支持20000个元素高效
问题描述
您好,如何使用 20,000 元素等大元素使其打印效率更高?我花了很长时间编译,我尝试 在可变参数模板 C++ 中遵循这个静态 const 值数组
但我不能让它与折叠表达式一起工作,任何想法如何解决这个问题
#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;
}
}
推荐阅读
- linux - 匹配一个模式,然后转到下一个条件并打印详细信息
- javascript - 过滤与字符序列匹配的 JavaScript 字符串数组
- mysql - 如何从 tableB 中选择 tableB_name 并加入 mysql 中的 tableA?
- c# - 如何在 Oracle 和 C# 中获得相同的数字哈希?
- mysql - 重新排列行顺序查询仅在行数为奇数时有效
- javascript - 如何使两个日期选择器在 html 中相互依赖?
- xamarin - 为什么 Xamarin Forms Webview 绑定不会在 iOS 上刷新?
- web-development-server - Q# 是否支持 Web 开发?
- ms-access - 在单个查询或报告中执行多个分类
- python - 多处理 - 数量对处理时间的影响