0

Hi,how would i need to change this code to accept strings insted integers in avl tree?

template<class T>
void InsertItem(TreeType<T>& tree)
 {
    cout << "Enter number: ";
    int num;
    cin >> num;
    tree.InsertItem(num);
    cout << "Number inserted:" << num;
    }
3
Contributors
2
Replies
23
Views
1 Year
Discussion Span
Last Post by deceptikon
1

The code ? maybe ? something like this:

template< class T >
void tqkeInAndInsertItem( TreeClass< T >& tree )
{
    cout << "Enter item: ";
    T tmp;
    cin >> tmp; // MUST HAVE defined >> for type T //
    tree.insert( tmp );
    cout << "Item inserted:" << tmp; // MUST HAVE defined << for type T //
}

Edited by David W

1

how would i need to change this code to accept strings insted integers in avl tree?

Agreed with David W. Though the function seems entirely unnecessary. It's generally better practice to avoid having functions that do multiple things (ie. gather input and insert into a data structure) in favor of just one (ie. only gather input and return it or only insert into a data structure with provided values). Since your class already has an insert method, this function doesn't really do anything to justify a separate function.

It's also inflexible in that it only takes input from cin and also unconditionally writes to cout, so you can't automate this process such as accepting input from a file silently.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.