根据输入参数初始化C ++数组 [英] initialize C++ array based on input arguments

查看:103
本文介绍了根据输入参数初始化C ++数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立位集的二维数组,我想获得位集从用户。自从模板和值必须是常量我该怎么办呢?

  INT主(INT ARGC,CHAR *的argv []){
INT常量LEN;
诠释常量数;
如果(ARGC == 3)
{
    LEN =的atoi(ARGV [1]);
    NUMBER =的atoi(argv的[2]);
}
其他{
    LEN = 1000;
    NUMBER = 800;
}
位集合<&LEN GT; B〔NUMBER];
}


解决方案

如果你可以设置一个限制,你能有多大一个bitset大小使用,可以作弊。

  //调用目标< I> {}()如果我> =分钟,I<最大
模板<模板<为size_t>类的目标,为size_t最大,为size_t分钟= 0>
结构magic_switch {
  void运算符()(为size_t我)const的{
    constexpr为size_t中期=(最小值+最大-1)/ 2;
    如果(ⅰ== MAX-1)靶下;最大-1。> {}();
    否则如果(ⅰ&下;中旬)magic_switch<目标,中期,分钟> {}(ⅰ);
    别的magic_switch<目标,最大-1,中间> {}(ⅰ);
  }
};
模板<模板<为size_t>类的目标,为size_t分钟>
结构magic_switch<目标,分,分> {
  void运算符()(为size_t我)const的{} //什么也不做
};
INT编号;
模板<为size_t的len>
结构程序{
  模板<为size_t号>
  结构P2 {
    void运算符()()const的{
      和std :: bitset<&LEN GT; ARR [编号]
      // code
    }
  };
  void运算符()()const的{
    magic_switch&所述; P2,1001,1 GT; {}(数);
  }
};INT主(INT ARGC,字符常量*的argv []){
  INT LEN;
  如果(ARGC == 3)
  {
    LEN =的atoi(ARGV [1]);
    数=与atoi(argv的[2]);
  }
  magic_switch<程序,1001,1 GT; {}(LEN);
}

当然,这种做法完全是坚果和一个坏主意。

基本上,我创建了千万程序,每个LEN x个,然后做了一个巨大的链接,如果弄清楚跑哪一个。

这可以通过存储指向程序在一个数组,然后做一个数组查找,而不是20深的递归作出更好的性能。它仍然是一个坏主意。

不要使用此解决方案。如果你不明白为什么你不应该使用它,那么相信我。如果你这样做明白为什么你不应该使用它,那么这一段是多余的。

一个更快 magic_switch (一些编译器会不喜欢它,并使用C ++ 14功能):

 模板<模板<为size_t>类的目标,为size_t最大,为size_t分钟= 0>
结构magic_switch {
  模板<为size_t ...是>
  void运算符()(STD :: index_sequence<是...>中为size_t我){
    使用任务=无效(*)();
    静态常量任务ARR [] = {
      [] {目标<分+ IS> {}(); } ...
    };
    ARR [I-分]();
  }
  void运算符()(为size_t我)const的{
    (*此)(的std :: make_index_sequence<最大最小> {},我);
  }
};

在这里我们建立了一个跳转表(在静态常量数组),做了查找,并执行它。

扩展这一带/返回ARGS是比较容易的:

 模板<类SIG,模板<为size_t>类的目标,为size_t最大,为size_t分钟= 0>
结构magic_switch;
模板< R级,类参数... args,模板<为size_t>类的目标,为size_t最大,为size_t分钟>
结构magic_switch< R(参数...),目标,最大值,最小值> {
  模板<为size_t ...是>
   - [R运算符()(的std :: index_sequence<是...>中为size_t我,参数数量和放大器;&放大器;参数... args){
    使用任务= R(*)(参数和放大器;&安培; ...);
    静态常量任务ARR [] = {
      [](参数和放大器;&安培; ...参数) - > R {
        返回目标<分+ IS> {}(的std ::向前<&参数数量GT;(参数)...);
      } ...
    };
    返回ARR [I-分钟(的std ::向前<&参数数量GT;(参数)...);
  }
   - [R运算符()(为size_t我,参数数量参数... args){常量
    返回(*本)(
      的std :: make_index_sequence<最大最小> {},
      一世,
      的std ::向前<&参数数量GT;(参数)...
    );
  }
};

使用这个人会带走的数量变量在全球范围内存储的要求。由于全局变量是不好的做法,这提高了code。

I need to build 2D array of bitset, and I want to get the size of the array and size of bitset from user. Since its template and the value must be const how can I do that?

int main(int argc, char* argv[]) {
int const LEN ;
int const NUMBER;
if ( argc == 3 ) 
{
    LEN =  atoi(argv[1]);
    NUMBER =  atoi(argv[2]);
}
else{
    LEN = 1000;
    NUMBER = 800;
}
bitset<LEN> b[NUMBER];
}

解决方案

If you can set a limit to how big a bitset size you can use, you can cheat.

// invokes target<i>{}() if i >= min and i < max
template<template<size_t>class target, size_t max, size_t min=0>
struct magic_switch {
  void operator()( size_t i ) const {
    constexpr size_t mid = (min+max-1)/2;
    if (i==max-1) target<max-1>{}();
    else if(i<mid) magic_switch<target, mid,min>{}(i);
    else magic_switch<target, max-1,mid>{}(i);
  }
};
template<template<size_t>class target, size_t min>
struct magic_switch<target, min, min> {
  void operator()( size_t i ) const {} // do nothing
};
int number;
template<size_t len>
struct program {
  template<size_t number>
  struct p2 {
    void operator()() const {
      std::bitset<len> arr[number];
      // code
    }
  };
  void operator()() const {
    magic_switch<p2, 1001, 1>{}(number);
  }
};

int main(int argc, char const* argv[]) {
  int len;
  if ( argc == 3 ) 
  {
    len =  atoi(argv[1]);
    number =  atoi(argv[2]);
  }
  magic_switch<program, 1001, 1>{}( len );
}

of course, this approach is completely nuts and a bad idea.

Basically I created 10000000 programs, one for each len x number, then did a massive chained if to figure out which one to run.

This can be made more performant by storing pointers to the programs in an array, then doing an array lookup instead of a 20-deep recursion. It is still a bad idea.

Do not use this solution. If you don't understand why you shouldn't use it, then trust me. If you do understand why you shouldn't use it, then this paragraph is redundant.

A faster magic_switch (some compilers won't like it, and uses C++14 features):

template<template<size_t>class target, size_t max, size_t min=0>
struct magic_switch {
  template<size_t...Is>
  void operator()( std::index_sequence<Is...>, size_t i ) {
    using task = void(*)();
    static const task arr[] = {
      []{ target<min+Is>{}(); }...
    };
    arr[i-min]();
  }
  void operator()( size_t i ) const {
    (*this)( std::make_index_sequence<max-min>{}, i );
  }
};

here we build a jump table (in a static const array), do a lookup, and execute it.

Extending this to take/return args is relatively easy:

template<class Sig, template<size_t>class target, size_t max, size_t min=0>
struct magic_switch;
template<class R, class...Args, template<size_t>class target, size_t max, size_t min>
struct magic_switch<R(Args...), target, max, min> {
  template<size_t...Is>
  R operator()( std::index_sequence<Is...>, size_t i, Args&&...args ) {
    using task = R(*)(Args&&...);
    static const task arr[] = {
      [](Args&&...args)->R{
        return target<min+Is>{}(std::forward<Args>(args)...);
      }...
    };
    return arr[i-min](std::forward<Args>(args)...);
  }
  R operator()( size_t i, Args...args ) const {
    return (*this)(
      std::make_index_sequence<max-min>{},
      i,
      std::forward<Args>(args)...
    );
  }
};

Using this one will take away the requirement that the number variable be stored globally. As global variables are bad practice, this improves the code.

这篇关于根据输入参数初始化C ++数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆