tags:

views:

321

answers:

4

I have a std::set of std::string. I need the "index" or "position" of each string in the set, is this a meaningful concept in the context?

I guess find() will return an iterator to the string, so my question might be better phrased as : "How do I convert an iterator to a number?".

+5  A: 

STL distance is what you need. You will want, I guess std::distance(set.begin(), find_result)

laura
Remark: `std::distance` is O(n) since `set` iterators are models of `BidirectionalIterator` and not `RandomAccessIterator`
Matthieu M.
Cool, thanks. From a STL n00b.
Alex Strickland
+1  A: 

I don't think it is meaningful - set's are 'self keyed' and sorted thus the 'index' would be invalidated when the set is modified.

Of course it depends upon how you intend to use the index and if the set is essentially static (say a dictionary).

Gray-M
I think that as long as a container is ordered the position of its elements has semantic meaning. In particular, the actual position might be important, depending on what you are doing (timestamped data for example). If the set changes, then naturally so does the position of some elements.
laura
A: 

Despite what others have written here, I don't think that "index" or "position" has meaning with respect to a set. In mathematical terms, a set exposes only its members and maybe its cardinality. The only meaningful operations involve testing whether an item is a member of the set, and combining or subtracting sets to yield new sets.

Some people talk about sets as data structures in looser terms, by facets of being "ordered" or "unordered", and whether they permit duplicates or enforce uniqueness. The former facet distinguishes an array with an O(n) insertion guard, where an attempt to insert an item first scans the existing members to see if the new item exists and, if not, inserts the new item at the end, and a hash table, that might retain such order only within a bucket's chain. A tree such as the Red-Black Tree used by std::set is somewhere in between; its traversal order is deterministic with respect to the strict weak order imposed by the comparator predicate, but, unlike the array sketched above, it doesn't retain insertion order.

The other facet -- whether the set permits duplicate elements -- is meaningless in mathematics, and is more accurately described as a bag. Such a structure acknowledges the difference between identity and value-based "sameness".

Your problem may involve caring about some position; it's not clear what that position means, but I expect you're going to need some data structure separate from std::set to model this properly. Perhaps a std::map mapping from your set of elements to each position would do. That would not guarantee that the positions are unique.

It may also help clarify the problem to think how you'd model it as relations, such as in a relational database. What comprises the key? What portions of the entities can vary independently?

seh
I am building up a "set" of unique strings from a database, then those strings must be represented as numbers. The distance() function suffices. It is possible (probable?) that set is not a good choice, I made it because it seemed an efficient way to guarantee uniqueness of the strings. The STL is new territory for me.
Alex Strickland
If you've found a design that solves your problem, all is well. If you find yourself warming to the STL concepts, you might enjoy the book *Elements of Programming* by Stepanov and McJones (http://www.elementsofprogramming.com/).
seh
A: 

http://stackoverflow.com/questions/1796503/index-or-position-in-stdset/1810416#1810416

@seh if we see semantically, whatever you have said is correct

But the sets are ordered. They are usually implemented using some form of Balanced trees like red black tree, and use strict weak ordering to order the elements in the set. I don't know whether standard mandates this ordering or not. But this is how it is

Yogesh Arora