2008-11-16 81 views
48

C++中是否有一种很好的方法来实现(或伪造)一个通用的向量向量类型?C++中的向量通用向量

忽略矢量矢量何时是个好主意的问题(除非有相同的东西总是更好)。假设它确实对问题建模,并且矩阵不能准确地模拟问题。假设以这些参数为参数的模板化函数需要操纵结构(例如调用push_back),所以它们不能只支持[][]的通用类型。

我想要做的是:

template<typename T> 
typedef vector< vector<T> > vecvec; 

vecvec<int> intSequences; 
vecvec<string> stringSequences; 

当然这是不可能的,因为类型定义不能作为模板的,但是。

#define vecvec(T) vector< vector<T> > 

是接近的,并且可以节省重复横跨其上运行vecvecs每个模板函数的类型,但不会是流行的大多数C++程序员。

回答

51

你想要有模板类型定义。那就是而不是在当前的C++中仍然支持。一种解决方法是做

template<typename T> 
struct vecvec { 
    typedef std::vector< std::vector<T> > type; 
}; 

int main() { 
    vecvec<int>::type intSequences; 
    vecvec<std::string>::type stringSequences; 
} 

在接下来的C++(所谓的C++ 0x,C++ 1X由于至2010年),这将是可能的:

template<typename T> 
using vecvec = std::vector< std::vector<T> >; 
+13

我认为他们也已经修复了>> – 2008-11-17 22:56:35

+0

的空格需求的确,他们有:) – 2008-11-18 00:54:16

4

您可以简单地创建一个新的模板:

#include <string> 
#include <vector> 

template<typename T> 
struct vecvec : public std::vector< std::vector<T> > {}; 

int main() 
{ 
    vecvec<int> intSequences; 
    vecvec<std::string> stringSequences; 
} 

如果你这样做,你要记住载体的那析构函数不是虚拟的,而不是做这样的事情:

void test() 
{ 
    std::vector< std::vector<int> >* pvv = new vecvec<int>; 
    delete pvv; 
} 
+4

你会遇到损失矢量的所有方便的构造函数。您需要定义它们,只需将参数传递给父级。一种可能性,但不是一种精益求精的解决方案。 – xtofl 2008-11-19 19:58:41

+0

-1答案本身是完美的,你指出了解决方案的一个主要缺陷。但是,这种缺陷和其他缺陷已经足以让人产生厌恶情绪。 – 2011-06-13 10:10:29

2

可以使用std::vector为基础实现矢量矢量的-基本类型:

#include <iostream> 
#include <ostream> 
#include <vector> 
using namespace std; 

template <typename T> 
struct vecvec 
{ 
    typedef vector<T> value_type; 
    typedef vector<value_type> type; 
    typedef typename type::size_type size_type; 
    typedef typename type::reference reference; 
    typedef typename type::const_reference const_reference; 

    vecvec(size_type first, size_type second) 
     : v_(first, value_type(second, T())) 
    {} 

    reference operator[](size_type n) 
    { return v_[n]; } 

    const_reference operator[](size_type n) const 
    { return v_[n]; } 

    size_type first_size() const 
    { return v_.size(); } 

    size_type second_size() const 
    { return v_.empty() ? 0 : v_[0].size(); } 

    // TODO: replicate std::vector interface if needed, like 
    //iterator begin(); 
    //iterator end(); 

private: 
    type v_; 

}; 

// for convenient printing only 
template <typename T> 
ostream& operator<<(ostream& os, vecvec<T> const& v) 
{ 
    typedef vecvec<T> v_t; 
    typedef typename v_t::value_type vv_t; 
    for (typename v_t::size_type i = 0; i < v.first_size(); ++i) 
    { 
     for (typename vv_t::size_type j = 0; j < v.second_size(); ++j) 
     { 
      os << v[i][j] << '\t'; 
     } 
     os << endl; 
    } 
    return os; 
} 

int main() 
{ 
    vecvec<int> v(2, 3); 
    cout << v.first_size() << " x " << v.second_size() << endl; 
    cout << v << endl; 

    v[0][0] = 1; v[0][1] = 3; v[0][2] = 5; 
    v[1][0] = 2; v[1][1] = 4; v[1][2] = 6; 
    cout << v << endl; 
} 

这只是模仿矩阵(只要用户承诺一个非常简单的容器,由改善vecvec定义或正确使用,矩形)。