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) ?

2
  • 2
    T x[N]; becomes std::vector<T> x(N);. Don't use lists.
    – Kerrek SB
    Commented Jun 19, 2013 at 8:50
  • See also C++14 std::dynarray, if you want a dynamic container whose size is fixed at construction. But the analogue to your example would be std::array<int, 50>. Commented Jun 19, 2013 at 8:53

2 Answers 2

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.

6
  • 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
    Commented Jun 19, 2013 at 9: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. Commented Jun 19, 2013 at 9: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
    Commented Jun 19, 2013 at 9: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. Commented Jun 19, 2013 at 9:24
  • Yes sure. I was using memset(position, 0, sizeof(position)); and memcpy(sum_copy, sum, sizeof(sum_copy));
    – MelMed
    Commented Jun 19, 2013 at 9:25
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.