-3

I need to dynamically allocate an array of 5 vectors of pairs. This code snippet is supposed to add first elements to all 5 vectors:

std::vector<std::pair<int, int>> * arr = new std::vector<std::pair<int, int>>[5];
for (int i = 0; i < 5; i++) {
    arr[i].push_back(std::make_pair(i+1, i+11));
}

But it adds only 1 element to arr[0] vector

for (auto el : *arr) {
    std::cout << el.first << ", " << el.second << std::endl;
}

Printing out gives 1, 11
What I need is

1, 11
2, 12
3, 13
4, 14
5, 15

Please give me some hints. How to work with dynamic vector of pairs?

EDIT: Vector of vectors is one possible way. However, I want to use an array of vectors.

user2376997
  • 501
  • 6
  • 22
  • You allocate an array of 5 vectors, not a single vector. Then you add a single element to each of those 5 vectors. Is this what you wanted? Your prose doesn't appear to match your code. You only call `push_back` on `arr[0]` once, so it's unclear why you expect `arr[0].size()` to be anything but 1 – Igor Tandetnik Jan 05 '19 at 14:13
  • 1
    There's *rarely* any need to have pointers to containers. Not even for creation of them. – Some programmer dude Jan 05 '19 at 14:15
  • the size of an array is only known at runtime – user2376997 Jan 05 '19 at 14:18
  • Your second loop iterates over `arr[0]`, which, again, only contains one element. Did you mean to iterate over all vectors in `arr`? That would be `for (auto v : arr) { std::cout << (*v)[0].first ...; }` – Igor Tandetnik Jan 05 '19 at 14:19
  • Don't you mean that the size of an array must be fixed at *compile-time*? If the size is only known at run-time then use a *vector* (which of course can contain other vectors). – Some programmer dude Jan 05 '19 at 14:20
  • `for (auto v : arr)` doesn't work – user2376997 Jan 05 '19 at 14:21
  • @IgorTandetnik Won't work very well with pointers. – Some programmer dude Jan 05 '19 at 14:23
  • 1
    You should not be using `new[]`, you should instead use a `std::vector`. – Eljay Jan 05 '19 at 14:26
  • 1
    Side note - consider using `arr[i].emplace_back(i+1, i+11)` instead of `arr[i].push_back(std::make_pair(i+1, i+11))`. Not only it is slightly more readable, it should also be more efficient. Regarding the edit - **why** do you want to use dynamically allocated array of vectors, instead of vector of vectors? – Fureeish Jan 05 '19 at 14:28
  • Because array of vectors is also used for graph representation – user2376997 Jan 05 '19 at 14:35

1 Answers1

4

Note: Edited entire answer because of the edit of the question.


The statement:

for (auto el : *arr) {
    std::cout << el.first << ", " << el.second << std::endl;
}

will print the element(s) for the first vector only (i.e. arr[0]). That's because arr will decay as a pointer to the first element of the array.


If you want to print for all vectors, you need to iterate over the size of the array (as already done for the insertion):

for (int i = 0; i < 5; i++) {
    // arr[i] now is the i-th vector, and you can print whatever you want

    // For example the following will print all element for each vector.
    for (auto el : arr[i]) {
      std::cout << el.first << ", " << el.second << std::endl;
    }
}
BiagioF
  • 9,368
  • 2
  • 26
  • 50
  • No, my intention is 5 different containers. So I am missing a point with printing it out. As I said `for (auto el : arr)` doesn't work – user2376997 Jan 05 '19 at 14:25
  • 2
    @user2376997 then use *vector of vectors* instead of *dynamically allocated array of vectors*. – Fureeish Jan 05 '19 at 14:27
  • 1
    @user2376997 Uhm, I saw only now you've edited the question. I suggest you be more precise in your question. Especially in the usage of *singular/plural*. – BiagioF Jan 05 '19 at 14:27