Posts

Showing posts with the label STL

Finding permutations in strings

Image
In C++ the Standard Template Library provides us with std::next_permutation to easily implement this. [code language="cpp"] #include <iostream> #include <algorithm> #include <iterator> #include <string.h> int main() { char str[] = "abcd"; const size_t len = strlen( str ); do { std::copy( str, str + len, std::ostream_iterator<char>( std::cout ) ); std::cout << std::endl; } while ( std::next_permutation( str, str + len ) ); return 0; } [/code]

Counting the Number of Words in a Text File in STL / C++

Image
This post aims to illustrate the power of using STL's associative arrays as a word counter. It reads the entire contents of the text file, word-by-word, and keeps a running total of the number of occurences of each word. All using just a few lines of code, discounting the bits that output the results.

Priority Queues and Min Priority Queues in STL / C++

Image
Programming Tip: Now you can load your essential programming tools such as emulators and IDE`s into the cloud with high performance citrix vdi from CloudDesktopOnline and access it remotely at your convenience on your preferred device(PC/Mac/android/iOS). If you prefer a gpu dedicated server, Try dedicated gpu hosting from Apps4Rent with 24*7*365 days top-notch tech-support and migration assistance. Priority Queues A priority queue is just like a normal queue data structure except that each element inserted is associated with a ”priority”. It supports the usual push() , pop() , top() etc operations, but is specifically designed so that its first element is always the greatest of the elements it contains, according to some strict weak ordering condition. STL Usage In STL, priority queues take three template parameters: [code language="cpp"] template < class T, class Container = vector<T>, class Compare = less<typename...

How to Permanently Remove Items in STL Containers

Image
remove - What is does and does not do Like all STL algorithms, remove receives a pair of iterators to identify the range of container elements over which it needs to operate, as shown in its declaration: [code language="cpp"] template< class ForwardIterator, class T > ForwardIterator remove( ForwardIterator first, ForwardIterator last, const T& value ); [/code]

A First Stab at boost::bind

Boost::bind is “able to bind any argument to a specific value or route input arguments into arbitrary positions.” It's a means of converting a function into an object that can be copied around and called at a later point, deferred callbacks for example.

Avoiding Memory Leaks using Boost Libraries

Using boost::scoped_array When we want to dynamically allocate an array of objects for some purpose, the C++ programming language offers us the new and delete operators that are intended to replace the traditional malloc() and free() subroutines that are part of the standard library :

Using Template Classes to Handle Generic Data Types in STL Containers

Image
A code snippet with which utilizes both template classes and STL to handle generic data types. In keeping with the spirit of this blog, I have kept the explanation to a minimum and hopefully the code posted below should be self-explanatory.  The idea is for readers to get the gist of what I am saying so that they can go off and make up more relevant examples of their own.

Sorting Objects Using STL

Image
Here's an example of how using objects (hat-tip: Paul Wolfensberger )

Using Smart Pointers to Avoid Memory Leaks

Using boost::scoped_array When we want to dynamically allocate an array of objects for some purpose, the C++ programming language offers us the new and delete operators that are intended to replace the traditional malloc() and free() subroutines that are part of the standard library :

User Defined Predicates

Just some simple examples posted here as a means of easy lookup...