简体   繁体   English

将值插入向量图<entry>

[英]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. 我在学校为CIS算法分析课程编写程序,在如何将我从输入文件中解析的数据插入到映射(其中值是Entry类型的向量)中的过程中,我难以理解实现包含页码和范围的结构。 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} 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'; 输入的第一行完成处理后,page_Number ='2'; and ent_nam_substring = "Series|(" 和ent_nam_substring =“系列|(”

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. 在映射中,“系列”是该行的键,而“ |(”表示该页的页面范围的开始,该页范围从第2页开始。我需要做的是扫描输入的每一行,如果输入则插入一个键不存在,如果确实存在,则将下一个页码推到与该键关联的向量上,直到到达“ |)”为止,表示该标题的页码范围的末尾。

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. 顺便说一句,stricmp不可移植。 On Unux you may need strcasecmp. 在Unux上,您可能需要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... 关于其余的问题,您解释了代码应执行的操作,但是您忘了提及它实际上在做什么……

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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