简体   繁体   English

C ++ STL容器

[英]C++ STL containers

Different STL containers like vector , stack , set , queue , etc support different access methods on them. 不同的STL容器(例如vectorstacksetqueue等)在其上支持不同的访问方法。

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. 例如,如果您使用Notepad ++vim进行编码,则必须不断查阅文档以查看所有可用方法,至少我必须这样做。

Is there some good way of remembering which container supports which methods?? 有什么好方法可以记住哪个容器支持哪些方法?

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. 例如, push_back对集合是无意义的。 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 support push_back , just push ). 在谈论堆栈时, insert没有任何意义(当然,堆栈也没有正面或背面,因此它不支持push_back ,仅支持push )。 For a vector, both have a well-defined meaning, so vector supports both insert and push_back . 对于向量,两者都有明确的含义,因此向量同时支持insertpush_back

充分使用它们,以便记住每种方法。

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! 另外,我强烈建议您使用带有intellisense的真实编码IDE! Notepad++ is probably too simple for being productive in C++. Notepad ++可能太简单了,无法在C ++中发挥作用。

Use something that has built in intellisense such as Visual Studio on Windows or KDevelop on Linux. 使用内置的智能感知功能,例如Windows上的Visual Studio或Linux上的KDevelop。

There are also add-ons for vim and emacs for intellisense. 还有一些用于vim和emacs的附加组件,用于intellisense。

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. 为了有效地使用STL,您还需要了解算法。 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. 我建议读一本好书(Stroustrup,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": 这可能不完全是您想要的,但Scott Niyers(以“ Effective C ++”着称)基于Nicolai Josuttis的书“ The C ++ Standard Library”编译了以下STL算法列表:

Josuttis' Summary of STL Algorithms Josuttis的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. STL并不完全一致,但是非常好。

Admitting that it doesn't support remembering you can get some kind of intellisense running on vim. 承认它不支持记住您可以在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. 无论如何,STL需要特殊处理,在此进行描述。

Download these vim-scripts OmniCppComplete and SuperTab . 下载这些vim脚本OmniCppCompleteSuperTab

Install OmniCppComplete: 安装OmniCppComplete:

  • Unzip the plugin to ~/.vim. 将插件解压缩到〜/ .vim。

Install SuperTab: 安装SuperTab:

  • Open the file in vim ($ vim supertab.vba). 在vim($ vim supertab.vba)中打开文件。
  • Source the file (:so %). 源文件(:so%)。

Install ctags via your favourite package manager. 通过您喜欢的软件包管理器安装ctags Download and unpack this file and run ctags on it. 下载并解压缩该文件 ,然后在其上运行ctags。

$ ctags -R --c++-kinds=+p --fields=+iaS --extra=+q --language-force=C++ tags_stl cpp_src 

This will generate a ctags file named 'tags_stl' containing the STL-Tags. 这将生成一个包含STL标签的名为“ tags_stl”的ctags文件。 Copy it anywhere you like. 将其复制到您喜欢的任何位置。 Add the following lines which do not already exist to your ~/.vimrc: 将以下尚不存在的行添加到〜/ .vimrc中:

set tags+=~/path/to/your/tags_stl  
filetype on  
filetype plugin on  
let OmniCpp_GlobalScopeSearch=1  
let OmniCpp_NamespaceSearch=2  
let OmniCpp_MayCompleteDot=1  
let OmniCpp_MayCompleteArrow=1  
let OmniCpp_MayCompleteScope=1  
let OmniCpp_DisplayMode=1  
let OmniCpp_DefaultNamespaces=["std"]

This completes STL statements on 'tab', '.', '::' and '->' even when 'using namespace std;'. 即使在“使用命名空间std;”时,也会完成对“ tab”,“。”,“ ::”和“->”的STL语句。 Don't do it if you hate magenta. 如果您讨厌洋红色,请不要这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM