0

According to that question, I'm more and more worried about the management of the memory in my project, and my program would crash when there is somewhere a conflict in one of the array memories.

The different answers to this posted question have converged to the fact that I would change my arrays to Vectors and some of my arrays are defined as below:

#define Nb 50
int position[Nb];
double indication[Nb];
double sum[Nb];

how to change each one of them to

 std::vector
 std::list?

Can we manipulate / process them when changing their declaration (organizing them in ascending order for example or overwriting one of the values) ?

Community
  • 1
  • 1
MelMed
  • 255
  • 10
  • 19
  • 2
    `T x[N];` becomes `std::vector x(N);`. Don't use lists. – Kerrek SB Jun 19 '13 at 08:50
  • See also C++14 [std::dynarray](http://en.cppreference.com/w/cpp/container/dynarray), if you want a dynamic container whose size is fixed at construction. But the analogue to your example would be `std::array`. – juanchopanza Jun 19 '13 at 08:53

2 Answers2

2

You are using fixed size arrays,

#define Nb 50
int position[Nb];

the modern C++ alternative to these is std::array, not std::vector:

std::array<int, 50> position;

std::vector is a dynamically sized array, whose size can be determined at runtime and can change after construction.

int n;
std::cin >> n;
std::vector<int> v(n); // vector has n value constructed ints
v.push_back(42); // v has n+1 ints now.

C++14 offers std::dynarray, which is a dynamically sized array, whose size can be determined at runtime but cannot be changed after construction.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thank you. And is the manipulation / assignment of values when using that declaration is the same, I mean: I shall write position[0]=0 and position[2]=4 for example? – MelMed Jun 19 '13 at 09:14
  • @MelMed yes, it is exactly the same, although the containers also give you iterators, which in this case look quite similar to pointers to elements of a C-style array. – juanchopanza Jun 19 '13 at 09:16
  • But it seems for example that the memcopy and the memset don't work on that type. An idea how to do so? – MelMed Jun 19 '13 at 09:22
  • @MelMed you do not use `memcopy` or `memset`. You don't really need them. If you give examples of what you are trying to do with C arrays, then it would be easier to show the C++ equivalents. – juanchopanza Jun 19 '13 at 09:24
  • Yes sure. I was using memset(position, 0, sizeof(position)); and memcpy(sum_copy, sum, sizeof(sum_copy)); – MelMed Jun 19 '13 at 09:25
  • @MelMed `vector` and `dynarray` will zero-initialize the elements. For `array`, you need to instantiate it like this: `std::array position{};`. As for `memcpy`, all you need is `sum_copy = sum;` – juanchopanza Jun 19 '13 at 09:30
1

The Syntax is as follows:

#define Nb 50
std::vector<int> position(Nb); // create std::vector holding ints, Initial size is 50

You should also refrain from using #define, but rather use const, this is more C++:

const int Nb = 50; //maybe wrap Nb in a namespace
 std::vector<int> position(Nb);

See vector and its constructors.

bash.d
  • 13,029
  • 3
  • 29
  • 42