views:

361

answers:

8
+6  Q: 

C++ STL containers

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??

+3  A: 

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.

Brian R. Bondy
@Brian R. Bondy You mean there is no way to remember these easily? I really do not want to use intellisense features.
Amoeba
@cambr: As you use them more in that case you will remember them automatically.
Brian R. Bondy
@Brian: When you say "I really do not want to use intellisense features", what we all read is "I really do not want to be a more effective programmer".
Alan
@Alan I think you mean @cambr.
Brian R. Bondy
That is correct. I meant to say @cambr.
Alan
+5  A: 

Use them enough so that you remember the methods of each.

Ben S
+3  A: 

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++.

AshleysBrain
+1  A: 

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

Alexandros Gezerlis
+1  A: 

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.

David Thornley
+9  A: 

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.

Eclipse
`stack`, `queue` and `priority_queue` are not containers in the first place (they are *container adaptors* with a really tiny interface) :)
UncleBens
A: 

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.

robsn
+3  A: 

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.

Nemanja Trifunovic