Thread: Vector of a struct containing another vector

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    The larch
    Join Date
    May 2006
    Posts
    3,573
    A map<string, set<unsigned> > is what I thought about too.

    Here's how it might look like. Note that inserting a keyword and a pagenumber, while making sure that they are in sorted order and unique takes just one simple line.

    Code:
    #include <iostream>
    #include <string>
    #include <map>
    #include <set>
    #include <cctype>
    
    int main()
    {
        typedef std::map<std::string, std::set<unsigned> > Index;
        Index index;
        std::string line;
        unsigned line_num = 0;
    
        //enter empty line to stop
        while (std::cout << ++line_num << ": " && std::getline(std::cin, line) && !line.empty()) {
            //tokenize
            std::string::iterator word_start, word_end = line.begin();
            while (
                word_start = std::find_if(word_end, line.end(), std::ptr_fun<int, int>(std::isalnum)),
                word_end = std::find_if(word_start, line.end(), std::not1(std::ptr_fun<int, int>(std::isalnum))),
                word_start != word_end
            ) {
                //could be converted to lower-case
                if (word_end - word_start > 3)
                    index[std::string(word_start, word_end)].insert(line_num);
            }
        }
        //display what we got
        for (Index::iterator word_it = index.begin(); word_it != index.end(); ++word_it) {
            std::cout << word_it->first << ':';
            for (std::set<unsigned>::iterator line_it = word_it->second.begin(); line_it != word_it->second.end(); ++line_it) {
                std::cout << ' ' << *line_it;
            }
            std::cout << '\n';
        }
    }
    However, perhaps if you are going to index once but use the index a lot, you might also just throw the line (page) numbers into a vector and finally sort them and remove duplicates.
    Last edited by anon; 12-05-2008 at 06:17 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Quote Originally Posted by anon View Post
    A map<string, set<unsigned> > is what I thought about too.

    Here's how it might look like. Note that inserting a keyword and a pagenumber, while making sure that they are in sorted order and unique takes just one simple line.

    Code:
    #include <iostream>
    #include <string>
    #include <map>
    #include <set>
    
    int main()
    {
        typedef std::map<std::string, std::set<unsigned> > Index;
        Index index;
        std::string line;
        unsigned line_num = 0;
    
        //enter empty line to stop
        while (std::cout << ++line_num << ": " && std::getline(std::cin, line) && !line.empty()) {
            //tokenize
            std::string::iterator word_start, word_end = line.begin();
            while (
                word_start = std::find_if(word_end, line.end(), std::ptr_fun<int, int>(std::isalnum)),
                word_end = std::find_if(word_start, line.end(), std::not1(std::ptr_fun<int, int>(std::isalnum))),
                word_start != word_end
            ) {
                //could be converted to lower-case
                if (word_end - word_start > 3)
                    index[std::string(word_start, word_end)].insert(line_num);
            }
        }
        //display what we got
        for (Index::iterator word_it = index.begin(); word_it != index.end(); ++word_it) {
            std::cout << word_it->first << ':';
            for (std::set<unsigned>::iterator line_it = word_it->second.begin(); line_it != word_it->second.end(); ++line_it) {
                std::cout << ' ' << *line_it;
            }
            std::cout << '\n';
        }
    }
    However, perhaps if you are going to index once but use the index a lot, you might also just throw the line (page) numbers into a vector and finally sort them and remove duplicates.
    Wow that's perfect. Don't really have a clue about how it works though. Would be perfect for this project though.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    122
    Quote Originally Posted by Todd88 View Post
    Wow that's perfect. Don't really have a clue about how it works though. Would be perfect for this project though.
    Could you maybe alter it a bit so it takes in my parameters? That would be a perfect solution to my problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Vector class
    By Desolation in forum C++ Programming
    Replies: 2
    Last Post: 05-12-2007, 05:44 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. towers of hanoi problem
    By aik_21 in forum C Programming
    Replies: 1
    Last Post: 10-02-2004, 01:34 PM
  5. Operators for 3D Vector Mathematics
    By Anarchist in forum C++ Programming
    Replies: 10
    Last Post: 01-31-2003, 07:33 PM