C++ STL列表问题C

C++ STL列表问题C,c++,list,pointers,stl,C++,List,Pointers,Stl,我有c语言中的列表指针: list<int> * pointer = (list<int> *)malloc(sizeof(list<int>)); 我得到一个错误,因为malloc不调用列表构造函数。我知道用C++做这件事: list<int> * pointer = new list<int>(); list*指针=新列表(); 但我需要这个c 有人知道这个解决方案吗?没有,因为它们是不同的语言。仅仅因为名称中的一个普通字母后

我有c语言中的列表指针:

list<int> * pointer = (list<int> *)malloc(sizeof(list<int>));
我得到一个错误,因为malloc不调用列表构造函数。我知道用C++做这件事:

list<int> * pointer = new list<int>();
list*指针=新列表();
但我需要这个c


有人知道这个解决方案吗?

没有,因为它们是不同的语言。仅仅因为名称中的一个普通字母后面只有文本字符串“++”,这并不意味着什么——这在功能上等同于尝试在Python中使用Java容器

如果你想使用STL,你必须使用C++编译器。

你也可以使用新的()的“放置”版本。在malloc()分配的内存块上调用构造函数

/*使用malloc分配内存*/
list*指针=(list*)malloc(sizeof(list));
/*使用C++新的放置版本调用C++构造函数
指针=新的(指针)列表();

这毫无意义。C甚至没有类或模板。<代码> STD::列表< /C>是C++独有的特性,而不是C。使用<代码> MalOC 这样的方法会破坏标准容器类相对于内存管理的点。如果你需要使用<代码> STD::清单,你必须使用CCome上的STL,伙计:这是一个NoOB问题,但是没有理由用下注来敲击这个家伙,是吗?”克里斯:无论如何,你需要在C++中构建你的包装器。更不用说C++没有模板,这是在代码片段中使用的。(即使假设不是STL)
list<int> * pointer = new list<int>();
/* allocate memory using malloc */
list<int> * pointer = (list<int> *)malloc(sizeof(list<int>));

/* invoke the C++ constructor using the placement version of new */
pointer = new(pointer) list<int>();