C++ STL extras - random_sample_n

| 1 Comment

Working on some code for my research tonight, I wasted a lot of time looking for some information on a particlar STL function. Since I couldn't find the answers elsewhere, I am posting a quick explanation/solution here, to hopefully save someone else the trouble.

I am writing a C++ application, and use the STL in various places. I have been consulting the SGI Standard Template Library Programmer's Guide. It is quite poorly named; it is really a reference, with not much guidance at all. And unfortunately I have yet to find a decent book on the STL (I have one but it sucks).

(Note that the below applies to the g++ compiler version 3.3; I don't know about other compilers or libraries.)

Anyway, I found just the function I was after: random_sample_n. So I added the include for algorithm, and couldn't get the sucker to compile. It turns out that since it is an extension to the STL, it is located in the ext/algorithm header, and under the namespace __gnu_cxx:: instead of std::. This stuff I found the hard way, slugging through header files.

Another thing: I didn't find any decent examples (the one in the SGI docs is trivial) so here is some real code:

1 Comment

thank you
Hi, dear blogger:
thank you for sharing this random_sample_n function article.
I'm doing a research report on this algo and I have no idea of how to do it. There are just a few info on web about it, actually there're only two, one is from sgi, i think you knew about it, and another is yours.
Thanks
by the way, can you give me an example of an application using this function?

Leave a comment