Turn on/off buffering for streams in c++

Use io manipulator ‘unitbuf’ to turn off stream buffering and ‘nounitbuf’ to turn on stream buffering. If ‘unitbuf’ is on the stream object is flushed after every insertion else the stream is not force flushed. For e.g. endl triggers a flush in cases of ‘nounitbuf’. Sample code: fstream fstr( “c://Hello.txt” ); [sourcecode language=”cpp”]cout << unitbuf […]
Continue reading…

 

back_inserter explained

back_inserter stl iterator class is an adapter class. So what is an adapter? If you buy a laptop in India and then take it to the US/Europe, plugs given won’t fit into the sockets there, so what we do is buy an adapter, which adapts our laptop’s plug to the sockets there. Adapter pattern based […]
Continue reading…

 

Working with the STL iterators

Almost every programmer some how gets involved with STL iterators while programming. I’m quite fond of these concepts. So what exactly is an iterator? STL stands for the Standard Template Library which has different flavors or implementations floating around with different compilers but the concept and structure behind all of them is the same. It’s […]
Continue reading…

 

Reading contents of a file to a vector at one go!

It’s quite easy to read the contents of a file to a vector at one go. For this we need to get the size of a file and ask vector to allocate a buffer to hold these many bytes (use vector::resize()). Here is a function which does this. Note that it’s a byte/char vector. [sourcecode […]
Continue reading…

 

Some interesting uses of count_if stl algorithm function!

STL algorithm count_if function is a fun function with some serious usages associated with it. Main benefit of using count_if is that we can use normal character arrays/pointers along with normal CRT functions like isupper, islower etc. Using count_if to get count of lower/upper case characters in a string Just one function call and we […]
Continue reading…

 

Dumping contents of a vector/list to cout

Is it possible to do this with just one line of code? Oh yes! Have a look… [sourcecode language=”cpp”]typedef int VT; typedef std::vector VTVec; void Dump( VTVec& VecToPrint, std::ostream& stream, const char* lpszMsg ) { // We will be wrapping every element within a square bracket. // In the end output looks like this… [123] […]
Continue reading…

 

Filling out a vector using generate function

Let us suppose that you want to fill out a vector with 10000 random elements, phew. Hmm so what you would do is to write a for loop, iterate through, and fill out the elements. Well I can do it with just one line of code. 🙂 Look [sourcecode language=”cpp”]// Typedefs typedef int VtElem; typedef […]
Continue reading…

 

Initializing vector elements to a particular value

There are 8 constructors for std::vector class, out of which there is this constructor that takes two parameters… [sourcecode language=”cpp”]vector( size_type _Count,  const Type& _Val );[/sourcecode] First parameter is size of the vector, second is default value for all elements in the vector. Let’s suppose you have 1000 Employees in your organization each of them has an initial […]
Continue reading…

 

Using std transform function

So what does std::transform function do? “Applies a specified function object to each element in a source range or to a pair of elements from two source ranges and copies the return values of the function object into a destination range.” Some general applications of using transform is as follows… For doing some kind of operation […]
Continue reading…