Thread: Help me understand the program!

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    112

    Help me understand the program!

    Help me understand the program!

    Good to everyone universal!

    What in this line is it? What is each element?

    Am I right to understand that each found in the location program will be replaced by a compiler for pair <short, short>?
    And what is a pair <short, short>? And what's wrong with that?

    ||=== Build: Debug in vec6.14 (compiler: GNU GCC Compiler) ===|
    E:\GRAF\file\vec6.14\vec6.14.cpp|20|error: 'pair' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp|21|error: 'vector' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp|22|error: 'vector' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp|23|error: 'pair' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp|30|error: 'string' has not been declared|
    E:\GRAF\file\vec6.14\vec6.14.cpp|51|error: 'string' has not been declared|
    E:\GRAF\file\vec6.14\vec6.14.cpp|55|error: 'vector' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp|56|error: 'text_loc' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp|57|error: 'map' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp|59|error: 'string' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp||In constructor 'TextQuery::TextQuery()':|
    E:\GRAF\file\vec6.14\vec6.14.cpp|27|error: 'memset' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|30|error: 'filt_elems' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|62|error: 'string' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp||In member function 'void TextQuery::retrieve_text()':|
    E:\GRAF\file\vec6.14\vec6.14.cpp|76|error: 'string' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|76|note: suggested alternative:|
    c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\st ringfwd.h|62|note: 'std::string'|
    E:\GRAF\file\vec6.14\vec6.14.cpp|78|error: 'cout' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|79|error: 'cin' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|79|error: 'file_name' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|81|error: 'ifstream' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|81|note: suggested alternative:|
    c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\iosfwd| 157|note: 'std::ifstream'|
    E:\GRAF\file\vec6.14\vec6.14.cpp|82|error: 'infile' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|83|error: 'cerr' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|89|error: 'lines_of_text' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|89|error: 'vector' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp|89|error: 'allocator' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|89|note: suggested alternative:|
    c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\me moryfwd.h|64|note: 'std::allocator'|
    E:\GRAF\file\vec6.14\vec6.14.cpp|89|error: expected primary-expression before ';' token|
    E:\GRAF\file\vec6.14\vec6.14.cpp|90|error: expected ';' before 'textline'|
    E:\GRAF\file\vec6.14\vec6.14.cpp|92|error: 'infile' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|92|error: 'textline' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|92|error: 'getline' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|92|note: suggested alternative:|
    c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\ba sic_string.h|2827|note: 'std::getline'|
    E:\GRAF\file\vec6.14\vec6.14.cpp||In member function 'void TextQuery::separate_words()':|
    E:\GRAF\file\vec6.14\vec6.14.cpp|100|error: 'vector' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|100|note: suggested alternative:|
    c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\st l_vector.h|214|note: 'std::vector'|
    E:\GRAF\file\vec6.14\vec6.14.cpp|100|error: 'string' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|100|note: suggested alternative:|
    c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\st ringfwd.h|62|note: 'std::string'|
    E:\GRAF\file\vec6.14\vec6.14.cpp|100|error: 'allocator' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|100|note: suggested alternative:|
    c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\me moryfwd.h|64|note: 'std::allocator'|
    E:\GRAF\file\vec6.14\vec6.14.cpp|100|error: 'words' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|100|error: 'vector' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp|100|error: expected primary-expression before ';' token|
    E:\GRAF\file\vec6.14\vec6.14.cpp|101|error: 'location' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|101|error: 'locations' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|102|error: 'vector' does not name a type|
    E:\GRAF\file\vec6.14\vec6.14.cpp|102|error: expected primary-expression before ';' token|
    E:\GRAF\file\vec6.14\vec6.14.cpp|104|error: 'lines_of_text' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|108|error: expected ';' before 'textline'|
    E:\GRAF\file\vec6.14\vec6.14.cpp|109|error: 'string' is not a class, namespace, or enumeration|
    E:\GRAF\file\vec6.14\vec6.14.cpp|110|error: 'string' is not a class, namespace, or enumeration|
    E:\GRAF\file\vec6.14\vec6.14.cpp|112|error: 'pos' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|112|error: 'textline' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|113|error: 'string' is not a class, namespace, or enumeration|
    E:\GRAF\file\vec6.14\vec6.14.cpp|116|error: 'prev_pos' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|118|error: 'make_pair' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|118|note: suggested alternative:|
    c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\st l_pair.h|276|note: 'std::make_pair'|
    E:\GRAF\file\vec6.14\vec6.14.cpp|123|error: 'textline' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|123|error: 'prev_pos' was not declared in this scope|
    E:\GRAF\file\vec6.14\vec6.14.cpp|123|error: 'pos' was not declared in this scope|
    ||More errors follow but not being shown.|
    ||Edit the max errors limit in compiler options...|
    ||=== Build failed: 50 error(s), 0 warning(s) (0 minute(s), 27 second(s)) ===|


    Code:
    //6.14 The Complete Program
    //This section presents the full working program developed within this chapter, with two modifications: rather than preserve the procedural organization of separate data structures and functions, we have introduced a TextQuery class to encapsulate both (we'll look at this use of a class in more detail in subsequent chapters), and we are presenting the text as it was modified to compile under the currently available implementations. The iostream library reflects a prestandard implementation, for example. Templates do not support default arguments for template parameters. To have the program run on your current system, you may need to modify this or that declaration.
    
    // standard library header files
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <utility>
    #include <map>
    #include <set>
    
    // prestandard iostream header file
    #include <fstream>
    
    // Standard C header files
    #include <stddef.h>
    #include <ctype.h>
    
    // typedefs to make declarations easier
    typedef pair<short,short>          location;
    typedef vector<location,allocator> loc;
    typedef vector<string,allocator>   text;
    typedef pair<text*,loc*>           text_loc;
    
    class TextQuery {
    public:
       TextQuery() { memset( this, 0, sizeof( TextQuery )); }
    
       static void
              filter_elements( string felems ) { filt_elems = felems; }
    
       void query_text();
       void display_map_text();
       void display_text_locations();
       void doit() {
            retrieve_text();
            separate_words();
            filter_text();
            suffix_text();
            strip_caps();
            build_word_map();
       }
    
    private:
    
       void retrieve_text();
       void separate_words();
       void filter_text();
       void strip_caps();
       void suffix_text();
       void suffix_s( string& );
       void build_word_map();
    
    private:
       vector<string,allocator>        *lines_of_text;
       text_loc                        *text_locations;
       map< string,loc*,
               less<string>,allocator> *word_map;
       static string                        filt_elems;
    };
    
    string TextQuery::filt_elems( "\",.;:!?)(\\/" );
    
    int main()
    {
       TextQuery tq;
       tq.doit();
       tq.query_text();
       tq.display_map_text();
    }
    
    void
    TextQuery::
    retrieve_text()
    {
       string file_name;
    
       cout < "please enter file name: ";
       cin >> file_name;
    
       ifstream infile( file_name.c_str(), ios::in );
       if ( !infile ) {
             cerr < "oops! unable to open file "
                  < file_name < " -- bailing out!\n";
             exit( -1 );
       }
       else cout < "\n";
    
       lines_of_text = new vector<string,allocator>;
       string textline;
    
       while ( getline( infile, textline, '\n' ))
               lines_of_text->push_back( textline );
    }
    
    void
    TextQuery::
    separate_words()
    {
       vector<string,allocator> *words = new vector<string,allocator>;
       vector<location,allocator> *locations =
                     new vector<location,allocator>;
    
       for ( short line_pos = 0; line_pos < lines_of_text->size();
             line_pos++ )
    {
             short word_pos = 0;
             string textline = (*lines_of_text)[ line_pos ];
             string::size_type eol = textline.length();
             string::size_type pos = 0, prev_pos = 0;
    
             while (( pos = textline.find_first_of( ' ', pos ))
                         != string::npos )
             {
                      words->push_back(
                          textline.substr( prev_pos, pos - prev_pos ));
                      locations->push_back(
                           make_pair( line_pos, word_pos ));
                      word_pos++; pos++; prev_pos = pos;
             }
    
             words->push_back(
                 textline.substr( prev_pos, pos - prev_pos ));
    
             locations->push_back(make_pair(line_pos,word_pos));
       }
    
       text_locations = new text_loc( words, locations );
    }
    void
    TextQuery::
    filter_text()
    {
       if ( filt_elems.empty() )
            return;
    
       vector<string,allocator> *words = text_locations->first;
    
       vector<string,allocator>::iterator iter = words->begin();
       vector<string,allocator>::iterator iter_end = words->end();
    
       while ( iter != iter_end )
       {
            string::size_type pos = 0;
            while (( pos = (*iter).find_first_of( filt_elems, pos ))
                       != string::npos )
                       (*iter).erase(pos,1);
            ++iter;
    }
    }
    
    void
    TextQuery::
    suffix_text()
    {
       vector<string,allocator> *words = text_locations->first;
    
       vector<string,allocator>::iterator iter = words->begin();
       vector<string,allocator>::iterator iter_end = words->end();
    
       while ( iter != iter_end )
       {
          if ( (*iter).size() <= 3 )
                { iter++; continue; }
    
          if ( (*iter)[ (*iter).size()-1 ] == 's' )
                suffix_s( *iter );
    
          // additional suffix handling goes here ...
    
         iter++;
       }
    }
    
    void
    TextQuery::
    suffix_s( string &word )
    {
       string::size_type spos = 0;
       string::size_type pos3 = word.size()-3;
    
       // "ous", "ss", "is", "ius"
       string suffixes( "oussisius" );
    
       if ( ! word.compare( pos3, 3, suffixes, spos, 3 ) ||
            ! word.compare( pos3, 3, suffixes, spos+6, 3 ) ||
            ! word.compare( pos3+1, 2, suffixes, spos+2, 2 ) ||
            ! word.compare( pos3+1, 2, suffixes, spos+4, 2 ))
                  return;
    
       string ies( "ies" );
       if ( ! word.compare( pos3, 3, ies ))
       {
            word.replace( pos3, 3, 1, 'y' );
            return;
       }
    
       string ses( "ses" );
       if ( ! word.compare( pos3, 3, ses ))
       {
            word.erase( pos3+1, 2 );
            return;
       }
    
       // erase ending 's'
       word.erase( pos3+2 );
    
       // watch out for "'s"
       if ( word[ pos3+1 ] == '\'' )
            word.erase( pos3+1 );
    }
    
    void
    TextQuery::
    strip_caps()
    {
       vector<string,allocator> *words = text_locations->first;
    
       vector<string,allocator>::iterator iter = words->begin();
       vector<string,allocator>::iterator iter_end = words->end();
    
       string caps( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
    
       while ( iter != iter_end ) {
               string::size_type pos = 0;
               while (( pos = (*iter).find_first_of( caps, pos ))
                           != string::npos )
                      (*iter)[ pos ] = tolower( (*iter)[pos] );
            ++iter;
       }
    }
    
    
    void
    TextQuery::
    build_word_map()
    {
       word_map = new map< string, loc*, less<string>, allocator >;
    
       typedef map<string,loc*,less<string>,allocator>::value_type
                value_type;
       typedef set<string,less<string>,allocator>::difference_type
                diff_type;
    
       set<string,less<string>,allocator> exclusion_set;
    
       ifstream infile( "exclusion_set" );
       if ( !infile )
       {
          static string default_excluded_words[25] = {
            "the","and","but","that","then","are","been",
            "can","can't","cannot","could","did","for",
            "had","have","him","his","her","its","into",
            "were","which","when","with","would"
          };
    
          cerr < "warning! unable to open word exclusion file! -- "
               < "using default set\n";
          copy( default_excluded_words, default_excluded_words+25,
                inserter( exclusion_set, exclusion_set.begin() ));
    }
    else {
      istream_iterator< string, diff_type >
            input_set( infile ), eos;
    
      copy( input_set, eos,
            inserter( exclusion_set, exclusion_set.begin() ));
    }
    
    // iterate through the the words, entering the key/pair
    
    vector<string,allocator> *text_words = text_locations->first;
    vector<location,allocator> *text_locs = text_locations->second;
    
    register int elem_cnt = text_words->size();
    for ( int ix = 0; ix < elem_cnt; ++ix )
    {
       string textword = ( *text_words )[ ix ];
    
       if ( textword.size() < 3 ||
            exclusion_set.count( textword ))
                  continue;
    
       if ( ! word_map->count((*text_words)[ix] ))
       { // not present, add it:
         loc *ploc = new vector<location,allocator>;
         ploc->push_back( (*text_locs)[ix] );
         word_map->insert( value_type( (*text_words)[ix], ploc ));
        }
        else (*word_map)[(*text_words)[ix]]->
                      push_back( (*text_locs)[ix] );
       }
    }
    
    void
    TextQuery::
    query_text()
    {
       string query_text;
    
       do {
         cout < "enter a word against which to search the text.\n"
              < "to quit, enter a single character ==> ";
         cin >> query_text;
    
         if ( query_text.size() < 2 ) break;
    
         string caps( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
         string::size_type pos = 0;
         while (( pos = query_text.find_first_of( caps, pos ))
                     != string::npos )
                  query_text[ pos ] = tolower( query_text[pos] );
    
         // if we index into map, query_text is entered, if absent
         // not at all what we should wish for ...
    
         if ( !word_map->count( query_text )) {
              cout < "\nSorry. There are no entries for "
                   < query_text < ".\n\n";
              continue;
         }
    
         loc *ploc = (*word_map)[ query_text ];
         set<short,less<short>,allocator> occurrence_lines;
         loc::iterator liter = ploc->begin(),
                       liter_end = ploc->end();
    
         while ( liter != liter_end ) {
           occurrence_lines.insert(
              occurrence_lines.end(), (*liter).first);
           ++liter;
         }
    
         register int size = occurrence_lines.size();
         cout < "\n" < query_text
              < " occurs " < size
              < (size == 1 ? " time:" : " times:")
              < "\n\n";
    
         set<short,less<short>,allocator>::iterator
                        it=occurrence_lines.begin();
         for ( ; it != occurrence_lines.end(); ++it ) {
              int line = *it;
    
              cout < "\t( line "
                   // don't confound user with
                   // text lines starting at 0
                   < line + 1 < " ) "
                   < (*lines_of_text)[line] < endl;
         }
    
         cout < endl;
       }
       while ( ! query_text.empty() );
       cout < "Ok, bye!\n";
    }
    
    void
    TextQuery::
    display_map_text()
    {
       typedef map<string,loc*,less<string>,allocator> map_text;
       map_text::iterator iter = word_map->begin(),
                          iter_end = word_map->end();
    
       while ( iter != iter_end ) {
         cout < "word: " < (*iter).first < " (";
    
         int           loc_cnt = 0;
         loc          *text_locs = (*iter).second;
         loc::iterator liter = text_locs->begin(),
                       liter_end = text_locs->end();
    
         while ( liter != liter_end )
         {
            if ( loc_cnt )
                 cout < ",";
            else ++loc_cnt;
    
          cout < "(" < (*liter).first
               < "," < (*liter).second < ")";
    
          ++liter;
       }
          cout < ")\n";
          ++iter;
       }
    
       cout < endl;
    }
    
    void
    TextQuery::
    display_text_locations()
    {
       vector<string,allocator> *text_words = text_locations->first;
       vector<location,allocator> *text_locs = text_locations->second;
    
       register int elem_cnt = text_words->size();
       if ( elem_cnt != text_locs->size() )
       {
            cerr < "oops! internal error: word and position vectors "
                 < "are of unequal size\n"
                 < "words: " < elem_cnt < " "
                 < "locs: " < text_locs->size()
                 < " -- bailing out!\n";
            exit( -2 );
       }
    
       for ( int ix = 0; ix < elem_cnt; ix++ )
       {
            cout < "word: " < (*text_words)[ ix ] < "\t"
                 < "location: ("
                 < (*text_locs)[ix].first < ","
                 < (*text_locs)[ix].second < ")"
                 < "\n";
       }
    
       cout < endl;
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,731
    If you want to get rid of most of those errors, you must either put "std::" in front each and every one of them or, realistically, do "using namespace std;" at the beginning.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    112
    Thank you so much!! Respect! I do not even know how I did not understand this myself. I apologize. I had to see it myself. It happens to me that blindness of attention finds.

  4. #4
    Registered User
    Join Date
    Nov 2015
    Posts
    112
    The code currently is.
    What is meant by?

    Code:
    //6.14 The Complete Program
    //This section presents the full working program developed within this chapter, with two modifications: rather than preserve the procedural organization of separate data structures and functions, we have introduced a TextQuery class to encapsulate both (we'll look at this use of a class in more detail in subsequent chapters), and we are presenting the text as it was modified to compile under the currently available implementations. The iostream library reflects a prestandard implementation, for example. Templates do not support default arguments for template parameters. To have the program run on your current system, you may need to modify this or that declaration.
    
    // standard library header files
    #include <algorithm>
    #include <string>
    #include <vector>
    #include <utility>
    #include <map>
    #include <set>
    #include <cstring>
    #include <iterator>     // std::istream_iterator
    
    // prestandard iostream header file
    #include <fstream>
    #include <iostream>
    
    // Standard C header files
    #include <stddef.h>
    #include <ctype.h>
    
    using namespace std;
    
    // typedefs to make declarations easier
    typedef std::pair<short,short>          location;
    typedef std::vector<location> loc;
    typedef std::vector<std::string>   text;
    typedef std::pair<text*,loc*>           text_loc;
    
    class TextQuery {
    public:
       TextQuery() { memset( this, 0, sizeof( TextQuery )); }
    
       static void
              filter_elements( string felems ) { filt_elems = felems; }
    
       void query_text();
       void display_map_text();
       void display_text_locations();
       void doit() {
            retrieve_text();
            separate_words();
            filter_text();
            suffix_text();
            strip_caps();
            build_word_map();
       }
    
    private:
    
       void retrieve_text();
       void separate_words();
       void filter_text();
       void strip_caps();
       void suffix_text();
       void suffix_s( string& );
       void build_word_map();
    
    private:
       vector<string>        *lines_of_text;
       text_loc                        *text_locations;
       map< string,loc*,
               less < string > >  *word_map;
       static string                        filt_elems;
    };
    
    string TextQuery::filt_elems( "\",.;:!?)(\\/" );
    
    int main()
    {
       TextQuery tq;
       tq.doit();
       tq.query_text();
       tq.display_map_text();
    }
    
    void
    TextQuery::
    retrieve_text()
    {
       string file_name;
    
       cout << "please enter file name: ";
       cin >> file_name;
    
       ifstream infile( file_name.c_str(), ios::in );
       if ( !infile ) {
             cerr << "oops! unable to open file "
                  << file_name << " -- bailing out!\n";
             exit( -1 );
       }
       else cout << "\n";
    
       lines_of_text = new vector<string>;
       string textline;
    
       while ( getline( infile, textline, '\n' ))
               lines_of_text->push_back( textline );
    }
    
    void
    TextQuery::
    separate_words()
    {
       vector<string> *words = new vector<string>;
       vector<location> *locations =
                     new vector<location>;
    
       for ( short line_pos = 0; line_pos < lines_of_text->size();
             line_pos++ )
    {
             short word_pos = 0;
             string textline = (*lines_of_text)[ line_pos ];
             string::size_type eol = textline.length();
             string::size_type pos = 0, prev_pos = 0;
    
             while (( pos = textline.find_first_of( ' ', pos ))
                         != string::npos )
             {
                      words->push_back(
                          textline.substr( prev_pos, pos - prev_pos ));
                      locations->push_back(
                           make_pair( line_pos, word_pos ));
                      word_pos++; pos++; prev_pos = pos;
             }
    
             words->push_back(
                 textline.substr( prev_pos, pos - prev_pos ));
    
             locations->push_back(make_pair(line_pos,word_pos));
       }
    
       text_locations = new text_loc( words, locations );
    }
    void
    TextQuery::
    filter_text()
    {
       if ( filt_elems.empty() )
            return;
    
       vector<string> *words = text_locations->first;
    
       vector<string>::iterator iter = words->begin();
       vector<string>::iterator iter_end = words->end();
    
       while ( iter != iter_end )
       {
            string::size_type pos = 0;
            while (( pos = (*iter).find_first_of( filt_elems, pos ))
                       != string::npos )
                       (*iter).erase(pos,1);
            ++iter;
    }
    }
    
    void
    TextQuery::
    suffix_text()
    {
       vector<string> *words = text_locations->first;
    
       vector<string>::iterator iter = words->begin();
       vector<string>::iterator iter_end = words->end();
    
       while ( iter != iter_end )
       {
          if ( (*iter).size() <= 3 )
                { iter++; continue; }
    
          if ( (*iter)[ (*iter).size()-1 ] == 's' )
                suffix_s( *iter );
    
          // additional suffix handling goes here ...
    
         iter++;
       }
    }
    
    void
    TextQuery::
    suffix_s( string &word )
    {
       string::size_type spos = 0;
       string::size_type pos3 = word.size()-3;
    
       // "ous", "ss", "is", "ius"
       string suffixes( "oussisius" );
    
       if ( ! word.compare( pos3, 3, suffixes, spos, 3 ) ||
            ! word.compare( pos3, 3, suffixes, spos+6, 3 ) ||
            ! word.compare( pos3+1, 2, suffixes, spos+2, 2 ) ||
            ! word.compare( pos3+1, 2, suffixes, spos+4, 2 ))
                  return;
    
       string ies( "ies" );
       if ( ! word.compare( pos3, 3, ies ))
       {
            word.replace( pos3, 3, 1, 'y' );
            return;
       }
    
       string ses( "ses" );
       if ( ! word.compare( pos3, 3, ses ))
       {
            word.erase( pos3+1, 2 );
            return;
       }
    
       // erase ending 's'
       word.erase( pos3+2 );
    
       // watch out for "'s"
       if ( word[ pos3+1 ] == '\'' )
            word.erase( pos3+1 );
    }
    
    void
    TextQuery::
    strip_caps()
    {
       vector<string> *words = text_locations->first;
    
       vector<string>::iterator iter = words->begin();
       vector<string>::iterator iter_end = words->end();
    
       string caps( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
    
       while ( iter != iter_end ) {
               string::size_type pos = 0;
               while (( pos = (*iter).find_first_of( caps, pos ))
                           != string::npos )
                      (*iter)[ pos ] = tolower( (*iter)[pos] );
            ++iter;
       }
    }
    
    
    void
    TextQuery::
    build_word_map()
    {
       word_map = new map< string, loc*, less<string>  >;
    
       typedef map<string,loc*,less<string> >::value_type
                value_type;
       typedef set<string,less<string> >::difference_type
                diff_type;
    
       set<string,less<string> > exclusion_set;
    
       ifstream infile( "exclusion_set" );
       if ( !infile )
       {
          static string default_excluded_words[25] = {
            "the","and","but","that","then","are","been",
            "can","can't","cannot","could","did","for",
            "had","have","him","his","her","its","into",
            "were","which","when","with","would"
          };
    
          cerr < "warning! unable to open word exclusion file! -- "
               < "using default set\n";
          copy( default_excluded_words, default_excluded_words+25,
                inserter( exclusion_set, exclusion_set.begin() ));
    }
    else {
      istream_iterator< string, diff_type >
            input_set( infile ), eos;
    
      copy( input_set, eos,
            inserter( exclusion_set, exclusion_set.begin() ));
    }
    
    // iterate through the the words, entering the key/pair
    
    vector<string> *text_words = text_locations->first;
    vector<location> *text_locs = text_locations->second;
    
    register int elem_cnt = text_words->size();
    for ( int ix = 0; ix < elem_cnt; ++ix )
    {
       string textword = ( *text_words )[ ix ];
    
       if ( textword.size() < 3 ||
            exclusion_set.count( textword ))
                  continue;
    
       if ( ! word_map->count((*text_words)[ix] ))
       { // not present, add it:
         loc *ploc = new vector<location>;
         ploc->push_back( (*text_locs)[ix] );
         word_map->insert( value_type( (*text_words)[ix], ploc ));
        }
        else (*word_map)[(*text_words)[ix]]->
                      push_back( (*text_locs)[ix] );
       }
    }
    
    void
    TextQuery::
    query_text()
    {
       string query_text;
    
       do {
         cout < "enter a word against which to search the text.\n"
              < "to quit, enter a single character ==> ";
         cin >> query_text;
    
         if ( query_text.size() < 2 ) break;
    
         string caps( "ABCDEFGHIJKLMNOPQRSTUVWXYZ" );
         string::size_type pos = 0;
         while (( pos = query_text.find_first_of( caps, pos ))
                     != string::npos )
                  query_text[ pos ] = tolower( query_text[pos] );
    
         // if we index into map, query_text is entered, if absent
         // not at all what we should wish for ...
    
         if ( !word_map->count( query_text )) {
              cout < "\nSorry. There are no entries for "
                   < query_text < ".\n\n";
              continue;
         }
    
         loc *ploc = (*word_map)[ query_text ];
         set<short,less<short>,allocator> occurrence_lines;
         loc::iterator liter = ploc->begin(),
                       liter_end = ploc->end();
    
         while ( liter != liter_end ) {
           occurrence_lines.insert(
              occurrence_lines.end(), (*liter).first);
           ++liter;
         }
    
         register int size = occurrence_lines.size();
         cout < "\n" < query_text
              < " occurs " < size
              < (size == 1 ? " time:" : " times:")
              < "\n\n";
    
         set<short,less<short>,allocator>::iterator
                        it=occurrence_lines.begin();
         for ( ; it != occurrence_lines.end(); ++it ) {
              int line = *it;
    
              cout < "\t( line "
                   // don't confound user with
                   // text lines starting at 0
                   < line + 1 < " ) "
                   < (*lines_of_text)[line] < endl;
         }
    
         cout < endl;
       }
       while ( ! query_text.empty() );
       cout < "Ok, bye!\n";
    }
    
    void
    TextQuery::
    display_map_text()
    {
       typedef map<string,loc*,less<string>,allocator> map_text;
       map_text::iterator iter = word_map->begin(),
                          iter_end = word_map->end();
    
       while ( iter != iter_end ) {
         cout < "word: " < (*iter).first < " (";
    
         int           loc_cnt = 0;
         loc          *text_locs = (*iter).second;
         loc::iterator liter = text_locs->begin(),
                       liter_end = text_locs->end();
    
         while ( liter != liter_end )
         {
            if ( loc_cnt )
                 cout < ",";
            else ++loc_cnt;
    
          cout < "(" < (*liter).first
               < "," < (*liter).second < ")";
    
          ++liter;
       }
          cout < ")\n";
          ++iter;
       }
    
       cout < endl;
    }
    
    void
    TextQuery::
    display_text_locations()
    {
       vector<string> *text_words = text_locations->first;
       vector<location> *text_locs = text_locations->second;
    
       register int elem_cnt = text_words->size();
       if ( elem_cnt != text_locs->size() )
       {
            cerr < "oops! internal error: word and position vectors "
                 < "are of unequal size\n"
                 < "words: " < elem_cnt < " "
                 < "locs: " < text_locs->size()
                 < " -- bailing out!\n";
            exit( -2 );
       }
    
       for ( int ix = 0; ix < elem_cnt; ix++ )
       {
            cout < "word: " < (*text_words)[ ix ] < "\t"
                 < "location: ("
                 < (*text_locs)[ix].first < ","
                 < (*text_locs)[ix].second < ")"
                 < "\n";
       }
    
       cout < endl;
    }
    Code:
    |269|:istream_iterator< string, diff_type >
            input_set( infile ), eos;
    E:\GRAF\file\vec6.14\vec6.14.cpp|269|error: no matching function for call to 'std::istream_iterator<std::basic_string<char>, int>::istream_iterator(std::ifstream&)'|
    E:\GRAF\file\vec6.14\vec6.14.cpp|269|note: candidates are:|
    c:\mingw\lib\gcc\mingw32\4.9.3\include\c++\bits\st ream_iterator.h|72|note: std::istream_iterator<_Tp, _CharT, _Traits, _Dist>::istream_iterator(const std::istream_iterator<_Tp, _CharT, _Traits, _Dist>&) [with _Tp = std::basic_string<char>; _CharT = int; _Traits = std::char_traits<int>; _Dist = int]|

    Code:
    |72|:istream_iterator(const istream_iterator& __obj)
          : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
            _M_ok(__obj._M_ok)
          { }

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,731
    Oh, I see. The overloaded operator for output( cout, cerr etc ) is "<<", not "<". You should change that wherever you use it.

    Also, I looked in your code a bit and, for your information, there already exists a standard function that checks if a character is an uppercase/capital letter. It's called isupper(). Maybe you should use that instead of the loop and constant searching you do now.
    Last edited by GReaper; 03-28-2017 at 05:39 PM.
    Devoted my life to programming...

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    112
    Thank you, thank you very much! Velycheznishe you so much!
    Last edited by Dmy; 03-30-2017 at 02:08 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I can't understand this program
    By tzungshianlin in forum C Programming
    Replies: 2
    Last Post: 12-11-2014, 11:30 PM
  2. Help me understand what the next program does
    By alon4963 in forum C Programming
    Replies: 4
    Last Post: 10-26-2013, 11:26 PM
  3. Can you help me understand this program?
    By SCRIPT_KITTEH in forum C Programming
    Replies: 7
    Last Post: 07-23-2013, 04:03 AM
  4. Help me understand this program
    By DCICJay in forum C Programming
    Replies: 2
    Last Post: 06-06-2011, 08:52 PM
  5. Can someone help me understand this example program
    By Guti14 in forum C Programming
    Replies: 6
    Last Post: 09-06-2004, 12:19 PM

Tags for this Thread