Boost logo

Boost :

Subject: Re: [boost] Am I missing something or is Boost: String dictionary?
From: Yakov Galka (ybungalobill_at_[hidden])
Date: 2013-05-06 11:39:28


On Mon May 6 18:26:46 2013, david.hagood_at_[hidden] wrote:
[...]
> The basic idea is:
> Given you have a string and an instance of a dictionary, you can:
> 1) check if the string is already in the dictionary.
> 2) Add the string to the dictionary and return the dictionary entry (if
> the string is already present, return existing entry).
>
> The dictionary entries contain a copy of the string. Entries may be
> compared for equality/inequality in O(1) time. The lifespan of the strings
> in the entry is managed by the entry.

What you described seems to be [string
interning](http://en.wikipedia.org/wiki/String_interning), and it can
be done simply as

    unordered_set<string> interned;

    const string *intern(const string &s) {
        return &*interned.insert(s).first;
    }

    assert(intern("abc") == intern("abc"));
    assert(intern("abc") != intern("xyz"));

Cheers,
Yakov


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk