Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><code>std::string</code> already includes a working overload of <code>operator&lt;</code>, so you can just compare strings directly. Java uses <code>compareTo</code> primarily because the built-in comparison operator produces results that aren't generally useful for strings. Being a lower-level language, Java doesn't support user-defined operator overloads, so it uses <code>compareTo</code> as a band-aid to cover for the inadequacy of the language.</p> <p>From your description, however, you don't need to deal with <em>any</em> of that directly at all. At least as you've described the problem, you really want is something like:</p> <pre><code>std::map&lt;std::string, std::vector&lt;int&gt; &gt; page_map; </code></pre> <p>You'll then read words in from your text file, and insert the page number where each occurs into the page map:</p> <pre><code>page_map[current_word].push_back(current_page); </code></pre> <p>Note that I've used <code>std::map</code> above, on the expectation that you may want ordered results (e.g., be able to quickly find all words from <code>age</code> to <code>ale</code> in alphabetical order). If you don't care about ordering, you may want to use <code>std::unordered_map</code> instead.</p> <p>Edit: here's a simple text cross-reference program that reads a text file (from standard input) and writes out a cross-reference by line number (i.e., each "word", and the numbers of the lines on which that word appeared).</p> <pre><code>#include &lt;map&gt; #include &lt;unordered_map&gt; #include &lt;iostream&gt; #include &lt;string&gt; #include &lt;vector&gt; #include &lt;sstream&gt; #include &lt;iterator&gt; #include "infix_iterator.h" typedef std::map&lt;std::string, std::vector&lt;unsigned&gt; &gt; index; namespace std { ostream &amp;operator&lt;&lt;(ostream &amp;os, index::value_type const &amp;i) { os &lt;&lt; i.first &lt;&lt; ":\t"; std::copy(i.second.begin(), i.second.end(), infix_ostream_iterator&lt;unsigned&gt;(os, ", ")); return os; } } void add_words(std::string const &amp;line, size_t num, index &amp;i) { std::istringstream is(line); std::string temp; while (is &gt;&gt; temp) i[temp].push_back(num); } int main() { index i; std::string line; size_t line_number = 0; while (std::getline(std::cin, line)) add_words(line, ++line_number, i); std::copy(i.begin(), i.end(), std::ostream_iterator&lt;index::value_type&gt;(std::cout, "\n")); return 0; } </code></pre> <p>If you look at the first <code>typedef</code> (of <code>index</code>), you can change it from <code>map</code> to <code>unordered_map</code> if you want to test a hash table vs. a red-black tree. Note that this interprets "word" pretty loosely -- basically any sequence of non-whitespace characters, so for example, it'll treat <code>example,</code> as a "word" (and it'll be separate from <code>example</code>).</p> <p>Note that this uses the <code>infix_iterator</code> I've posted <a href="https://codereview.stackexchange.com/q/13176/489">elsewhere</a>.</p>
 

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