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??
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 support push_back
, just push
). For a vector, both have a well-defined meaning, so vector supports both insert
and push_back
.
Use them enough so that you remember the methods of each.
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++.
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.
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.
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
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.
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:
- Unzip the plugin to ~/.vim.
Install SuperTab:
- Open the file in vim ($ vim supertab.vba).
- Source the file (:so %).
Install ctags via your favourite package manager. Download and unpack this file and run ctags on it.
$ 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. Copy it anywhere you like. Add the following lines which do not already exist to your ~/.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;'. Don't do it if you hate magenta.
精彩评论