C ++ STL valarrays与向量 [英] C++ STL valarrays vs. vectors

查看:55
本文介绍了C ++ STL valarrays与向量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在C ++

程序中表示数字或bool类型的1D和2D数组。我想要的应用程序中的数组大小是

动态,因为它们在编译时是不可知的,所以我想要b $ b喜欢使用STL容器类模板,如valarray或vector

表示1D数组,valarrays或向量的valarrays或

向量表示2D数组。


正如我所说,在我的预期应用程序中阵列的大小是动态的,因为它们是在运行时确定的,但另一方面

我不喜欢在调用声明它的例程时,预计需要在

a期间不断调整给定数组的大小。对我的应用程序来说更重要的是速度。 valarrays与矢量的元素访问速度之间是否存在显着的差异?


对于1D阵列,选择是:

valarray< type>与矢量< type>


对于2D数组,选项包括:

valarray<的valarray<类型> > vs. valarray<矢量<类型> > vs.

vector<的valarray<类型> >与矢量<矢量<类型> >


在我的应用程序中,键入将是标量类型,例如bool,double,

int或size_t。


代码最初是针对Linux的,但它也可能是移植到另一个UNIX(如IRIX,或者甚至是Windows),所以我会

喜欢使用STL容器来保持便携性,而不是来自某些操作系统特定模板库的一个




-Michael

I need to represent 1D and 2D arrays of numeric or bool types in a C++
program. The sizes of the arrays in my intended application are
dynamic in the sense that they are not known at compile time, so I''d
like to use an STL container class template such as valarray or vector
to represent 1D arrays, and valarrays or vectors of valarrays or
vectors to represent 2D arrays.

As I said the sizes of the arrays in my intended application are
dynamic in that they are determined at run time, but on the other hand
I don''t anticipate the need to continually resize a given array during
a given call to the routine in which it is declared. The thing that''s
more important to my application is speed. Is there a significant
different between the element access speeds of valarrays vs. vectors?

For 1D arrays, the choices are:
valarray<type> vs. vector<type>

For 2D arrays, the choices are:
valarray< valarray<type> > vs. valarray< vector<type> > vs.
vector< valarray<type> > vs. vector < vector<type> >

In my application, type is will be a scalar type such as bool, double,
int, or size_t.

The code is initially being targeted for Linux, but it may also be
ported to another UNIX such as IRIX, or perhaps even Windows, so I''d
like to keep things portable by using an STL container rather than one
from some operating system specific template library.

-Michael

推荐答案

" Michael Aramini" < ... M ******* @ Verizon.net>在消息中写道

新闻:Se ******************* @ nwrdny02.gnilink.net ...
"Michael Aramini" <M.*******@Verizon.net> wrote in message
news:Se*******************@nwrdny02.gnilink.net...
我需要在C ++
程序中表示数字或bool类型的1D和2D数组。我想要的应用程序中的数组大小是动态的,因为它们在编译时是不可知的,所以我想要使用STL容器类模板,如valarray或vector
表示1D数组,valarray或矢量valarrays或
向量来表示2D数组。

正如我所说的,在我的预期应用程序中的数组大小是
动态,因为它们是在运行时确定的,但另一方面
我不希望在给定的例程调用期间继续调整给定数组的大小。对我的应用来说更重要的是速度。 valarrays与矢量的元素访问速度之间是否存在显着差异?

对于1D阵列,选择是:
valarray< type> vs. vector< type>

对于2D数组,选项包括:
valarray<的valarray<类型> > vs. valarray<矢量<类型> > vs.
vector<的valarray<类型> >与矢量<矢量<类型> >

在我的应用程序中,type将是一个标量类型,如bool,double,
int或size_t。

代码最初是针对性的对于Linux,但它也可能被移植到另一个UNIX,如IRIX,甚至可能是Windows,所以我想通过使用STL容器而不是一个容器来保持便携性。 >来自某些特定于操作系统的模板库。

-Michael
I need to represent 1D and 2D arrays of numeric or bool types in a C++
program. The sizes of the arrays in my intended application are
dynamic in the sense that they are not known at compile time, so I''d
like to use an STL container class template such as valarray or vector
to represent 1D arrays, and valarrays or vectors of valarrays or
vectors to represent 2D arrays.

As I said the sizes of the arrays in my intended application are
dynamic in that they are determined at run time, but on the other hand
I don''t anticipate the need to continually resize a given array during
a given call to the routine in which it is declared. The thing that''s
more important to my application is speed. Is there a significant
different between the element access speeds of valarrays vs. vectors?

For 1D arrays, the choices are:
valarray<type> vs. vector<type>

For 2D arrays, the choices are:
valarray< valarray<type> > vs. valarray< vector<type> > vs.
vector< valarray<type> > vs. vector < vector<type> >

In my application, type is will be a scalar type such as bool, double,
int, or size_t.

The code is initially being targeted for Linux, but it may also be
ported to another UNIX such as IRIX, or perhaps even Windows, so I''d
like to keep things portable by using an STL container rather than one
from some operating system specific template library.

-Michael




我不知道valarray和valarray之间存在任何严重的访问速度差异

矢量。但是,我会说整体valarray并不是设想为矢量。


对于2D阵列,你也有其他选择。例如,你可以分配一个

一维数组并从行索引和列索引计算索引。

是否更快取决于你想要的算法申请。

例如,复制矩阵几乎肯定会更快。


我建议您在编写之前搜索一些数字库

拥有2D数组类型。这是以前做过的。


-

Cy
http://home.rochester.rr.com/cyhome/


Michael Aramini写道:
Michael Aramini wrote:
我需要在C ++
程序中表示数字或bool类型的1D和2D数组。我想要的应用程序中的数组大小是动态的,因为它们在编译时是不可知的,所以我想要使用STL容器类模板,如valarray或vector
表示1D数组,valarray或矢量valarrays或
向量来表示2D数组。

正如我所说的,在我的预期应用程序中的数组大小是
动态,因为它们是在运行时确定的,但另一方面
我不希望在给定的调用例程期间不断调整给定数组的大小。
对我的应用来说更重要的是速度。


标准矢量类模板适用于任何类型的* flexible *数组。

矢量矢量中的每个矢量可以有不同的长度。 />
他们只会给你带来麻烦

如果你真的需要* rigid *,*矩形*数组。

在这种情况下,标准valarray类模板是一个更好的选择。

valarrays与向量的元素访问速度之间是否存在显着差异?


这取决于实现

但是valarrays是针对高性能数值应用所需的优化而设计的.b $ b 。

对于1D阵列,选择是:
valarray< type> vs. vector< type>

对于2D数组,选项包括:
valarray<的valarray<类型> > vs. valarray<矢量<类型> > vs.
vector<的valarray<类型> >与矢量<矢量<类型> >


No.使用


valarray< type>


和标准切片模板视图"它作为矩阵[tensor]。

参见Bjarne Stroustrup,The C ++ Programming Language:Third Edition,

第22章数字,第4节矢量算术,第662页-79。

在我的应用程序中,type将是一个标量类型,如bool,double,
int或size_t。

代码最初是针对Linux,但它也可能被移植到另一个UNIX,如IRIX,甚至可能是Windows,所以我想通过使用STL容器而不是一个容器来保持便携性。来自某些特定于操作系统的模板库。
I need to represent 1D and 2D arrays of numeric or bool types in a C++
program. The sizes of the arrays in my intended application are
dynamic in the sense that they are not known at compile time, so I''d
like to use an STL container class template such as valarray or vector
to represent 1D arrays, and valarrays or vectors of valarrays or
vectors to represent 2D arrays.

As I said the sizes of the arrays in my intended application are
dynamic in that they are determined at run time, but on the other hand
I don''t anticipate the need to continually resize a given array
during a given call to the routine in which it is declared.
The thing that''s more important to my application is speed.
Standard vector class templates are for *flexible* arrays of any type.
Each of the vectors in a vector of vectors could have different lengths.
They will only cause you trouble
if you really need *rigid*, *rectangular* arrays of numbers.
In which case, standard valarray class templates are a better choice.
Is there a significant different
between the element access speeds of valarrays vs. vectors?
It depends upon the implementation
but valarrays were designed for the kind of optimizations
that are required for high performance numerical applications.
For 1D arrays, the choices are:
valarray<type> vs. vector<type>

For 2D arrays, the choices are:
valarray< valarray<type> > vs. valarray< vector<type> > vs.
vector< valarray<type> > vs. vector < vector<type> >
No. Use

valarray<type>

and standard slice templates to "view" it as a matrix [tensor].
See Bjarne Stroustrup, "The C++ Programming Language: Third Edition",
Chapter 22 Numerics, Section 4 Vector Arithmetic, pages 662-79.
In my application, type is will be a scalar type such as bool, double,
int, or size_t.

The code is initially being targeted for Linux, but it may also be
ported to another UNIX such as IRIX, or perhaps even Windows, so I''d
like to keep things portable by using an STL container rather than one
from some operating system specific template library.




看看

C ++标量,向量,矩阵和张量类库( SVMTL)

http://www.netwood。 net /~edwin / svmtl /



面向对象的数字页面

htt p://www.oonumerics.org/oon/


可能最好的方法是实现矢量和矩阵类

基于标准Bjarne建议使用valarray模板。



Take a look at
The C++ Scalar, Vector, Matrix and Tensor class Library (SVMTL)

http://www.netwood.net/~edwin/svmtl/

and
The Object-Oriented Numerics Page

http://www.oonumerics.org/oon/

Probably the best approach is to implement vector and matrix classes
based upon standard valarray templates as Bjarne suggests.


Michael Aramini< M. ******* @ Verizon.net>在消息新闻中写道:< Se ******************* @ nwrdny02.gnilink.net>。 ..
Michael Aramini <M.*******@Verizon.net> wrote in message news:<Se*******************@nwrdny02.gnilink.net>. ..
我需要在C ++
程序中表示数字或bool类型的1D和2D数组。我想要的应用程序中的数组大小是动态的,因为它们在编译时是不可知的,所以我想要使用STL容器类模板,如valarray或vector
表示1D数组,valarray或矢量valarrays或
向量来表示2D数组。

正如我所说的,在我的预期应用程序中的数组大小是
动态,因为它们是在运行时确定的,但另一方面
我不希望在给定的例程调用期间继续调整给定数组的大小。对我的应用来说更重要的是速度。 valarrays与矢量的元素访问速度之间是否存在显着差异?

对于1D阵列,选择是:
valarray< type> vs. vector< type>

对于2D数组,选项包括:
valarray<的valarray<类型> > vs. valarray<矢量<类型> > vs.
vector<的valarray<类型> >与矢量<矢量<类型> >

在我的应用程序中,type将是一个标量类型,如bool,double,
int或size_t。

代码最初是针对性的对于Linux,但它也可能被移植到另一个UNIX,如IRIX,甚至可能是Windows,所以我想通过使用STL容器而不是一个容器来保持便携性。 >来自某些特定于操作系统的模板库。

-Michael
I need to represent 1D and 2D arrays of numeric or bool types in a C++
program. The sizes of the arrays in my intended application are
dynamic in the sense that they are not known at compile time, so I''d
like to use an STL container class template such as valarray or vector
to represent 1D arrays, and valarrays or vectors of valarrays or
vectors to represent 2D arrays.

As I said the sizes of the arrays in my intended application are
dynamic in that they are determined at run time, but on the other hand
I don''t anticipate the need to continually resize a given array during
a given call to the routine in which it is declared. The thing that''s
more important to my application is speed. Is there a significant
different between the element access speeds of valarrays vs. vectors?

For 1D arrays, the choices are:
valarray<type> vs. vector<type>

For 2D arrays, the choices are:
valarray< valarray<type> > vs. valarray< vector<type> > vs.
vector< valarray<type> > vs. vector < vector<type> >

In my application, type is will be a scalar type such as bool, double,
int, or size_t.

The code is initially being targeted for Linux, but it may also be
ported to another UNIX such as IRIX, or perhaps even Windows, so I''d
like to keep things portable by using an STL container rather than one
from some operating system specific template library.

-Michael




您可以将以下类用于二维数组。

模板< class T,int ROW_T = 0,int COL_T = 0>

class dynamic_2d_array

{

public:

dynamic_2d_array(int row,int col):m_row(row),m_col(col),
m_data((行!= 0&& col!= 0)?new T [row * col ]:NULL){}

dynamic_2d_array():m_row(ROW_T),m_col(COL_T),m_data(新

T [ROW_T * COL_T])

{if(!COL_T ||!ROW_T){int x [ROW_T] = {{ROW_T}}; int y [COL_T] =

{{x [0]}}; } $

~moDynamic_2d_array(){if(m_data)delete [] m_data;}

T * operator [](int i){return(m_data +(m_col *) i));}

T const * const运算符[](int i)const {return(m_data +

(m_col * i));}

私人:

const int m_row;

const int m_col;

T * m_data;

};


如果您查看以下链接,上面的类和其他方法的测试性能代码是



http://www.axter.com/faq/topic.asp?T..._ID=4&CAT_ID=9


以上链接具有用于在C样式静态2D上执行测试的代码:
数组,C-Syle动态2D数组,向量< vector< obj> > ,以及上面的

dynamic_2d_array类。


在测试中,dynamic_2d_array类在访问时执行了向量

方法数据通过operator []。

然而,当通过迭代器访问数据时,vector vector方法执行了所有其他方法。



You can use the following class for a two dimensional array.
template < class T, int ROW_T = 0, int COL_T = 0 >
class dynamic_2d_array
{
public:
dynamic_2d_array(int row, int col):m_row(row),m_col(col),
m_data((row!=0&&col!=0)?new T[row*col]:NULL){}
dynamic_2d_array():m_row(ROW_T),m_col(COL_T), m_data(new
T[ROW_T*COL_T])
{if (!COL_T || !ROW_T) {int x[ROW_T] = {{ROW_T}};int y[COL_T] =
{{x[0]}};}}
~dynamic_2d_array(){if(m_data) delete []m_data;}
T* operator[](int i) {return (m_data + (m_col*i));}
T const*const operator[](int i) const {return (m_data +
(m_col*i));}
private:
const int m_row;
const int m_col;
T* m_data;
};

If you look at the following link, there''s code for test performance
on the above class and other methods.

http://www.axter.com/faq/topic.asp?T..._ID=4&CAT_ID=9

The above link has code for performing test on C-Style Static 2D
array, C-Syle Dynamic 2D array, vector<vector<obj> > , and the above
dynamic_2d_array class.

In the test, the dynamic_2d_array class out performmed the vector
method when accessing the data via operator[].
However, the vector method out performmed all the other methods when
accessing the data via iterators.


这篇关于C ++ STL valarrays与向量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆