简体   繁体   中英

insertion of values into a map of vector<entry>

I am writing a program for a CIS Algorithm Analysis class at school, I am having some trouble understanding the implementation as far as how to insert data I have parsed from an input file into a map where the values are a vector of type Entry which is a struct containing a page number and a range. I have been reading for hours and I just cant seem to get the syntax correct.

The first 3 lines of the input file are as follows:

IX: {Series|(} {2} IX: {Series!geometric|(} {4} IX: {Euler's constant} {4}

Basically I'm parsing an index for a book and when I print out the map it should list the headings and the page ranges for all headings / sub headings.

#include <fstream>
#include <iostream>
#include <vector>
#include <string>
#include <map>

using namespace std;

#define START 0
#define END -1

struct Cmp_Insen
{
bool operator()(const string &lhs, const string &rhs) const
    {
    return stricmp(lhs.c_str(), rhs.c_str()) < 0;
    }
};

struct Entry
{
int pageNum;
int type;
};

int main( int argc, char *argv[] )
{
string inputstring, ent_nam_substring, pag_num_substring, page_number;
int ent_nam_str, ent_nam_end, pag_num_str, pag_num_end;
vector<Entry> page_range;

if( argc < 2)
    return 0;
else
{
    ifstream the_file ( argv[1] );
    if( !the_file )
        cerr<<"Could not open file!\n";
    else
    {

    map<string,vector<Entry>,Cmp_Insen> mIndex;
    //map<string,vector<Entry>,Cmp_Insen>::const_iterator iter;
    //for(iter = mIndex.begin(); iter != mIndex.end(); ++iter)


    while( !the_file.eof() )
    {
        getline( the_file, inputstring );     // Extract Entry Heading Start
        ent_nam_str = inputstring.find("{");
        ent_nam_end = inputstring.find("}");
        ent_nam_substring = inputstring.substr(ent_nam_str + 1, ent_nam_end - (ent_nam_str + 1));          // Extract Entry Heading End


        pag_num_substring = inputstring.substr(ent_nam_end + 1, inputstring.length() - ent_nam_substring.length()); // Extract Page Number Start
        pag_num_str = pag_num_substring.find("{");
        pag_num_end = pag_num_substring.find("}");
        page_number = pag_num_substring.substr(pag_num_str + 1, pag_num_end - (pag_num_str + 1));  // Extract Page Number End


    }
    }
}
return 0;

}

When the first line of input is finishes processing page_Number = '2'; and ent_nam_substring = "Series|("

In the map "Series" is the key for this line and "|(" denotes the begining of a page range for that heading which starts on page 2. What I need to do is scan each line of input and insert a key if it does not exist and if it does then push the next page number onto the vector associated with that key until "|)" is reached denoting the end of the page range for that heading.

My other related question is that if I am doing a case-insensitive comparison so that the map stays ordered on my keys, do I have to explicity call the comparison each time or once it is defined and declared in the map declaration will the map just use it whenever an operation like insert is performed?

Sorry for any confusion I am causing with a lack of knowledge, any helpful feedback would be greatly appreciated as my 2 textbooks are not providing me with the answers I am looking for.

Once you have defined the comparison operator, the map automatically handles its usage by itself. You don't have to call it.

By the way, stricmp is not portable. On Unux you may need strcasecmp. This should do the trick:

#ifndef WIN32 
  #define stricmp strcasecmp 
#endif

Regarding the rest of your question, you explained what your code should do, but you forgot to mention what is it actually doing instead...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM