阵列不同尺寸
阵列的我已经一些代码,产生一组TR1不同尺寸的阵列的::,但相同的类型,像阵列不同尺寸
array<int, 2>
array<int, 4>
array<int, 6>
这些阵列的数,并且它们的尺寸,都是在编译时给出的,所以我确切地知道它们中会有多少,以及每一个的大小(但它们可能不同)。
问题:我想把它们放在一个集合中(使用数组<>会很棒),但是所有成员的类型必须相同,事实并非如此。
我想过使用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());
}
您可以将不同类型的类放入STL容器的唯一方法是,如果容器包含指针(引用不工作,因为它们不是默认可构造的),而是指向某个基类型,并且要收集的对象全部继承从那种类型。请注意,从基类继承的类型的容器(或任何模板类)不会从基类型的容器继承。你可以使用void *,但是你需要做很多丑陋和危险的演员,你必须记得自己释放内存。你为什么不写一个固定大小的数组类,允许你在构造函数中设置大小?如果你写作araray的包装,它不应该是太多的工作。如果你想使用一些基于智能指针的解决方案,不要试图使用auto_ptr,因为STL容器的复制语义是错误的 - 请参阅boost shared_ptr。
你不会说你为什么要收集不同大小的静态大小的数组。这很奇怪。为什么不使用动态大小的数组的集合?
你的选择是:
使用std ::载体,而不是性病:: TR1 ::阵列。
存储指向数组的指针和数组的大小。这看起来像这样:
std::vector<std::pair<int *, size_t> >
。只要确保阵列的生命周期至少与载体的寿命一样长!我希望boost :: variant也可以工作,但在实践中使用会非常费力。
boost::variant<array<int, 2>, array<int, 4>, array<int, 6>, ... >
'的std ::矢量'可能会是最简单的。只是不要改变大小。诚然,它不完全相同,但它很干净。 – GManNickG 2010-07-28 00:17:56
是的,我已经有一个使用std :: vector而不是tr1 :: array的实现,但是使用动态大小是毫无意义的,因为它们可以在编译时计算,所以我一直在寻找一个静态解决方案。 – AkiRoss 2010-07-28 00:30:59
你可能会喜欢其中一个容器:http://www.boost.org/doc/libs/1_43_0/libs/fusion/doc/html/fusion/container.html。另外,如果您想回复某人,请使用@,就像@aaa – Anycorn 2010-07-28 00:42:38