CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: STL map in UNIX

Threaded View

  1. #1
    Join Date
    May 2007
    Posts
    437

    Angry STL map in UNIX

    I want to write shell programming in UNIX, alao i am new to unix. i have written very simple program for console

    ISSUE:
    I added some item in map as string int pair, now i am searching string read from commandline and i am unable to find it, but if i use same code with windows . i am able to do same.


    Code is as follows
    Code:
    /
    //Inclue files for libraries
    #include <stdlib.h>
    #include <ctype.h>
    #include <iostream>
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    #include <ctime> 
    #include <string>
    #include <stdio.h>
    #include <map>
    #include <algorithm>
    
    using namespace std;
    
    map <string, int> m1;
    
    int main(int argc, char* argv[])
    {
    	m1.insert(map <string, int> :: const_iterator::value_type("cd",0));		
    	m1.insert(map <string, int> :: const_iterator::value_type("dir",0));		
    	m1.insert(map <string, int> :: const_iterator::value_type("pwd",0));		
    	m1.insert(map <string, int> :: const_iterator::value_type("echo",0));		
    	m1.insert(map <string, int> :: const_iterator::value_type("help",0));		
    	m1.insert(map <string, int> :: const_iterator::value_type("quit",0));		
    	map <string, int> :: const_iterator m1_AcIter, m1_RcIter;
    	typedef pair <string, int> Int_Pair;
    
    	if(argc == 1)
    	{
    		cout<<"Please enter unix command..";
    	}
    	else
    	{
    		string command;
    		command =  argv[1];            
    		map <string, int> :: const_iterator m1_AcIter, m1_RcIter;
    		m1_AcIter   = m1.find(command);
    		if(m1_AcIter  == m1.end())
    		{
    			cout<<"Didn't find unix command - "<< command<<endl ;
    		}
    		else
    		{ 
    			system(command.c_str());
    		}
    
    	}
    	
    	map <string, int> :: iterator it;
    	for ( it=m1.begin() ; it != m1.end(); it++ )
    		cout << (*it).first << " => " << (*it).second << endl;
    	
    	return 0;
    
    }
    now my input is
    ./a.out ls

    and i received following output

    Didn't find unix command - ls
    cd => 0
    dir => 0
    echo => 0
    help => 0
    pwd => 0
    quit => 0
    but same code is working perfectly with windows,
    Last edited by ashukasama; September 5th, 2009 at 03:58 AM. Reason: simplified code.
    ashu
    always use code tag

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured