0

i am very new to C++ programming. I am doing my master and working on one problem given by professor. the problem is regarding performing the basic operations on binary search tree. i have one file which has below format :

I 1015291402
I 729831403
I 1005116371
F 757970570
D 1005116371
F 729831403
I 1218751282
D 1015291402
I 582339464
I 92421221

There are three basic operations Insert, Delete and Find can be done on search tree. so i need to read this file perform operation line by line. Below is the code i wrote so far.

string line;
ifstream infilesmall("inputfile_small.txt");
//ifstream infilelarge("inputfile_small.txt");

while (getline(infilesmall, line))
{
    istringstream iss(line);
    vector<string> tokens;
    copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(tokens));
    string  str = ((tokens)._Myfirst)[0];
    cout<< ((tokens)._Myfirst)[1];

    //char operation =  new char(((tokens)._Myfirst)[0]);
    /*typedef void (*funcPointer)(int);
    void String1Action(int arg);
    void String2Action(int arg);
    map<string, funcPointer> stringFunctionMap;
    stringFunctionMap.add("string1", &String1Action);*/

    insert(t,10);
    find(t,0);
    //Delete(t,10);
}

so the question what is ideal way of calling Insert, Delete and Find by splitting the line? i need to take care the performance. One approach i found out is to create enum with key and value pair and having function pointer. so depending on the key value ("I","D","F") the appropriate function will be called with it's respective value. Can you please suggest me/correct my approach and guide me on this code. Appreciate your time. Thanks

R Sahu
  • 204,454
  • 14
  • 159
  • 270
DharaPPatel
  • 12,035
  • 9
  • 31
  • 49
  • 1
    First of all, I wouldn't parse it as a string, I'd parse directly into a `char` and an `int` (as long as you trust the formatting of the input). Secondly, a set of if statements will end up being faster than a map of function pointers given the small scope (only 3 options). – kmdreko Mar 08 '16 at 20:11
  • 2
    why do you use such strange expression `((tokens)._Myfirst)[0]` ? If this is std::vector then just `tokens[0]` – PiotrNycz Mar 08 '16 at 20:12
  • can you guide me more on coding or reference link where i can see it ? – DharaPPatel Mar 08 '16 at 20:13
  • @PiotrNycz when i debug the code, i was checking it in immediate window and i was not able to get the value by token[0] there and i saw that in quick window so i put that but it works.. – DharaPPatel Mar 08 '16 at 20:16

1 Answers1

3

Your code is needlessly complex. You can read the operator and the number from the file, one pair at a time, and use the number appropriately based the value of the operator.

char op;
int number;
while ( infilesmall >> op >> number )
{
   switch (op)
   {
      case 'I':
         insertData(number);
         break;

      case 'D':
         deleteData(number);
         break;

      case 'F':
         findData(number);
         break;

      default:
         std::err << "Unknown operator, " << op << std::endl;
   }
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270