Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   STL: Combining the use of map, string and vector (http://www.go4expert.com/forums/stl-combining-map-string-vector-t28417/)

david_BS 20May2012 20:22

STL: Combining the use of map, string and vector
 
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;
}



All times are GMT +5.5. The time now is 14:52.