Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <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>
    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.
    1. COThis actually makes a lot of sense to me instead of creating a whole class which maps a vector to the string that way. So in that case i would be adding maps (of strings and vectors) to a tree or hashtable (which is what i want to do) than when i do a find operations (in this case i have it defined to return the data) it will return a map. From which i can access the pages. However when doing comparisons in a tree will the map compare the strings or the vector? Pretty much all i need to be doing in this exercise is to store words with an index of page numbers it occurs on using different trees
      singulars
    2. COWhy would you add the map to a tree or hashtable? The map *is* a tree (and unordered_map is a hash table). It already takes you directly from word to page number(s).
      singulars
    3. COI have already constructed a redblacktree, binarytree, hashtable, chainedhashtable and a few more data structures. All of these have their own adds,removes, and finds that take a paramete T x so it can be used in any case. Thats why i though creating my own data type which stores a string and an array was the best option, i dont know how to compare though. The idea is to index the words using each of these data structures (So i can change in between then than observe the running times of each data structure) and add an array ossociated with the word that stores the page numbers it occures on.
      singulars
 

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