Could use some additions to the article I have been working on.

closed account (3qX21hU5)
So for the past few weeks I have been think on how I could give back to this forum and others that have helped me learn C++. I finally decided to write a article. The article is aimed more towards the beginner level(Since I'm no where near the skill level needed to advice anything on advance techniques) and its goal is to show beginners how easily they can accomplish some tasks just by using the STL instead of reinventing the wheel.

So I am putting together anywhere from 10 to 15 of the most useful features of the STL or other libraries like Boost(Debating on adding these in) that a lot of beginners don't know about and breaking them down and showing how they can be used.

Some ones that I have been thinking on are stuff like random_shuffle(), find(), find_first_of(), sort(), all them goodies.

So was wondering if you guys had any input on what you think would be some of the most beneficial for beginners to learn? If something like this would be useful?

Could use all the help I can get here ;p and of course any examples or suggestions you guys give you will receive full credit for.
I think it would be beneficial to talk about C++11 lambda functions, and how they can be plugged into some of the algorithms. std::transform() and std::generate() could be used to illustrate the utility of lambdas.

You will also have to explain what iterators do. (Don't forget to mention stream iterators.)

There are many tricks that the algorithms + iterators can do.
One thing I've seen Cubbi do a number of times is something like:

1
2
3
4
5
6
7
8
9
10
11
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>

int main()
{
    std::vector<int> vi {1, 2, 3, 4, 5};

    std::copy(vi.begin(), vi.end(), std::ostream_iterator<int>(std::cout, " "));
}
1 2 3 4 5 


This is an implementation of Selection Sort from cppreference.com:
http://en.cppreference.com/w/cpp/algorithm/iter_swap

1
2
3
4
5
6
7
8
#include <algorithm>

template<class ForwardIt>
void selection_sort(ForwardIt begin, ForwardIt end)
{
    for (ForwardIt i = begin; i != end; ++i)
        std::iter_swap(i, std::min_element(i, end));
}

closed account (3qX21hU5)
Thank you Catfish!! I actually never thought of lambdas for some reason, now they are going towards the top of the list.
Don't forget my insertion sort from http://en.cppreference.com/w/cpp/algorithm/rotate

1
2
3
 for (auto i = v.begin(); i != v.end(); ++i) {
        std::rotate(std::upper_bound(v.begin(), i, *i), i, i+1);
    }
Topic archived. No new replies allowed.