我有这样一个列表: C ++中的列表
list<int> p[15];
list<int> copy_of_p[15];
C#中的列表
list<int>[15] p;
list<int>[15] copy_of_p;
我尝试使用此代码
在C#中制作10000份副本for (int counter = 0; counter < 15; counter++)
{
copy_of_p[counter] = p[counter].toList();
}
花了大约10个MiliSecs
然后我在c ++中用这段代码做了同样的事情
for (int counter = 0; counter < 15; counter++)
{
copy_of_p[counter] = p[counter];
}
花了大约1200 MiliSecs
这意味着在c ++中应该有一种方法来复制列表至少与C#一样快。你能指导我扔这个吗?
P.S:我试过
copy(p.begin(), p.end(), copy_of_p[counter]);
但是它造成了构建错误
答案 0 :(得分:7)
请记住,stl-list&lt;&gt;与C#List&lt;&gt;不同。 stl-list是双向链表,而C#List&lt;&gt;将数据存储在一个有名的内存块中。因此stl-list&lt;&gt;复制操作要快得多。
C#List&lt;&gt;相当于stl-vector&lt;&gt;
stl-list相当于C#LinkedList&lt;&gt;
答案 1 :(得分:1)
C#List实际上不是链表。 std::list
是一个链接列表。因此,您需要比较两种截然不同的操作。其次,C ++代码将调用析构函数并为用户再次提供内存,而玩具C#程序将不执行任何内存管理或释放任何资源。例如,如果你有list<file>
,那么C ++程序将关闭所有文件而C#程序则不会。