pointer to vector problem

Mar 19, 2014 at 4:49am

ive created a vector of vector and initialized like this
vector<vector<double>*>* vec1= new vector<vector<double>*>;
for(size_t t =0 ;t<500 ;t++ )
{
vector<double>* v = new vector<double>;
vec1->pushback(v);
}

now can i assign like this ??
vector<vector<double>*>* vec2 = vec2 ;
in my program it worked fine and when i rebuild im getting error ->

Error 33 error C2440: 'initializing' : cannot convert from 'std::vector<_Ty>' to 'std::vector<_Ty> '
Mar 19, 2014 at 4:53am
May I ask why you are using pointers for this?
Mar 19, 2014 at 4:53am
Do you really need a vector pointer of vector pointers? What are you trying to achieve?
Mar 19, 2014 at 4:56am
yes i need pointers because values or not fixed it is more than 500
10000+ values need to be inserted
Mar 19, 2014 at 5:11am
yes i need pointers because values or not fixed it is more than 500
Vector is dynamically sized so large and unknown amounts is not a problem.
10000+ values need to be inserted
Vector stores it values in heap, so it will be no problem with that too.
Read about reserve() method. It will help with perfomance.
Mar 19, 2014 at 5:52am
thanks

now ive used pointer everywhere in my project and i dont want to modify that one but wanted to assign it some other
i want to assign this -> vector<vector<double>*>* vec1 to vector<vector<double>> is it possible ???
Mar 19, 2014 at 5:52am
use the copy constructor.

std::vector<std::vector<double>* >* vec2(vec1);
Mar 19, 2014 at 6:19am
i wanted to copy pointer values vector<vector<double>*>* vec1 to
vector<vector<double>> vec2
Mar 19, 2014 at 6:25am
Oh I didn't even notice because your original code had pointers. Anyways why would you want to do that? Doesn't really make sense to me. The only way I can think of though is to iterate with something like:

1
2
3
4
5
6
7
8
for(const auto &it:vec1)
{
    vec2.push_back(std::vector<double>());
    for(const auto &it2:it)
    {
        vec2.back().push_back(it2);
    }
}



There is probably other ways but I don't think any of them will be to pretty.
Mar 19, 2014 at 6:41am
okk
but in my project all vector pointers are used i dont know y
cant we use simple vectors for that
can u tell me diff between vector<vector<double>*>* and vector<vector<double>> in terms of memory and etc
Mar 19, 2014 at 6:54am
One of them is a pointer to a container of containers full of pointers and the other is a container full of containers of objects. I would really only use pointers if the data types are large and not built-in types like doubles/ints/chars ect..

I think you should read up on pointers and STL containers before using them.

http://www.cplusplus.com/doc/tutorial/pointers/
http://www.cplusplus.com/reference/stl/
Mar 19, 2014 at 7:40am
ok got it
i want to copy vector<vector<double>*>* vec1 from vector<vector<double>*>* vec2 and pass vector<vector<double>*>* vec1 to a function

and i need vec1 and vec2 to be present
ex : func(vector<vector<double>*>* , vector<string>);
Topic archived. No new replies allowed.