1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

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:
    16
    Likes Received:
    3
    Trophy Points:
    0
    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