pointer arrays and std::array

Are the two arrays, one from c++11 and the other prior, basically the same from the vantage input/output? I was struggling with understanding array<>my_array etc. when it dawned on me that are both instantiating a struct class. What other data structures can the two methods accommodate? Is the std::array been the new standard?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include<iostream>
#include <array>

using namespace std;

struct A
			{

		    int rank;
		    int suit;

			};

struct B
			{

		    int rank;
		    string suit;

			};


int main()
{             //c++11
	       array<B, 51>b; // c++11
		b[0].suit = "String member test";
		b[0].rank = 13;
		cout<<b[0].suit<<"   "<<b[0].rank<<endl;

                //older
		A * n_ptr = new A[51];
		n_ptr->rank = 100;
		cout<<n_ptr->rank<<endl;

	return 0;
	}
Hi,

I think this says it all:

http://en.cppreference.com/w/cpp/container/array


std::array allows one to avoid ordinary arrays and provides advantages so that it works with the STL.

Even better it allows one to avoid using new and delete

A fixed length array is handy sometimes, otherwise one would just use a std::vector for a lot of things, unless there is an advantage in using some other STL container.

What other data structures can the two methods accommodate?


You know that STL containers can be nested, right?

1
2
3
4
5
6
7
8
9
struct Cell {
    // Cell info
.
.
.

};

std::vector<std::array<Cell,10>> CellData;


Apparently the nesting can be arbitrarily deep, but things could become harder to understand and deal with if one has too much of a wacky data structure.

Is the std::array been the new standard?


I would prefer std::array over an ordinary array, and almost anything else over using new and delete. Try to use the STL as much as possible, use smart pointers if you really need to.
Last edited on
Thanks IDM for pointing me in the right direction...

Good reading:

http://en.cppreference.com/w/cpp/container/array



I can pass strings into the array using struct/string member. I can use std::string as a data structure (container). I'll have to read up on the potential benefits of using the array in such a way. My guess for now is I can use the member functions of std::string in conjunction with std::array.

Thx
Last edited on
array<B, 51>b;
Why are you creating an array with 51 elements instead of 52? Normally a deck of cards have 52 or 54 cards. Sometimes the deck contains fewer cards, for example euchre uses a deck of 24, however rarely does a deck contain 51 cards.

I would prefer std::array over an ordinary array,

I would also recommend std::array over an ordinary array, however I would normally recommend std::vector over a std::array as well.



Well, I got mixed up with index[0]. You correctly pointed out that element count is not indexing. Thx

array<B, 52>b

I am at a point where any information to go toward std::array versus vector would be helpful. ( why would u choose vector?


Last edited on
why would u choose vector?

One reason is because it is easier to create "generic" functions that can handle a vector of any size than it is to create "generic" functions to std::arrays. Another reason is that std::vector is available with any C++ standard, arrays were added after the first standard.

IMO std::vector should be the container of first choice. The only time I recommend another container is when a std::vector has been found not to be adequate or another container has required features that are not easily available with a vector.
Topic archived. No new replies allowed.