does anyone know how to define the method/function The positionOf() method

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lafayettejohnson
    New Member
    • Jul 2010
    • 7

    does anyone know how to define the method/function The positionOf() method

    The positionOf() method, it is declared as private in the private section of the set class declared in the .h file. You define the method the exact same way you define public methods, in the .cpp implementation file for the class. The private means you can call this method from any class method but not from outside the class, for instance, in the main method.

    int set::positionOf (SET_ELEMENT_TY PE searchElement)

    Returns the position of searchElement or -1 if not found by sequentially searching the vector for the searchElement.

    Use a for loop to locate the searchElement if it is in the vector. Use the four iterator methods in the for loop: first(); !isDone(); next() and use currentItem() to check if it is equivalent to the searchElement. If the searchElement is found return the value stored in my_index.

    If not found, outside the for loop return -1, meaning the searchElement was not found
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    To define it one first need to know what is the set class it belongs to (its declaration). The requirements mention "four iterator methods" one of them is "first()" but what type does it return? They expect something like common stl iterator usage
    int index = 0;
    for(iterator_ty pe it = first(); !isDone(); next(), index++)
    {
    if (it.currentItem () == searchElement)
    return index;
    }
    return -1;

    Comment

    Working...