[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to "verify" your details, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ Programming ➜ STL ➜ Initialising containers

Initialising containers

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Nick Gammon   Australia  (23,046 posts)  Bio   Forum Administrator
Date Fri 09 Jul 2004 02:26 AM (UTC)

Amended on Fri 09 Jul 2004 07:03 AM (UTC) by Nick Gammon

Message
One of the more tedious things about STL containers is that you cannot easily initialise them, like you can a straight C array.

This is discussed a bit in this post:


http://www.gammon.com.au/forum/?bbsubject_id=2910


For example, in C you can do this:


char * mydata [] = 
  {
  "The", "quick", "brown", "fox", "jumped", "over", 
  "the", "lazy", "dog"
  };


However you cannot do this, although you might want to:


vector<string> mydata2 = 
  "The", "quick", "brown", "fox", "jumped", "over", 
  "the", "lazy", "dog";


I found a reasonably neat way of working around this, an adaptation of which I present here.

It involves deriving a class based on the container you want (eg. a vector<string> or a list<int>) and then overriding:


  • operator=

    This lets you assign the base type to the container (effectively replacing the container with a single element).

    For example:


    v = "apple";


    Normally you cannot do that, because v is a container, and apple is not.

  • operator,

    This lets you assign a list of things to the container, by using the comma operator to push_back additional elements.

    For example:


    v = "apple", "orange", "pear";


    What this does is start "v" with "apple", and then push_back "orange" and "pear" into it.

  • operator+=

    This lets you add to the container, by using the += operator to append to it. You can then use the comma operator to add even more items.

    For example:


    v += "banana", "pear", "mango";


    What this does is append "banana" and then use the operator comma to also add "pear" and "mango".


The code is below. The important part (which you can use in your own applications) is in bold, the rest is the example program.

The last part of the test program shows that you can still use the normal assignment of vectors (the base class assign) to copy one vector to another.

The only requirement for this to work is that the base class supports the push_back operation. Some containers (eg. maps) will not support that.


// disable warnings about long names
#ifdef WIN32
  #pragma warning( disable : 4786)
#endif

#include <string>
#include <vector>
#include <iostream>
#include <iterator>

using namespace std;


// C is the container type (eg. vector)
template < typename C >
class init : public C
  {

  public:

   // add to container (eg. v = 1, 2, 3; )
  template <typename T>
  init & operator, ( const T & value )
    {
    push_back (value);
    return *this;
    }

  // append to container (eg. v += 4)

  template <typename T>
  init & operator+= ( const T & value )
    {
    push_back (value);
    return *this;
    }

  // replace container  (eg. v = 22)
  template <typename T>
  init & operator= ( const T & value )
    {
    clear ();
    push_back (value);
    return *this;
    }

  };  // end of class init


// general routine to show a container's contents
template <typename T>
void show (const string & prefix, 
           const T & container, 
           const char * delim = " ",
           const string & suffix = "\n\n")
  {
  cout << prefix;   // description
  copy(container.begin(), 
       container.end(), 
       ostream_iterator<string>(cout, delim));
  cout << suffix;  // final description (eg. \n )
  } // end of show

int main (void)
  {
  // vector of strings
  init <vector<string> > v;

  v = "The", "quick", "brown", "fox", "jumped", "over", 
      "the", "lazy", "dog";

  show ("Vector ...\n", v);

  vector<string> v2 (v);

  show ("Copy ...\n", v2);

  v += "and", "started", "running";

  show ("After appending ...\n", v);

  return 0;
  } // end of main



Output


Vector ...
The quick brown fox jumped over the lazy dog

Copy ...
The quick brown fox jumped over the lazy dog

After appending ...
The quick brown fox jumped over the lazy dog and started running




Note, you cannot create and initialise the vector in a single line, like this:


init < vector<int> > v4 = 1, 2, 3, 4;


This is because the compiler evaluates the expression after the "=" sign first, before creating the vector, and thus does not yet know that it has an operator comma. You need to split it into two lines, or at least, two statements, like this:


init < vector<int> > v4;
v4 = 1, 2, 3, 4;





Credits

I got this idea from "Pretty Good Initialization Library" by Thorsten Ottosen, from the CodeProject web site:


http://www.codeproject.com/vcpp/stl/PGIL.asp


That was apparently based on "InitUtil: An STL Container Initialization Library" by Leor Zolman, from this web site:


http://www.bdsoft.com/tools/initutil.html


Personally I found those a bit long. The first one was 895 lines of code, and the second one 783 lines of code. If all you want to do is initialise a vector with a string of numbers, then my code presented here is only 36 lines of code. For maps of different things (eg. numbers keyed to a string) my code only takes 41 lines. The highly general approach may be an overkill in these situations.

The CodeProject one (the first one) also requires the Boost library to compile, which is even more code to install on your system, if you don't already have it.

However, you may find these approaches more general, more reliable, who knows? You can check them out if you want.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,046 posts)  Bio   Forum Administrator
Date Reply #1 on Fri 09 Jul 2004 04:36 AM (UTC)

Amended on Fri 09 Jul 2004 06:42 AM (UTC) by Nick Gammon

Message
To continue with this theme, here are another couple of templated classes for initialising maps (and multimaps I presume).

The problem with maps is that you need pairs of data to initialise them, eg.


map <string, int> m;

m = "nick", 42, "fred", 56;


To make this work the operator comma has to switch between key and values (key in this example is "nick", value is 42).

If they are different types this is comparatively easy, as the compiler can use the type supplied to work out which is which. For instance in the above example, if a string is supplied we simply store it as the "remembered" key, and then when an int is supplied, insert both as a pair.

However if they are the same type, we need to use a boolean flag to switch between key/value/key/value, and thus doing an insert on every second thing supplied.

Unfortunately the "flag to switch" approach will not work if the key and value are different types, because we need different insertion functions (because of the different types), but if they types are the same then we will get a compiler error because we will have two identical functions.

The solution presented below is a little fiddly, and if anyone can present a neater one, please do so.

Basically you have two initialisation routines, "init_mapsame" if the map has the same type for key and data, and "init_mapdiff" if the map has different data types. Simply choose the appropriate one for the type of data in your map.

I couldn't work out a way of making the templates choose the correct one automatically, I got a "fatal compiler error" when I tried it (under Visual C++ 6).



// disable warnings about long names
#ifdef WIN32
  #pragma warning( disable : 4786)
  #define data_type referent_type
#endif

#include <string>
#include <map>
#include <iostream>
#include <iterator>

using namespace std;


// mapsame is for maps of the same thing
//  (eg. map<string, string>   or  map<int, int>  )
// in this case the data arriving in the operator comma must
// alternate between the key and the value

// C is the container type (eg. map)
template < typename C >
class init_mapsame : public C
  {

  typename C::key_type key;  // save key until we have the value

  bool have_key;    // remember whether we have a key yet or not

  public:

  // constructor - no key has arrived yet
  init_mapsame () : have_key (false) {}

   // add to container (eg. m = 1, 100, 2, 200, 3, 300; )
  init_mapsame & operator, ( const typename C::data_type & d )
    {
    if (have_key)
      insert (make_pair (key, d));
    else
      key = d;

    have_key = !have_key;
    return *this;
    }

  // append to container (eg. m += 4, 400; )
  init_mapsame & operator+= ( const typename C::key_type & k )
    {
    key = k;  // we have the key so far
    have_key = true;
    return *this;
    }

  // replace container  (eg. m = 8, 800; )
  init_mapsame & operator= ( const typename C::key_type & k )
    {
    clear ();
    key = k;
    have_key = true;
    return *this;
    }

  };  // end of class init_mapsame

// init_mapdiff is for maps of different things
//  (eg. map<string, int>   or  map<int, float>  )
// in this case the data arriving in the operator comma
// can be distinguished (key from data) by the data type

// C is the container type (eg. vector)
template < typename C >
class init_mapdiff : public C
  {

  typename C::key_type key;

  public:

   // add to container (eg. m = 1, "a", 2, "b", 3, "d"; )
  init_mapdiff & operator, ( const typename C::key_type & k )
    {
    key = k;
    return *this;
    }

   // add to container (eg. m = 1, "a", 2, "b", 3, "d"; )
  init_mapdiff & operator, ( const typename C::data_type & d )
    {
    insert (make_pair (key, d));
    return *this;
    }

  // append to container (eg. m += 4, "e"; )

  init_mapdiff & operator+= ( const typename C::key_type & k)
    {
    key = k;  // we have the key so far
    return *this;
    }

  // replace container (eg. m = 10, "x"; )
  init_mapdiff & operator= ( const typename C::key_type & k )
    {
    clear ();   // empty the container
    key = k;    // we have the key so far
    return *this;
    }

  };  // end of class init_mapdiff


// general routine to show a map container's contents
template <typename T>
void showpairs (const string & prefix,
                const T & container,
                const string & separator = "=",
                const string & delim = " ",
                const string & suffix = "\n\n")
  {
  cout << prefix;   // initial description
  for (typename T::const_iterator iter = container.begin ();
       iter != container.end ();
       iter++)
    cout << iter->first << separator << iter->second << delim;
  cout << suffix;   // final description (eg. \n )
  } // end of showpairs

int main (void)
  {

  init_mapsame <map<int, int> > m;

  m = 123, 456, 222, 789, 555, 666;

  showpairs ("m = ", m, "=", ", ");

  m += 8, 7;

  showpairs ("m after add = ", m, "=", ", ");

  init_mapdiff <map<int, float> > m2;

  m2 = 123, (float) 84.5, 567, (float) 99.0, 100, (float) 200.0;

  showpairs ("m2 = ", m2, "=", ", ");

  m2 += 44, (float) 56.7;

  showpairs ("m2 after add = ", m2, "=", ", ");

  init_mapdiff <map<string, int> > m3;

  m3 = "nick", 10, "john", 42, "peter", 123;

  showpairs ("m3 = ", m3, "=", ", ");

  return 0;

  } // end of main



Output


m = 123=456, 222=789, 555=666,

m after add = 8=7, 123=456, 222=789, 555=666,

m2 = 100=200, 123=84.5, 567=99,

m2 after add = 44=56.7, 100=200, 123=84.5, 567=99,

m3 = john=42, nick=10, peter=123,




The line near the start:


#define data_type referent_type


is because STL does not seem to have a standard typename for the data type of a map. The name key_type is standard between Windows and the Linux implementations but on Windows the data (ie. second part of the pair) is called "referent_type" however on Linux it is called "data_type". Later implementations may have different names, you may need to fiddle with, or remove, that define as required.




It might also be worth pointing out that these initialisation routines do not detect "bad data". For example, if you are initialising pairs of things and leave off the last entry, it will not be detected, eg.


init_mapdiff <map<string, int> > m;

m = "nick", 10, "john", 42, "peter";


In this example the key "peter" has been supplied but no value for "peter". Thus, the entry for "peter" will be silently discarded.

It will also not detect situations where you have two keys in a row, or two data elements in a row, eg.


init_mapdiff <map<string, int> > m;

m = "nick", "bruce", 10, "john", 42, 456;


In this example, it will store "nick", but then replace it with "bruce", eventually storing the pair <bruce, 10>.

Then when it gets "john", 42, it will store the pair <john, 42>. However the final 456 will also trigger another pair to be stored, namely <john, 456>.

You could make the classes a bit more complicated to try to detect these situations. However I am expecting that you would normally use them in program initialisation for short lists, and with proper visual layout in the program, such errors should be obvious to the eye.



- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Nick Gammon   Australia  (23,046 posts)  Bio   Forum Administrator
Date Reply #2 on Fri 09 Jul 2004 06:49 AM (UTC)
Message
Next I will present a slight variation of the first initialising class, for sets (multisets etc.) that do not have "push_back", but have "insert" instead.

Basically you replace the word "push_back" with "insert" where required.


// disable warnings about long names
#ifdef WIN32
  #pragma warning( disable : 4786)
#endif

#include <string>
#include <set>
#include <iostream>
#include <iterator>

using namespace std;

// C is the container type (eg. vector)
template < typename C >
class init_set : public C
  {

  public:

   // add to container (eg. v = 1, 2, 3; )
  template <typename T>
  init_set & operator, ( const T & value )
    {
    insert (value);
    return *this;
    }

  // append to container (eg. v += 4)

  template <typename T>
  init_set & operator+= ( const T & value )
    {
    insert (value);
    return *this;
    }

  // replace container  (eg. v = 22)
  template <typename T>
  init_set & operator= ( const T & value )
    {
    clear ();
    insert (value);
    return *this;
    }

  };  // end of class init_set


// general routine to show a container's contents
template <typename T>
void show (const string & prefix, 
           const T & container, 
           const char * delim = " ",
           const string & suffix = "\n\n")
  {
  cout << prefix;   // description
  copy(container.begin(), 
       container.end(), 
       ostream_iterator<string>(cout, delim));
  cout << suffix;  // final description (eg. \n )
  } // end of show

int main (void)
  {
  // vector of strings
  init_set <set<string> > s;

  s = "The", "quick", "brown", "fox", "jumped", "over", 
      "the", "lazy", "dog";

  show ("Set ...\n", s);

  set<string> s2 (s);

  show ("Copy ...\n", s2);

  s += "and", "started", "running";

  show ("After appending ...\n", s);

  return 0;
  } // end of main



Output


Set ...
The brown dog fox jumped lazy over quick the

Copy ...
The brown dog fox jumped lazy over quick the

After appending ...
The and brown dog fox jumped lazy over quick running started the


You can see that, unlike the vector example, the words are now in alphabetic order, as that is how sets operate.

If you had duplicate words (eg. add the word "fox" again) then with a set the duplicate will be removed, however with a multiset you will see both of them.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


14,030 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]