Most commonly used STL algorithm, predicates, iterators

I can not find this question in stackoverflow. But I wonder how people use STL (No fancy boost) ... just OLL STL mods. Tricks / tips / mostly used cases acquired over many, many years ... and possibly received ...

Share it ...

One tip for the answer ... with sample code -

Change is such a bad question, which leads to a decrease in the number of revolutions?

+6
c ++ stl
source share
11 answers

I use STL in almost all of my projects, for things from loops (with iterators) to split input into a program.

Toxicize the input string with spaces and enter the result in std :: vector for parsing later:

std::stringstream iss(input); std::vector<std::string> * _input = new std::vector<std::string>(); std::copy(std::istream_iterator<std::string>(iss), std::istream_iterator<std::string>(), std::back_inserter<std::vector<std::string> >(*_input)); 

Other favorites outside the course are std :: reverse and various other algorithms defined in <algorithm> .

+7
source share

My favorite is the following: to change something hidden in a string:

 template <class TYPE> std::string Str( const TYPE & t ) { std::ostringstream os; os << t; return os.str(); } 

Then:

 string beast = Str( 666 ); 
+9
source share

Using vector to replace pointer + new. This is huge.

+6
source share

I like istream_iterator and ostream_iterator.

A good easy way to read a stream and create it like any other container:

 // Copies a stream of integers on the std input // into a vector. int main() { std::vector<int> data; std::copy(std::istream_iterator<int>(std::cin), std::istream_iterator<>(), std::back_inserter(data) ); // By uisng the istream_iterator<> the input just becomes another container. } 
+6
source share

I love vector. It should have been C ++ arrays. However, I work a lot in real time. People who do not need a determin may prefer a list.

Almost everyone uses the devil from the string.

I don't use the algorithm very often, since we are still using VS6 here (which cannot handle complex template settings). It will soon pass.

+4
source share

Most Useful Algorithm (IMHO) - std :: for_each

+3
source share

I can’t remember if there is a favorite or most used algorithm / predicate / iterator, the one that did the best job for what I was trying to do at that time.

+2
source share

functional bind1st : bind1st , bind2nd , mem_fun , equal_to , etc. very useful if for some reason you do not have access to Bost Bind.

This is a very subjective question and a lot depends on the style of the command line, the type of project and other unknown factors.

+2
source share

There are no commonly used STL algorithms, predicates, or iterators. He likes to ask what is the most used operator in C ++. What do you use most often, operator+ or operator- ? Do you prefer if to while ? Or maybe before throw ?

Everything is used when it should be used.

PS: I suggest you read Scott Meyers Effective STL before asking such questions.

+1
source share

Ask the artist: "What is your favorite / most used brush?" :)

+1
source share

The following is a bit of “evil,” but it saved us from many mistakes.

(Update, thanks @ Ricky65 for the comment to bring me back here.) C ++ 11 has a range for the loop that far exceeds this if your compiler supports it; we are still working with some really old compilers.

  #define FOREACH (iter, stlContainer) \
 for (typeof (stlContainer.begin ()) iter = stlContainer.begin (), \
                                    iter ## End_Cached = stlContainer.end ();  \
       iter! = iter ## End_Cached;  \
       ++ iter)

(Further update, credit to Boost developers.) It is based on the more complex but more robust BOOST_FOREACH macro, but has the advantage of making debugging assemblies for smaller cases much easier, rather than requiring a small heap of forward headers (which are in some code bases / groups verboten).

Using std::for_each usually preferable, but has some disadvantages:

  • users need to know a lot about the interactions between bind1st / bind2nd / ptr_fun / mem_fun in order to use it effectively for a non-trivial “visit” - boost eliminates many of these problems, but not everyone has or knows boost
  • users may need to provide their own separate functor (usually a structure) for only one point of use; these structures cannot be declared inside the function surrounding the loop, which leads to the “nonlocality” of the associated code - it is not readable, and also has a logical sequence with the flow of the rest of the function in some cases
  • it is not always beautifully built in, depending on the compiler

The FOREACH macro, as mentioned above, gives a few things:

  • like std::for_each , you will not be mistaken in your border tests (without repeating the end, etc.)
  • it will use const_iterators over persistent containers

Note that this requires a somewhat non-standard type extension.

Typical uses may include:

  list <shared_ptr <Thing>> m_memberList;
 // later
 FOREACH (iter, m_memberList)
 {
    if ((* iter) -> getValue () <42) {
       doSomethingWith (* iter);
    }
 }

I am not completely satisfied with this macro, but here it was invaluable, especially for programmers who did not have much experience in STL design.

(Please feel free to indicate the pros and cons, I will update the answer.)

+1
source share

All Articles