显然;-)标准容器提供某种形式的保证.
什么类型的保证以及不同类型的容器之间究竟有什么区别?
Container Types:
================
Container:
Forward Container
Reverse Container
Random Access Container
Sequence
Front Insert Sequence
Back Insert Sequence
Associative Container
Simple Associative Container
Pair Associative Container
Sorted Associative Container
Multiple Associative Container
Container Types mapped to Standard Containers
=============================================
std::vector: Sequence Back Sequence Forward/Reverse/Random Container
std::deque: Sequence Front/Back Sequence Forward/Reverse/Random Container
std::list: Sequence Front/Back Sequence Forward/Reverse Container
std::set: Sorted/Simple/Unique Associative Container Forward Container
std::map: Sorted/Pair/Unique Associative Container Forward Container
std::multiset: Sorted/Simple/Multiple Associative Container …Run Code Online (Sandbox Code Playgroud) 当您想获得有关特定代码路径的性能数据时,您使用什么方法?
我刚观看了Bjarne Stroustrup对GoingNative'12演讲的录音.我有点困惑.
在本次演讲中,他特别讨论了vectorvs list问题,并建议在很多情况下vector即使你从中间插入和删除也要更快,因为编译器可以优化很多东西并且喜欢紧凑的结构.结论(据我所知)是:首先使用,vector然后再考虑是否需要其他东西.这听起来很合理,但考虑到第一次观察,我应该考虑哪些标准?我一直认为,如果你强烈插入/删除 - 使用列表.这里的一些主题也提出了类似的建议.看到
std :: vector与std :: list与std :: slist的相对表现?
和
现在根据Stroustrup我错了.
当然,我可以编写几个测试并试图弄清楚在每种特定情况下使用什么,但是有理论上的方法吗?
是否有任何C++库实现Haskell Data.Sequence容器之类的东西?
我最感兴趣的是:
O(logn)通过索引访问.阿卡operator[](size_type pos).O(logn) 在中间插入/删除(通过索引).是否可以在不到1秒(1.000000)的时间内加载3或4百万行的文件?一行包含一个单词.单词的长度范围是1到17(这有关系吗?).
我的代码现在是:
List<string> LoadDictionary(string filename)
{
List<string> wordsDictionary = new List<string>();
Encoding enc = Encoding.GetEncoding(1250);//I need ? ? ? ? etc.
using (StreamReader r = new StreamReader(filename, enc))
{
string line = "";
while ((line = r.ReadLine()) != null)
{
if (line.Length > 2)
{
wordsDictionary.Add(line);
}
}
}
return wordsDictionary;
}
Run Code Online (Sandbox Code Playgroud)
定时执行的结果:

如何强制该方法使其在一半的时间内执行?
正如标题所暗示的那样,我遇到了一个我的程序问题,我使用std :: list作为堆栈,并迭代列表中的所有元素.当名单变得非常大时,该计划花了太长时间.
有没有人对此有一个很好的解释?是一些堆栈/缓存行为?
(解决了问题,将列表更改为std :: vector和std :: deque(顺便说一下,这是一个惊人的数据结构),所有内容突然变得更快)
编辑:我不是一个傻瓜,我不访问列表中间的元素.我对列表做的唯一事情就是在结尾处开始删除/添加元素并迭代列表中的所有元素.而且我总是使用迭代器迭代列表.