Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h2>The "optimized way"</h2> <p>If we ignore the "premature optimization is the root of all evil", the standard way is to add a comparator, which is easy to write:</p> <pre><code>struct MyCharComparator { bool operator()(const char * A, const char * B) const { return (strcmp(A, B) &lt; 0) ; } } ; </code></pre> <p>To use with a:</p> <pre><code>std::set&lt;const char *, MyCharComparator&gt; </code></pre> <h2>The standard way</h2> <p>Use a:</p> <pre><code>std::set&lt;std::string&gt; </code></pre> <p>It will work even if you put a static const char * inside (because std::string, unlike const char *, is comparable by its contents).</p> <p>Of course, if you need to extract the data, you'll have to extract the data through std::string.c_str(). In the other hand, , but as it is a set, I guess you only want to know if "AAA" is in the set, not extract the value "AAA" of "AAA".</p> <p><em>Note: I did read about "Please do not suggest creating std::strings", but then, you asked the "standard" way...</em></p> <h2>The "never do it" way</h2> <p>I noted the following comment after my answer:</p> <blockquote> <blockquote> <p>Please do not suggest creating std::strings - it is a waste of time and space. <strong>The strings are static</strong>, so they can be compared for <strong>(in)equality based on their address</strong>.<br></p> </blockquote> </blockquote> <p><em>This smells of C (use of the deprecated "static" keyword, probable premature optimization used for std::string bashing, and string comparison through their addresses).</em></p> <p>Anyway, <strong>you don't want to to compare your strings through their address.</strong> Because I guess the last thing you want is to have a set containing:</p> <pre><code>{ "AAA", "AAA", "AAA" } </code></pre> <p>Of course, if you only use the same global variables to contain the string, this is another story.</p> <p>In this case, I suggest:</p> <pre><code>std::set&lt;const char *&gt; </code></pre> <p>Of course, it won't work if you compare strings with the same contents but different variables/addresses.</p> <p>And, of course, it won't work with <b>static const char *</b> strings if those strings are defined in a header.</p> <p>But this is another story.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload