1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

STL: Combining the use of map, string and vector

Discussion in 'C++' started by david_BS, May 20, 2012.

  1. david_BS

    david_BS New Member

    Joined:
    Apr 5, 2012
    Messages:
    16
    Likes Received:
    3
    Trophy Points:
    0
    Credits: BS, OGC

    This is a basic program to demostrate the posibility of saving some strings and be retrieved their identifiers (ID).

    A pair of definitions that can be used to simplify
    Code:
    #define INDEXFIND_BY_ID * (((PrivateData*)my_data)->findByID)
    #define INDEXFIND_BY_NAME (((PrivateData*)my_data)->findByName)
    
    Code:
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    //
    //
    // UTN FRGP TSP
    // BS
    // mail: david_bs@live.com
    // web: Etalking.Com.Ar
    // 2012
    //
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    #include <windows.h>
    #include <stdio.h>
    
    #include <fstream.h>
    #include <map>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    typedef map<int,int> MapIntInt;
    
    struct PrivateData{
    	MapIntInt findByID;
    	string findByName;
    };
    
    void* my_data;
    
    #define INDEXFIND_BY_ID * (((PrivateData*)my_data)->findByID)
    #define INDEXFIND_BY_NAME (((PrivateData*)my_data)->findByName)
    
    struct Info
    {
    	char	szName[36];
    	int		iId;
    };
    
    vector<Info> infoList;
    
    void Constructor()
    {
    	my_data = new PrivateData;
    }
    
    void Destructor()
    {
     * *delete (PrivateData*)my_data;
    }
    
    Code:
    void Completa(int id, const char* name){
    
    	Info newElemento;
    
    	strcpy( newElemento.szName, name );
    	newElemento.iId = id;
    	MapIntInt::iterator foundpos = (((PrivateData*)my_data)->findByID).find( newElemento.iId );
    	
    	typedef MapIntInt::value_type Entry;
    
    	if(foundpos==((((PrivateData*)my_data)->findByID).end()))
    	{
    		int index = infoList.size();
    		infoList.push_back(newElemento);
    		(((PrivateData*)my_data)->findByID).insert(Entry(newElemento.iId,index));
    		(((PrivateData*)my_data)->findByName).assign(newElemento.szName);
    	} 
    	else
    	{
    		int index = foundpos->second;
    		infoList[index]=newElemento;
    	}
    }
    
    Getting access to the names saved in the vector
    Code:
    const char* getNamebyId(int id)
    {
    	MapIntInt::iterator foundpos = (((PrivateData*)my_data)->findByID).find( id );
    	if(foundpos==((((PrivateData*)my_data)->findByID).end())) { return "none"; }
    	int index = foundpos->second;
    	return infoList[index].szName;
    }
    
    Code:
    int main(){
    
    	Constructor();
    	Completa(0, "AK");
    	Completa(0, "SCOUT");
    	Completa(1, "AP");
    	Completa(2, "DESERT");
    
    	Completa(1, "M4");
    	Completa(1, "M4");
    	Completa(1, "M4");
    
    	cout << "La id 0 contiene: " << getNamebyId(0) << endl;
    	cout << "La id 1 contiene: " << getNamebyId(1) << endl;
    	cout << "La id 2 contiene: " << getNamebyId(2) << endl;
    
    
    	cin.get();
    	Destructor();
    	return 0;
    }
    
     

Share This Page