gpt4 book ai didi

c++ - 不同大小的数组

转载 作者:塔克拉玛干 更新时间:2023-11-03 02:06:00 25 4
gpt4 key购买 nike

我有一些代码可以生成一组不同大小但类型相同的 tr1::array,例如

array<int, 2>
array<int, 4>
array<int, 6>

这些数组的数量,以及它们的大小,在编译时给出,所以我确切地知道它们会有多少以及每个有多大(但它们可能不同)。

问题:我想将它们放在一个集合中(使用 array<> 会很好),但是所有成员的类型必须相等,但事实并非如此。

我考虑过使用 boost::variant,但是如何使用编译时确定的类型列表来指定变体(我正在考虑大量使用预处理器...)?使用 boost::any 怎么样?其他方法? (野指针?)

TIA~秋

更正:预处理器在这种情况下不可用。

最佳答案

我会使用 Boost 的 MPL 和 Fusion 库。有两种方法可以结束类型列表:生成它们或显式定义它们。前者更灵活一些,但很难说哪种适合您,因为我们不知道您如何获得您拥有的值。

无论如何,生成:

#include <boost/mpl/for_each.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/transform.hpp>
#include <boost/mpl/vector.hpp>
#include <array>
#include <iostream>

namespace bmpl = boost::mpl;

// turns an index into an array
template <typename T>
struct make_array
{
// or whatever scheme you have
static const std::size_t size = T::value * 2;

// define generated type
typedef std::array<int, size> type;
};

// list of values to convert
typedef bmpl::range_c<size_t, 1, 10> array_range;

// transform that list into arrays, into a vector
typedef bmpl::transform<array_range, make_array<bmpl::_1>,
bmpl::back_inserter<bmpl::vector<>>
>::type array_collection;

或明确说明:

#include <boost/mpl/vector.hpp>
#include <array>
#include <iostream>

namespace bmpl = boost::mpl;

// list all array types
typedef bmpl::vector<
std::array<int, 2>,
std::array<int, 4>,
std::array<int, 6>,
std::array<int, 8>,
std::array<int, 10>,
std::array<int, 12>,
std::array<int, 14>,
std::array<int, 16>,
std::array<int, 18>
> array_collection;

无论哪种方式,您都可以像这样使用它:

#include <boost/fusion/algorithm.hpp>
#include <boost/fusion/container/vector.hpp>
#include <boost/fusion/mpl.hpp>
#include <boost/fusion/sequence.hpp>
#include <boost/mpl/for_each.hpp>
#include <typeinfo>

// fusion "fuses" the bridge between MPL and runtime
namespace bf = boost::fusion;

struct print_type
{
template <typename T>
void operator()(const T&) const
{
std::cout << typeid(T).name() << "\n";
}
};

struct print_values
{
template <typename T>
void operator()(const T& pArray) const
{
std::cout << "Printing array with size "
<< pArray.size() << ":\n";
std::for_each(pArray.begin(), pArray.end(),
[](int pX)
{
std::cout << pX << " ";
});
std::cout << std::endl;
}
};

int main(void)
{
// print all the types you have
bmpl::for_each<array_collection>(print_type());
std::cout.flush();

// make a usable type out of the typelist
typedef bf::result_of::as_vector<array_collection>::type array_fusion;
array_fusion arrays; // now have an array of different arrays,
// compile-time generated but run-time usable

// access like this:
bf::at_c<0>(arrays)[1] = 5;
bf::at_c<1>(arrays)[2] = 7;
bf::at_c<2>(arrays)[0] = 135;

// for_each:
bf::for_each(arrays, print_values());
}

关于c++ - 不同大小的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3348936/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com