Creating an Index on a vector<string>ΒΆ

// index.cpp

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// This code reads in words from cin, sorts them alphabetically, and then
// print them out.
void test1() {
    // read in all words
    vector<string> words;
    string w;
    while (cin >> w) {
        words.push_back(w);
    }

    // put them in alphabetical order
    sort(words.begin(), words.end());

    // print them out
    for(string w : words) {
        cout << w << "\n";
    }
}

bool ptr_cmp(string* a, string* b) {
    return *a < *b;
}

// Here we create an index for a vector<string>. For us, index is a
// vector<string*> where each pointer points to an element in the original
// vector<string>. We then sort the index so that the pointers point to the
// original in alphabetical order. The is usually faster than sorting the
// vector<string> itself, because no strings need to be swapped by the sorting
// algorithm (only string* pointers are swapped). Also, we this approach lets
// us create more than one index for the same vector<string>.
void test2() {
    // read in all words
    vector<string> words;
    string w;
    while (cin >> w) {
        words.push_back(w);
    }

    // make an index, i.e. a vector of pointers to the
    // strings in the words vector

    // first create an empty vector the same size as words
    vector<string*> index(words.size());
    for(int i = 0; i < words.size(); ++i) {
        index[i] = &(words[i]);  // & is the address-of operator
    }

    // sort the index (but not the words themselves)
    sort(index.begin(), index.end(), ptr_cmp); // ptr_cmp defined above

    // print out words in the order they're pointed to by the index
    for(int i = 0; i < index.size(); ++i) {
        cout << *(index[i]) << "\n";  // * is the de-reference operator
    }
}

bool ptr_size_cmp(string* a, string* b) {
    return a->size() < b->size();
}


// Shows how two index vectors can be created that refer to the same
// vector<string>.
void test3() {
    // read in all words
    vector<string> words;
    string w;
    while (cin >> w) {
        words.push_back(w);
    }

    // make an index, i.e. a vector of pointers to the
    // strings in the words vector

    // first create an empty vector the same size as words
    vector<string*> index(words.size());
    for(int i = 0; i < words.size(); ++i) {
        index[i] = &(words[i]);  // & is the address-of operator
    }

    // sort the index (but not the words themselves)
    sort(index.begin(), index.end(), ptr_cmp); // ptr_cmp defined above


    // now make another index order by string length
    vector<string*> index_size(words.size());
    for(int i = 0; i < words.size(); ++i) {
        index_size[i] = &(words[i]);  // & is the address-of operator
    }

    sort(index_size.begin(), index_size.end(), ptr_size_cmp); // ptr_size_cmp defined above

    // print out words in the order they're pointed to by the index
    for(int i = 0; i < index.size(); ++i) {
        cout << *(index[i]) << "\n";  // * is the de-reference operator
    }

    cout << "---------------------------------------------\n";

    // print out words in the order they're pointed to by the index_size
    for(int i = 0; i < index_size.size(); ++i) {
        cout << *(index_size[i]) << "\n";  // * is the de-reference operator
    }
}

int main() {
    // test1();
    // test2();
    test3();
}