pushing back a vector of pointers

Dec 18, 2015 at 5:20pm
whats wrong with the last line?
no matching function for call to 'std::vector<std::vector<int*>*>::push_back(std::vector<int*>&)'
test2.push_back(test);
1
2
3
4
5
6
7
8
9
    int *ptr;
    int *ptr2;
    *ptr = 1;
    *ptr = 2;
    std::vector<int*> test;
    std::vector<std::vector<int*>*> test2;
    test.push_back(ptr);
    test.push_back(ptr2);
    test2.push_back(test);
Last edited on Dec 18, 2015 at 5:20pm
Dec 18, 2015 at 5:45pm
closed account (E0p9LyTq)
Are you wanting to push a vector of pointers into a vector, or a pointer to a vector of pointers?

std::vector<std::vector<int*>*> test2; creates a vector that stores pointers to vectors of pointers.

I suspect you want std::vector<std::vector<int*>> test2; instead.

If you do want to store a pointer to a vector of pointers then your push_back should be: test2.push_back(&test);
Dec 18, 2015 at 6:06pm
@FurryGuy

assume that int is a class that is non copyable
std::vector<int*> test;
i made it pointer because int is non copyable

so would you still do this? std::vector<std::vector<int*>> test2;

im not sure if im right about line 6
Last edited on Dec 18, 2015 at 6:10pm
Dec 18, 2015 at 6:07pm
Lines 3-4 are going to trap. ptr and ptr2 are uninitialized pointers. They point to random memory.
Dec 18, 2015 at 6:17pm
Hi,

I wonder why you have a vector of pointer to vector at all? Is it because you think pointers to vector are more efficient? If so, I doubt that is necessary. Equally unsure as to why you want a vector of pointer to int? This usually not worth it: the pointer is the same size as the int on a 32 bit machine, and twice as big on a 64 bit machine - 64 bit is common these days. In short, don't bother with pointers to built in types, and use references where you can otherwise, unless doing polymorphism.

C++11 has perfect forwarding, copy elision and move semantics, so one can pass things by value more often than before.

Also, there could be alternatives to vector of vector, but maybe the composite design pattern is overkill?

Edit:

I didn't see the last 2 posts, while doing mine :+)
Last edited on Dec 18, 2015 at 6:19pm
Dec 18, 2015 at 6:34pm
Lines 3-4 are going to trap. ptr and ptr2 are uninitialized pointers. They point to random memory

its just all a test but thanks by the way
Dec 18, 2015 at 6:36pm
the original topic is solved.

added to it is i should go with std::vector<std::vector<int*>*> test2; or std::vector<std::vector<int*>> test2;?
(assume that int is a noncopyable class)
Dec 18, 2015 at 6:51pm
Use std::vector<std::vector<T>> if T is a movable type.

Otherwise you could use std::vector<std::vector<T*>> .

Another option if the objects are dynamically allocated with new is to use std::vector<std::vector<std::unique_ptr<T>>> so that you don't have to worry about using delete.
Last edited on Dec 18, 2015 at 6:54pm
Dec 18, 2015 at 7:06pm
closed account (E0p9LyTq)
xenoviaquarta wrote:
assume that int is a class that is non copyable

Then my answer would have been different. I was addressing what you had asked, not dealing with unvoiced conjectures.
Dec 19, 2015 at 8:41am
so i have this

1
2
std::vector<MyMusicPlayer*> songs;
std::vector<std::vector<MyMusicPlayer*>*> playLists;


after i pushback the songs to playLists, im going to put another pack of music to vector of songs

how do i move the songs to playLists?


EDIT:
ive searched about it and saw this one http://stackoverflow.com/questions/7601906/moving-a-object-pointer-in-a-vector-to-a-different-vector

so emptying the songs vector will do it


base on my testing
std::vector<std::vector<int*>*> t2; the outer pointer makes the vector itself a pointer not the type










this first snippet outputs the expected output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int a,b;
int *ptr1 = &a, *ptr2 = &b;
*ptr1 = 1;
*ptr2 = 2;

std::vector<int*> t;
std::vector<std::vector<int*>> t2;

t.push_back(ptr1);
t.push_back(ptr2);

t2.push_back(t);
t.clear();

std::cout << *(t2[0][0]);


this 2nd snippet does not. because when the t.clear() is executed, the t vector is also cleared in t2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int a,b;
int *ptr1 = &a, *ptr2 = &b;
*ptr1 = 1;
*ptr2 = 2;

std::vector<int*> t;
std::vector<std::vector<int*>*> t2;

t.push_back(ptr1);
t.push_back(ptr2);

t2.push_back(&t);
t.clear();

std::cout << *((*t2[0])[0]);


the same will happen if i modify the ptr1 and ptr2

so i should go to
std::vector<std::vector<int*>> t2;

thanks guys (y)
Last edited on Dec 19, 2015 at 9:33am
Topic archived. No new replies allowed.