-1

This doesn't compile:

vector<int[2]> v;
int p[2] = {1, 2};
v.push_back(p); //< compile error here

https://godbolt.org/z/Kabq8Y

What's the alternative? I don't want to use std::array.

The std::vector declaration on itself compiles. It's the push_back that doesn't compile.

anastaciu
  • 23,467
  • 7
  • 28
  • 53
tuket
  • 3,232
  • 1
  • 26
  • 41
  • 4
    Arrays are insanely stupid artefacts of an older era. They cannot be copied, assigned, or moved without help, and `vector` does a lot of all of those operations. Consider using `std::array` instead of a plain old array. `std::array` is a lot smarter. – user4581301 Oct 19 '20 at 21:40
  • 1
    Ohhhkay. If no `std:: array`, you can use `std::pair`, but that's clunky as hell. You can also make your own structure around the array that you can copy, assign, and move, but at that point you've just remade `std::array`. Probably easier to have a simple struct with two `int` members (which is basically `std::pair`, but you can give the members descriptive names).. – user4581301 Oct 19 '20 at 21:42
  • 2
    What's the aversion to `std::array`? Seems the most direct solution to the problem. It could be useful for potential answerers to know why the obvious choice has been rejected. – user4581301 Oct 19 '20 at 21:46
  • 1
    I don't want to use `std::array`. Why? This is a silly requirement by itself (if it's not provided by your assignment, which _don't want to_ does not indicate). – Daniel Langr Oct 19 '20 at 21:50
  • Here's a somewhat silly option: Don't store an array at all. Just store twice as many `int`s and double the index when accessing to get the location of the first element of the "array".. – user4581301 Oct 19 '20 at 22:00
  • @user4581301 I'm not a fan of `std::array`, that's all. I feel like it doesn't integrate nicely with the language, for exaple, when it doesn't infer the size from the initialization list. I agree that plain arrays have plenty of downsides as well though. – tuket Oct 20 '20 at 08:17
  • @DanielLangr Also, I already knew the answer with `std::array`, I wanted to avoid people telling me about that one. I wanted to learn something new. – tuket Oct 20 '20 at 08:27
  • @tuket It does infer size since C++17 thanks to deduction guides: `std::array a { 1, 2, 3 };`. – Daniel Langr Oct 20 '20 at 08:43
  • @DanielLangr I knew that but you can't specify the type `std::array {1, 2, 3}` :( – tuket Oct 20 '20 at 20:17

4 Answers4

6

You could use a structure containing an array, or a structure containing two integers, if you really don't want to use std::array:

struct coords {
    int x, y;
};

vector<coords> v;
coords c = {1, 2};
v.push_back(c);

alternatively, as mentioned, you could use a structure containing an array:

struct coords {
    int x[2];
};

vector<coords> v;
coords c = {1, 2};
v.push_back(c);
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
2

Use std::array:

vector<std::array<int, 2>> v;
std::array<int, 2> p = {1, 2};
v.push_back(p);
anastaciu
  • 23,467
  • 7
  • 28
  • 53
Robert Andrzejuk
  • 5,076
  • 2
  • 22
  • 31
  • That would be the obvious alternative but the OP explicitly states that `std::array` is not to be used – anastaciu Oct 19 '20 at 21:52
  • 1
    Unfortunately it's either a `std::array` or any other type which has the same properties. Just not a plain old "array". – Robert Andrzejuk Oct 19 '20 at 22:06
  • I'll remove my DV but note that are deleted answers equal to yours, you just can't see them. – anastaciu Oct 19 '20 at 22:07
  • Sorry I'm downvoting but I explicitly specified that I don't want to use `std::array` (it was like that since the beginning, not that I edited the question afterwards). This would be a good answer otherwise. – tuket Oct 20 '20 at 07:53
  • @tuket I understand. Just sorry to say, there is no way to use an array "directly". Indirectly is another story. – Robert Andrzejuk Oct 20 '20 at 14:41
1

As an alternative, as you explicitly state that std::array is not to be used, you could use pointers, it's kind of an oddball solution but it would work:

#include <iostream>
#include <vector>

int main()
{
    const int SIZE = 2;
    std::vector<int*> v;
    static int p[SIZE] = {1, 2}; //extended lifetime, static storage duration
    int *ptr[SIZE]; //array of pointers, one for each member of the array

    for(int i = 0; i < SIZE; i++){
        ptr[i] = &p[i];  //assign pointers
    }
 
    v.push_back(*ptr); //insert pointer to the beginning of ptr

    for(auto& n : v){
        for(int i = 0; i < SIZE; i++){ 
            std::cout << n[i] << " "; //output: 1 2
        }
    }
}
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • Not incorrect at all, and if the asker's question leaves them with only the possibility of oddball solutions, [they've really got only one choice](https://www.youtube.com/watch?v=SMhwddNQSWQ). – user4581301 Oct 19 '20 at 22:39
1

I think you should check the C++ reference for vectors.

http://www.cplusplus.com/reference/vector/vector/vector/

You can see in the example, there is explained every way you can initialize a vector.

I think that for your case you need to do:

 std::vector<int> v({ 1, 2 });
 v.push_back(3);
  • This would be inefficient, since vector stores elements on the heap. Much better to use `std::array`, but OP for some reason wants to avoid it (which, I am afraid, kind of implies that they want to avoid vectors as well). – Daniel Langr Oct 19 '20 at 21:59
  • yes, for sure, what you point is right, but I was answering to the question of how to make a pushback on a vector and the error on the above code. He didn't asked the most performant data structure to use and he neither told us about the usage of the code. I think it's just to learn. – Maxi Malvido Oct 19 '20 at 22:54
  • @DanielLangr No, I don't want to avoid vectors. So I think this solution is just as performant as using a `std::vector>` – tuket Oct 20 '20 at 07:48
  • @tuket You're right, I overlooked myself, my bad. I thought that there was a _vector-of-vectors_ instead. – Daniel Langr Oct 20 '20 at 08:45