C++ STL: Output Container to Stream

We can output the elements of any container to any ostream using std::copy like this. But, it would be really nice if we could output any vector like this:

std::vector<MyClass> myVec;
std::cout << myVec;

To achieve that, we just need one more wrapper around std::copy, a function template that accepts vectors of any type:

template <typename T>
std::ostream& operator << (std::ostream& os, const std::vector<T>& vec)
{
    std::copy( vec.begin(), vec.end(), std::ostream_iterator<T>( os ) );
    return os;
}

Similar functions can be written for other STL containers.

C++ STL: Output Container Elements to Stream

The std::copy function (defined in <algorithm>) can be used to easily output elements of any STL container to any ostream. To do this, we can use the std::ostream_iterator (defined in <iterator>). std::ostream_iterator can be passed a ostream object and an optional delimiter string.

For example to output a vector of integers to std::cout:

#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>

std::vector<int> iVec;
// ...
std::copy(iVec.begin(), iVec.end(), std::ostream_iterator<int>( std::cout ));
// ...
std::copy(iVec.begin(), iVec.end(), std::ostream_iterator<int>( std::cout, "\n" )); // One integer per line

ViM: vimrc

vimrc is the configuration file used by ViM. It is typically ~/.vimrc in Linux and C:\Program Files\Vim\vimrc in Windows. I currently use ViM across 3 computers with both Windows and Linux. I have my custom vimrc file which works across all these setups.

I like to keep my vimrc under source control (currently Mercurial). To make this convenient and have a single copy across partitions, I replace the default ~/.vimrc or C:\Program Files\Vim\vimrc with the following text:

source C:\FoobarRep\vimrc

Here, C:\FoobarRep\ is the directory of my repository where vimrc resides. For Linux, use the mounted path of the same directory.

Firefox: Update Intervals

I find it irritating when Firefox checks for browser and addons updates whenever it is started. Actually it does this once a day. While I would like the update check, I would like it to be more spaced out, let us say every 15 days.

  1. To do this, first ensure that udpates are enabled. They can be enabled at Tools → Options → Advanced → Updates.
  2. After this, open up about:config to set the intervals. The interval periods are integer values, set in seconds. So 15 days is 1296000 (seconds).
  3. To set the application (Firefox) update interval, set the value of app.update.interval to 1296000.
  4. To set the addons update interval, set the value of extensions.update.interval to 1296000.

Search multiple terms with multiple colors in Vim

Today I was trying to look up the occurrence of either of 3 terms in a huge log file. I had the text file open in Vim and wanted to hop to the occurrences of either term. I could have grepped the file, but highlighting of the search terms and having the ability to peek around the occurrence of these terms was important to me.

I was able to do this using the multisearch plugin. This is the latest in a series of Vim plugins written for this purpose by several authors over the years. The multisearch plugin is offered as a vimball file and can be downloaded from the bottom of the plugin page. There are 2 versions available, pick the Unix version if you are on Linux, pick the other one if you are on Windows. For help with installing a vimball, see this post.

Once multisearch is installed, open up the text file you want to search. To search for the occurrence of 3 terms, say Amar, Akbar and Anthony use:

:Msearch add /Amar/
:Msearch add /Akbar/
:Msearch add /Anthony/

You will find each of these search terms highlighted with different colors across your text file. Very nice!

To hop to the next occurrence of any of these terms in sequence use repeatedly:

:Msearch

Note that if you do not mind your multiple search terms being highlighted with the same color, then you need no plugin. Such a search capability is built into Vim. See this post for more information.

ViM: Vimballs

I noticed that plugins of ViM have now started to ship as vimballs. A vimball is a ViM-readable archive format that holds the plugin files and commands to install them. A plugin downloaded as a Vimball typically has a file extension of .vba or .vimball, say Foobar.vba.

To view the plugin files installed by a vimball and their destination directories, open Foobar.vba in ViM and use:

:VimballList

A Vimball can be installed with a single command. Open Foobar.vba in ViM and invoke:

:source %

% is a ViM read-only register that holds the name of the currently open file. :source executes Ex commands from its source, which is %, the vimball file in this case.

To view the currently loaded plugins, use:

:scriptnames

This lists all the loaded .vim plugin files with their complete path. The plugin installed from the vimball should also be listed in this output.

To remove a plugin which was installed from a Vimball use:

:RmVimball Foobar

The ability of ViM to read and execute vimballs is through a plugin called vimball. A default install of ViM should already have this plugin loaded.