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_TYPE 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

Ketsuekiame commented: Not here to do your work for you. At least attempt it first then come here where you get stuck and tell us what you tried +0

Recommended Answers

All 3 Replies

You can also use find() to check your results: http://programmingexamples.net/index.php?title=CPP/STL/Set

The loop would be something like

while(!found)
{
 if(currentItem() != searchElement)
 {
 next();
 }
}

Once you've given it a shot let us know and we can help you if you get stuck.

David

Yeah I wish I could just use that but this is the first part of an assignment an I have to use the positionOf.

So figure out how to define set::positionOf(). Once you have made an attempt, let us know how you did.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.