STL列表问题C
我在 c: 中有列表指针,
list<int> * pointer = (list<int> *)malloc(sizeof(list<int>));
当我尝试:时
pointer->push_back(1);
出现错误,因为 malloc 不调用列表构造函数。我知道在 C++ 中这样做:
list<int> * pointer = new list<int>();
但我需要在 C 中这样做?
有人知道这个问题的解决方案吗?
I have list pointer in c:
list<int> * pointer = (list<int> *)malloc(sizeof(list<int>));
when I try:
pointer->push_back(1);
I get error, because malloc doesn't call list constructor. I know to do this in c++ with:
list<int> * pointer = new list<int>();
but i need this in c?
Does anybody know solution for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,因为这些是不同的语言。仅仅因为名称中的常见字母后面有文本字符串“++”并没有任何意义 - 这与尝试在 Python 中使用 Java 容器的功能等效。
如果你想使用STL,你就必须使用C++编译器。
No, because these are different languages. Just because one only has the text string "++" after a common letter in the name doesn't mean anything - this is the functional equivalent of trying to use a Java container in Python.
If you want to use STL, you have to use a C++ compiler.
您还可以使用 new() 的“放置”版本。在由 malloc() 分配的内存块上调用构造函数。
You can also use the "placement" version of new(). to invoke the constructor on a chunk of memory allocated by malloc().