How does std :: vector select objects?

How std::vectorto select objects? It would seem that he simply uses std::allocator::allocateto create a block of memory, but never calls std::allocate::construct. It's true? Does std::vectormemory share and never create objects as a memory allocation?

What if there is no default constructor? How is a constructor called when there is no default constructor on an object? What if there is more than one parameter?

For example, there is no default constructor with this code, and std :: allocator resolves it.

#include <vector>
using namespace std;

class A{
protected:
    int m;
public:
    explicit A(int a) : m(a) { }
};

int main(){
    vector<A> test;
    return 0;
}
+3
source share
2 answers

This has changed since C ++ 11.

In C ++ 03, it constructcan only do copy-construction in place.

, std::vector , . , .

"" " ". allocator , . std::vector , . , ; , allocator::construct.

, ++ 03, std::vector, . push_back, insert, . , , - . , allocator construct, .

++ 11 allocator_traits<>::construct . varadic, . ( ) allocator::construct, . , new.

emplace.

, , , . construct .

+8

, std::allocator::allocate , ( ++ 11) .

, , .

0

All Articles