C++ STL容器
不同的STL容器如向量、堆栈、集合、队列等支持不同的访问方法。
例如,如果您在 Notepad++ 或 vim 中进行编码,则必须不断参考文档以了解所有可用方法,至少我必须这样做。
有没有一些好方法来记住哪个容器支持哪些方法?
Different STL containers like vector, stack, set, queue, etc support different access methods on them.
If you are coding for example in Notepad++ or vim, you have to continuously refer to the documentation to see what all methods are available, atleast I have to.
Is there some good way of remembering which container supports which methods??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
方法的名称并不是为了不同而不同。它有助于记住哪些容器有哪些方法,以及理解名称的含义。例如,
push_back
对于集合来说是无意义的。insert
在谈论堆栈时没有任何意义(当然堆栈也没有正面或背面,所以它不支持push_back
,只是 <代码>推)。对于向量来说,两者都有明确定义的含义,因此向量同时支持insert
和push_back
。The names of the methods aren't different for the sake of being different. It helps in remembering which containers have which methods, to understand the meaning of the name.
push_back
for example is nonsensical in relation to sets.insert
doesn't make any sense when talking about stacks (of course stacks don't have a front or a back either, so it doesn't supportpush_back
, justpush
). For a vector, both have a well-defined meaning, so vector supports bothinsert
andpush_back
.充分使用它们,以便记住每种方法的方法。
Use them enough so that you remember the methods of each.
如果您的记忆力一直不好,请尝试将所有这些内容的参考保留在另一个窗口中。如果您有多个显示器,那么在第二台显示器上放置类似的内容(用于任何类型的文档)确实非常方便。
另外,我强烈推荐具有智能感知功能的真正编码 IDE! Notepad++ 可能太简单,无法在 C++ 中提高工作效率。
If your memory keeps failing you, try keeping a reference of them all up in another window. If you have more than one monitor, it's really handy to have stuff like this on a second monitor (for documentation of any kind).
Alternatively I highly recommend a real coding IDE with intellisense! Notepad++ is probably too simple for being productive in C++.
使用内置智能感知的工具,例如 Windows 上的 Visual Studio 或 Linux 上的 KDevelop。
还有 vim 和 emacs 的附加组件用于智能感知。
Use something that has built in intellisense such as Visual Studio on Windows or KDevelop on Linux.
There are also add-ons for vim and emacs for intellisense.
即使你记住了所有“方法”,那也只是故事的一部分。为了有效地使用STL,您还需要了解算法。我建议阅读一本好书(Stroustrup、Josuttis,...)中有关 STL 的内容,以便记住可用的内容,然后在需要确切语法时返回书籍或打开参考站点。
Even if you remember all the "methods", that is only one part of the story. To effectively use STL, you need to know algorithms as well. I would suggest reading about STL in a good book (Stroustrup, Josuttis, ...) to just remember what is available, and then return to the books or have reference site open when you need the exact syntax.
这可能不是您正在寻找的内容,但 Scott Meyers(因“Effective C++”而闻名)根据 Nicolai Josuttis 的书“The C++ Standard Library”编制了以下 STL 算法列表:
Josuttis 的 STL 算法总结
This may not be exactly what you're looking for, but Scott Meyers (of "Effective C++" fame) has compiled the following list of STL algorithms based on Nicolai Josuttis's book "The C++ Standard Library":
Josuttis’ Summary of STL Algorithms
了解它们是什么以及常用方法,然后就很容易记住哪些方法适用。 STL 并不完全一致,但已经相当不错了。
Learn what they are, and the common methods, and then it should be fairly easy to remember which ones apply. The STL isn't perfectly consistent, but it's pretty good.
承认它不支持记忆,你可以在 vim 上运行某种智能感知。优点是您可以从自己的和外部的源代码文件创建标签。无论如何,STL 需要特殊处理,如下所述。
下载这些 vim 脚本 OmniCppComplete 和 SuperTab。
安装 OmniCppComplete:
安装 SuperTab:
通过您最喜欢的包管理器安装 ctags。下载并解压此文件并在其上运行ctags。
这将生成一个名为“tags_stl”的 ctags 文件,其中包含 STL 标签。将其复制到您喜欢的任何地方。将以下尚未存在的行添加到 ~/.vimrc 中:
这将完成 'tab'、'.'、'::' 和 '->' 上的 STL 语句即使在“使用命名空间 std;”时也是如此。如果您讨厌洋红色,请不要这样做。
Admitting that it doesn't support remembering you can get some kind of intellisense running on vim. The advantage is that you can create tags from both own and external source code files. Anyhow STL needs a special treatment which is described here.
Download these vim-scripts OmniCppComplete and SuperTab.
Install OmniCppComplete:
Install SuperTab:
Install ctags via your favourite package manager. Download and unpack this file and run ctags on it.
This will generate a ctags file named 'tags_stl' containing the STL-Tags. Copy it anywhere you like. Add the following lines which do not already exist to your ~/.vimrc:
This completes STL statements on 'tab', '.', '::' and '->' even when 'using namespace std;'. Don't do it if you hate magenta.