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

4
  • 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). Commented Mar 8, 2016 at 20:11
  • 2
    why do you use such strange expression ((tokens)._Myfirst)[0] ? If this is std::vector then just tokens[0] Commented Mar 8, 2016 at 20:12
  • can you guide me more on coding or reference link where i can see it ? Commented Mar 8, 2016 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.. Commented Mar 8, 2016 at 20:16

1 Answer 1

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;
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

is switch good to use when we have to take care the performance ?
@R Sahu Ok, that helps thank you. Can you suggest some resources to learn C++ ?
@DharaPPatel, sure. Take a look at The Definitive C++ Book Guide and List.
Thanks a lot @R Sahu

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.