Tag: STL

  • Finding permutations in strings

    In C++ the Standard Template Library provides us with std::next_permutation to easily implement this.

    #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;
    }
    

    (more…)

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

    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.
    (more…)

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

    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:

    template < class T, 
               class Container = vector<T>,
               class Compare = less<typename Container::value_type> 
             > 
    class priority_queue;
    

    T is the element type, eg an integer;
    Container is the container type, eg a vector.
    Compare is the comparison class which can be a function call operator or function pointer used to compare container elements. Its default is the less-than operator, less<T>.

    Example 1: Max Priority Queues

    As can be seen in the following example, using a priority queue in its default format gives you a max priority queue:

    #include <iostream>
    #include <queue>
    using namespace std;
    
    int main()
    {
        priority_queue<int> pq;
    
        pq.push(3);
        pq.push(5);
        pq.push(1);
        pq.push(8);
        while ( !pq.empty() )
        {
            cout << pq.top() << endl;
            pq.pop();
        }
        cin.get();
    }
    

    Giving you the following output:

    Example 2: Min Priority Queues

    Supposing you wish to get a min priority queue out of it. A posting of this subject appears over at Cprogramming.com, showing how to implement this, which I have borrowed here. A possible solution could be to define a suitable comparator with which to operator on the ordinary priority queue, such that the priority is reversed, as shown:

    #include <iostream>
    #include <queue>
    using namespace std;
    
    struct compare
    {
      bool operator()(const int& l, const int& r)
      {
          return l < r;
      }
    };
    
    int main()
    {
        priority_queue<int,vector<int>, compare > pq;
    
        pq.push(3);
        pq.push(5);
        pq.push(1);
        pq.push(8);
        while ( !pq.empty() )
        {
            cout << pq.top() << endl;
            pq.pop();
        }
        cin.get();
    }
    

    Giving the following output:

    Example 3: Priority Queues of Pointers

    Suppose you wish to implement a priority queue of pointers which point to some class object. In this example I wish to implement a priority queue of Node pointers, where Node is a class that I have defined myself.

    Having a priority queue of pointers will result in the pointers themselves being sorted, rather than (say) some value that the class object holds. Unless sorting by pointer value is what you actually want, that is.

    The solution is to specify how to sort the Node objects in the priority queue using a function object (functor), similar to that implemented in the previous section, by using a class or a struct that contains a function that takes two class objects as arguments and returns the desired comparison.

    #include <iostream>
    #include <queue>
    using namespace std;
    
    class Node
    {
    private:
    	int value;
    
    public:
    	Node( int v )  : value( v ) {}
    	int Val() const { return value; }
    };
    
    struct CompareNode : public std::binary_function<Node*, Node*, bool>                                                                                     
    {
      bool operator()(const Node* lhs, const Node* rhs) const
      {
    	 return lhs->Val() < rhs->Val();
      }
    };
    
    int main()
    {	
    	priority_queue<Node*,vector<Node*>, CompareNode > pq;
    
    	pq.push( new Node( 111 ) );
    	pq.push( new Node( 1111 ) );
    	pq.push( new Node( 1011 ) );
    	pq.push( new Node( 100 ) );
    	pq.push( new Node( 1110 ) );
    	pq.push( new Node( 101 ) );
    
    	while ( !pq.empty() )
        {
    		Node* n = pq.top();
    		cout << n->Val() << endl;
            pq.pop();
    
    		// Delete pointer that vector contains
    		delete n;
        }
        cin.get();
    }
    

    Resulting in the following output:

    Example 4: Sedgewick’s Implementation of Priority Queues

    And here is a non-STL example of a priority queue, as implemented by Robert Sedgewick. Full code listing as follows:

    #include <iostream>
    
    using namespace std;
    
    template <class Item>
    void exch(Item &A, Item &B) 
    { 
    	Item t = A; 
    	A = B; 
    	B = t; 
    } 
    
    template <class Item>
    class PQ 
    {
    private:
    
        Item *pq; 
        int N;
    public:
    
        PQ(int maxN)
        { 
    	pq = new Item[maxN];
    	N = 0;
        }
    
        int empty() const
        { return N == 0; }
    
        void insert(Item item)
        { 
    		pq[N++] = item; 
    	}
    
        Item getmax()
        { 
    		int max = 0;
            for (int j = 1; j < N; j++)
    			if (pq[max] < pq[j]) 
    				max = j;
            exch(pq[max], pq[N-1]);  
            return pq[--N];
        }
    };
    
    
    int main()
    {
    	PQ<int> pq( 8 );
    
    	pq.insert( 101 );
    	pq.insert( 111 );
    	pq.insert( 1111 );
    	pq.insert( 1001 );
    	pq.insert( 1011 );
    	pq.insert( 1010 );
    	pq.insert( 100 );
    	pq.insert( 1000 );
    
    	while ( !pq.empty() )
    	{
    		cout << pq.getmax() << "\n";
    	}
    
    	return 0;
    }
    

    The queue members being output in order of importance:

  • How to Permanently Remove Items in STL Containers

    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:

    template< class ForwardIterator, class T >
    ForwardIterator remove( ForwardIterator first,
                            ForwardIterator last,
                            const T& value );
    

    (more…)

  • 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. (more…)

  • 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 : (more…)

  • Using Template Classes to Handle Generic Data Types in STL Containers

    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. (more…)

  • Sorting Objects Using STL

    Here’s an example of how using objects (hat-tip: Paul Wolfensberger)
    (more…)

  • 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 :
    (more…)

  • User Defined Predicates

    Just some simple examples posted here as a means of easy lookup…
    (more…)