添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接

closed as not a real question by Cody Gray , Book Of Zeus , Mark , Andrew Barber , Matt Apr 17 '12 at 9:47

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center . If this question can be reworded to fit the rules in the help center , please edit the question .

A vector of pointers to newly allocated objects (not really initialization though):

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

Initializing a vector of pointers to newly allocated objects (needs C++11):

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

A smarter version of #3:

std::vector<std::unique_ptr<int>> stuff;
stuff.reserve(10);
for( int i = 0; i < 10; ++i )
    stuff.emplace_back(new int(i));
                @FredOverflow:  unique_ptr's constructor is explicit, so you either need to use emplace_back or stuff.push_back(std::unique_ptr<int>(new int(i)));.  Between the two, emplace_back is much cleaner.
                    – James McNellis
                Feb 1 '12 at 7:12
                    
                        
site design / logo © 2019 Stack Exchange Inc; user contributions licensed under cc by-sa 3.0
                            with attribution required.
                    rev 1.0.0.0