C++中向量的通用向量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/293988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Generic vector of vectors in C++
提问by Steve Jessop
Is there a good way in C++ to implement (or fake) a type for a generic vector of vectors?
在 C++ 中是否有一种好方法来实现(或伪造)通用向量向量的类型?
Ignore the issue of when a vector of vectors is a good idea (unless there's something equivalent which is always better). Assume that it does accurately model the problem, and that a matrix does not accurately model the problem. Assume also that templated functions taking these things as parameters do need to manipulate the structure (e.g. calling push_back), so they can't just take a generic type supporting [][]
.
忽略向量向量何时是一个好主意的问题(除非有一些等效的东西总是更好)。假设它确实准确地对问题建模,而矩阵不能准确地对问题建模。还假设将这些东西作为参数的模板化函数确实需要操纵结构(例如调用 push_back),因此它们不能只采用支持[][]
.
What I want to do is:
我想做的是:
template<typename T>
typedef vector< vector<T> > vecvec;
vecvec<int> intSequences;
vecvec<string> stringSequences;
but of course that's not possible, since typedef can't be templated.
但当然这是不可能的,因为 typedef 不能被模板化。
#define vecvec(T) vector< vector<T> >
is close, and would save duplicating the type across every templated function which operates on vecvecs, but would not be popular with most C++ programmers.
很接近,并且可以避免在每个在 vecvecs 上运行的模板化函数中复制类型,但不会受到大多数 C++ 程序员的欢迎。
回答by Johannes Schaub - litb
You want to have template-typedefs. That is notyet supported in the current C++. A workaround is to do
你想要模板类型定义。这是不被支持在当前的C ++。解决方法是做
template<typename T>
struct vecvec {
typedef std::vector< std::vector<T> > type;
};
int main() {
vecvec<int>::type intSequences;
vecvec<std::string>::type stringSequences;
}
In the next C++ (called c++0x, c++1x due to 2010), this would be possible:
在下一个 C++(由于 2010 年称为 c++0x、c++1x)中,这将是可能的:
template<typename T>
using vecvec = std::vector< std::vector<T> >;
回答by ?ukasz Milewski
I use Boost.MultiArraywhich is implemented in the boost library.
我使用在 boost 库中实现的Boost.MultiArray。
HTH
HTH
回答by Rexxar
You can simply create a new template :
您可以简单地创建一个新模板:
#include <string>
#include <vector>
template<typename T>
struct vecvec : public std::vector< std::vector<T> > {};
int main()
{
vecvec<int> intSequences;
vecvec<std::string> stringSequences;
}
If you do that you have to remember that destructor of vector is not virtual and not to do something like this :
如果你这样做,你必须记住 vector 的析构函数不是虚拟的,不要做这样的事情:
void test()
{
std::vector< std::vector<int> >* pvv = new vecvec<int>;
delete pvv;
}
回答by mloskot
You can implement basic vector-of-vector type using std::vector
as a basis:
您可以使用std::vector
作为基础来实现基本的 vector-of-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;
}
It's just a very simple container that mimics a matrix (as long as user promises, by improving vecvec definition or by proper use, rectangular shape).
它只是一个模拟矩阵的非常简单的容器(只要用户承诺,通过改进 vecvec 定义或通过正确使用,矩形形状)。