How to initialize a pointer vector

I am working on a C ++ program and I need to initialize a pointer vector. I know how to initialize a vector, but if someone can show me how to initialize it as a vector filled with pointers, that would be great!

+4
source share
1 answer

Pointer size zero vector:

std::vector<int*> empty; 

NULL Pointer Vector:

 std::vector<int*> nulled(10); 

Vector of pointers to newly selected objects (though not initialization):

 std::vector<int*> stuff; stuff.reserve(10); for( int i = 0; i < 10; ++i ) stuff.push_back(new int(i)); 

Initialization of a vector of pointers to newly selected objects (C ++ 11 is required):

 std::vector<int*> widgets{ new int(0), new int(1), new int(17) }; 

More reasonable version # 3:

 std::vector<std::unique_ptr<int>> stuff; stuff.reserve(10); for( int i = 0; i < 10; ++i ) stuff.emplace_back(new int(i)); 
+17
source

Source: https://habr.com/ru/post/1394115/


All Articles