STL: Using list and string

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

  1. david_BS

    david_BS New Member

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

    A demostration of the usage of std::list and std::string to create part of a command management system for a console command interpreter.

    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 <list>
    #include <string>
    
    using namespace std;
    
    struct ScheduledCommand
    {
    	string comando; 
    	int prioridad; 
    };
    
    typedef list<ScheduledCommand> ScheduleList;
    ScheduleList scheduleList;
    
    The functions to add, list and erase
    Code:
    void IngresarComando(){
    
    	string cmd;
    	char comando[21];
    	memset(comando,0,sizeof(comando));
    	cout << "Introduce a command" << endl;
    	cin.getline(comando,20,'\n');
    	cmd.assign(comando);
    
    //	cout << const_cast<char*>(cmd.c_str()) << endl;
    //	system("pause");
    
    	if(cmd.empty())
    	{
    		list<ScheduledCommand>::iterator pos;
    		for(pos=scheduleList.begin();pos!=scheduleList.end();++pos)
    		{
    			printf("pri:%d -- cmd:\"%s\"\n", (*pos).prioridad, (*pos).comando.c_str() );
    		}
    		return;
    	}
    
    	ScheduledCommand tmp;
    	tmp.comando=cmd;
    	tmp.prioridad=1;
    	if(scheduleList.size()<32){
    		scheduleList.push_back(tmp);
    	}
    }
    
    void BorrarComandos(){
    
    	scheduleList.erase(scheduleList.begin(),scheduleList.end());
    }
    
    void VerificarComandos(){
    
    	cout << "\n";
    
    	int cantidad=0;
    	for( ScheduleList::iterator pos = scheduleList.begin(); pos != scheduleList.end(); ++pos)
    	{
    		// cantidad++;
    		if( (*pos).prioridad == 1 )
    		{
    			cantidad++;
    			cout << "Comando: " << const_cast<char*>((*pos).comando.c_str()) << endl;
    			scheduleList.erase(pos);
    			break;
    		}
    	}
    
    	if(cantidad==0)
    		cout << "There are no commands !" << endl;
    }
    
    Main function, used for testing purposes..
    Code:
    int main(){
    
    	IngresarComando();
    	IngresarComando();
    	IngresarComando();
    
    	VerificarComandos();
    	VerificarComandos();
    	VerificarComandos();
    
    	BorrarComandos();
    
    	VerificarComandos();
    
    	cin.get();
    	return 0;
    }
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice