Showing posts with label Maps. Show all posts
Showing posts with label Maps. Show all posts

Wednesday, 24 February 2010

The power of 'typedef' in C++

C++ allows the definition of our own types based on other existing data types. We can do this using the keyword typedef, whose format is:

typedef existing_type new_type_name ;

where existing_type is a C++ fundamental or compound type and new_type_name is the name for the new type we are defining.

For example:
typedef char C;
typedef unsigned int WORD;
typedef char * pChar;
typedef char field [50];

There are many scenarios where using typedef's are very advantageous. The following program is written based on the reasons for using typedef's as defined by Herb Sutter in his book "More Exceptional C++"



//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//Program showing the advantages of using typedefs in C++

#include<iostream>
#include<map>
#include<vector>
#include<list>
#include<assert.h>

using namespace
std;

//Comment/Uncomment as required
//#define USING_MAPS
#define USING_OTHER_STL

//5 - Portability: Useful if different users want to use different STL classes
#if defined USING_MAPS
typedef map<int,int> table; //1 - Typeability: table is easier to type
#else
//4 - Flexibility: In future you could replace a map by a vector for example
typedef vector<int> table;
//typedef list<int> table;
#endif

typedef
table::iterator tableIter;

//map<int,int> multiplicationTableTill10(int num); - not very readable
table multiplicationTableTill10(int num); //2 - Readability: Easier to read

int
main()
{

int
i = 1;
table t = multiplicationTableTill10(7);
for
(tableIter t_iter = t.begin(); t_iter != t.end(); ++t_iter, ++i)
{

#if defined USING_MAPS
cout<<"7 * "<<i<<" = "<<t[i]<<endl;
#elif defined USING_OTHER_STL
cout<<"7 * "<<i<<" = "<<*t_iter<<endl;
#else
assert(0);
#endif
}
return
0;
}


typedef
int Multiplier; //int is also now written as Multiplier

//This program creates a multiplication table of the number input from 1 to 10
table multiplicationTableTill10(int num)
{

table t;
tableIter t_iter;
//3 - Communication: Multiplier is more meaningful then int in the case below
for(Multiplier i = 1; i <= 10; i++)
{

#if defined USING_MAPS
t[i] = num * i;
#elif defined USING_OTHER_STL
t_iter = t.end();
t.insert(t_iter, num * i);
#else
assert(0);
#endif
}
return
t;
}






The Output is as follows:



More information on Typedefs:


Monday, 8 June 2009

The problem with Maps in C++

The following discussion is from More Exceptional C++ By Herb Sutter:

Question 1:
a: What's wrong with the following code? How would you correct it?

map::iterator i = m.find( 13 );
if( i != m.end() )
{
const_cast( i->first ) = 9999999;
}

b: To what extent are the problems fixed by writing the following instead?

map::iterator i = m.find( 13 );
if( i != m.end() )
{
string s = i->second;
m.erase( i );
m.insert( make_pair( 9999999, s ) );
}

Consider a map named m that has the contents shown in Figure; each node within m is shown as a pair. I'm showing the internal structure as a binary tree because this is what all current standard library implementations actually use.

As the keys are inserted, the tree's structure is maintained and balanced such that a normal inorder traversal visits the keys in the usual less ordering. So far, so good.

But now say that, through an iterator, we could arbitrarily change the second entry's key, using code that looks something like the following:

1. a) What's wrong with the following code? How would you correct it?

// Example: Wrong way to change a

// key in a map m.

//

map::iterator i = m.find( 13 );

if( i != m.end() )

{

const_cast( i->first ) = 9999999; // oops!

}

Note that we have to cast away const to get this code to compile. The problem here is that the code interferes with the map's internal representation by changing the map's internals in a way that the map isn't expecting and can't deal with.

Example above corrupts the map's internal structure (see Figure). Now, for example, an iterator traversal will not return the contents of the map in key order, as it should. For example, a search for key 144 will probably fail, even though the key exists in the map. In general, the container is no longer in a consistent or usable state. Note that it is not feasible to require the map to automatically defend itself against such illicit usage, because it can't even detect this kind of change when it occurs. In Example above, the change was made through a reference into the container, without calling any map member functions.

A better, but still insufficient, solution is to follow this discipline: To change a key, remove it and reinsert it. For example:

b) To what extent are the problems fixed by writing the following instead?

// Example: Better way to change a key

// in a map m.

//

map::iterator i = m.find( 13 );

if( i != m.end() )

{

string s = i->second;

m.erase( i );

m.insert( make_pair( 9999999, s ) ); // OK

}

This is better, because it avoids any change to keys, even keys with mutable members that are significant in the ordering. It even works with our specific example. So this must be the solution, right?

Unfortunately, it's still not enough in the general case, because keys can still be changed while they are in the container. "What?" one might ask. "How can keys be changed while they're in the container, if we adopt the discipline of never changing key objects directly?" Here are two counterexamples:

Let's say the Key type has some externally available state that other code can get at—for example, a pointer to a shared buffer that can be modified by other parts of the system without going through the Key object. Let's also say that that externally available state participates in the comparison performed by Compare. Then making a change in the externally available state, even without the knowledge of the Key object and without the knowledge of the code that uses the associative container, can still change the relative ordering of keys. So in this case, even if the code owning the container tries to follow an erase-then-reinsert discipline, a key ordering change can happen at any time somewhere else and therefore without an erase-then-reinsert operation.

Consider a Key type of string and a Compare type that interprets the key as a file name and compares the contents of the files. It's obvious that even if the keys are never changed, the relative ordering of keys can still change if the files are modified by another process, or (if the file is shared on a network) even by a user on a different machine on the other side of the world.

For details see Item 8 of the book More Exceptional C++ By Herb Sutter



Tuesday, 17 March 2009

An example of bound C++ maps

The following example shows how to define a bound C++ map which can only contain maximum number of specified elements. This is just an approach and there will probably be better and more efficient approaches.


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//The example below shows a bound map. In a general C++ map you
//can add as many elements as required. In this case there can
//only be 10 elements in the map. If you try adding more it will
//generate an error
#include<iostream>
#include<map>
#include<string>
#include<assert.h>

using namespace
std;

map<int, string> freq;
int
a[10]={0,0,0,0,0,0,0,0,0,0};

int
store_in_maps(string s)
{

int
i;
for
(i=0; i < 10; i++)
{

if
(a[i]==0)
break
;
}

if
(i==10)
{

cout<<"Sorry no more Ids available in the pool"<<endl;
assert(0);
}


a[i]=1;
freq[i] = s;
return
i;
}


int
remove_from_map(string s)
{

map<int, string>::const_iterator iter;
for
(iter=freq.begin(); iter != freq.end(); ++iter)
{

if
(iter->second == s)
break
;
}

if
(iter==freq.end())
{

cout<<"Cant find the string "<<s<<endl;
return
(-1);
}

a[iter->first]=0;
return
iter->first;
}



int
main()
{

store_in_maps("first");
store_in_maps("second");
store_in_maps("third");
store_in_maps("fourth");
store_in_maps("fifth");
store_in_maps("sixth");
store_in_maps("seventh");
store_in_maps("eighth");
store_in_maps("ninth");
store_in_maps("tenth");
remove_from_map("fifth");
store_in_maps("eleventh");
remove_from_map("zahid");
remove_from_map("fourth");

map<int, string>::const_iterator iter;
for
(iter=freq.begin(); iter != freq.end(); ++iter)
{

cout<<"First: "<<(iter->first)<<" Second: "<<(iter->second)<<"\t\tPool value: "<<a[iter->first]<<endl;
}


return
0;
}



The output of the program is as follows:

Sunday, 15 March 2009

An example of advanced erase in C++ maps

The following example shows how to use erase in C++ maps


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program shows an example of how C++ maps erase work.
#include <iostream>
#include <map>

using namespace
std;

int
main ()
{

map<char,int> zgmap;
map<char,int>::iterator it,it_low,it_up;

zgmap['a']=1;
zgmap['b']=2;
zgmap['c']=3;
zgmap['d']=4;
zgmap['e']=5;
zgmap['g']=6;
zgmap['j']=7;
zgmap['n']=8;

it_low=zgmap.lower_bound ('c'); // it_low points to c
it_up=zgmap.upper_bound ('j'); // it_up points to n and not j

zgmap.erase(it_low,it_up); // erases all elements from c to j

//Print Map contents
cout<<"\n** Printing the zgmap contents **"<<endl;
for
( it=zgmap.begin() ; it != zgmap.end(); it++ )
{

cout << (*it).first << " => " << (*it).second << endl;
}


return
0;
}



The output of the program is as follows:

Thursday, 12 March 2009

An example of C++ maps

The following example shows the working of C++ maps. This also has a simple example of union which is sometimes used in C and C++ programs


//Program tested on Microsoft Visual Studio 2008 - Zahid Ghadialy
//This program shows an example of how maps work.
//In maps, the first parameter is key and second value
//The keys in the map are automatically softed from lower to higher
#include<iostream>
#include<map>
#include<string>

using namespace
std;

//defining a union that is used with newMap_
union uu
{

char
c;
int
i;
}
u;

//Lets define two different maps
//The first parameter is key and second value
map<string, int> portMap_;
map<void *, uu> newMap_;

int
main()
{

//first entry in portmap
portMap_["first"] = 1;

//example of using the iterator
map<string, int>::const_iterator it;
string z = "second";
it = portMap_.find(z); //not in the map so wont be found
if(it == portMap_.end())
{

portMap_[z] = 22; //add second element
}

//Add thrid element directly
z = "third";
portMap_[z] = 12345;

//Add 4th element by insert
portMap_.insert(pair<string,int>("fourth", 4444));

//Add 5th element by insert
portMap_.insert(pair<string,int>("fifth", 5555));


cout<<"\n** Printing the portmap_ values **"<<endl;
for
(it = portMap_.begin(); it != portMap_.end(); ++it)
cout<<"Key = "<<it->first<<" Val = "<<it->second<<endl;

cout<<"\n** Removing fourth element **"<<endl;
z = "fourth";
it = portMap_.find(z);
portMap_.erase(it);

cout<<"\n** Printing the portmap_ values **"<<endl;
for
(it = portMap_.begin(); it != portMap_.end(); ++it)
cout<<"Key = "<<it->first<<" Val = "<<it->second<<endl;

//Playing with New Map
cout<<"\n\nCreating New Map whose key is a void pointer"<<endl;

uu u_val1, u_val2;
void
*val1, *val2;
u_val1.i = 70, val1 = &u_val1;
newMap_[val1]=u_val1;

val2 = val1;
map<void *, uu>::const_iterator it_new;
it_new = newMap_.find(val2);
if
(it_new != newMap_.end())
{

u_val2 = it_new->second;
cout<<"Note that since u_val2 is a union you can print i or c as required"<<endl;
cout<<"val2 = "<<val2<<" value.c = "<<u_val2.c<<endl;
cout<<"val2 = "<<val2<<" value.i = "<<u_val2.i<<endl;
}


return
0;
}



The output is as follows: