STL vector container question

Oct 12, 2017 at 10:49pm
Hello, I am trying to use STL vector container to store a set of fixed dimension arrays. Everything works fine until I try to push_back() new values.
in code terms
1
2
3
4
5
6
7
8
9
10
11
12
typedef std::complex<double> dcomp;
typedef dcomp ctensor[2][2];

// ... some code here ...

std::vector<ctensor> data;
ctensor d_l;

// ... build d_l ...

data.push_back(d_l);
 

so my question is, what is the standard way to use a vector of (complex 2x2) tensors? Is there any boost option to make this task easy?
Oct 12, 2017 at 11:46pm
C-style arrays are idiosyncratic in that they don't support value semantics.
Use instead the standard array wrapper std::array.
1
2
3
# include <array>
typedef std::array<std::array<ctensor, 2>, 2> dcomp; // or
// using dcomp = std::array<std::array<ctensor, 2>, 2>; 


std::array is like a C-style array (it is an aggregate type), except that it does not discard type information and it can be copied and assigned in the natural way. It should be preferred over an a built-in array whenever required.

(IMO, Built-in arrays are one of the worst language features in C++. Library support is required to make their widespread use viable in most modern code.)

Also, Boost supports uBLAS
http://www.boost.org/doc/libs/1_65_1/libs/numeric/ublas/doc/index.html

I have not used it personally - I have had good luck with Eigen3:
http://eigen.tuxfamily.org/index.php?title=Main_Page
Last edited on Oct 12, 2017 at 11:53pm
Oct 13, 2017 at 8:55am
I manage to get it work by using
1
2
3
typedef std::complex<double> dcomp;
//typedef dcomp ctensor[2][2];
typedef std::array<std::array<std::complex<double>, 2>, 2> ctensor;

Thanks for your help.
Topic archived. No new replies allowed.