2008-11-16 79 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定義或正確使用,矩形)。