简体   繁体   English

按特定顺序输出地图

[英]Outputting a map in a specific order

Hopefully I can explain exactly what's going on, but basically I have a map of words and their corresponding line numbers on a document that is read in by the program. 希望我能确切解释发生了什么,但是基本上我在程序读取的文档上有单词图及其对应的行号。 I can output the map and everything with the words and their line numbers but I'm confused on how to change how they output. 我可以输出地图以及带有单词及其行号的所有内容,但是我对如何更改其输出方式感到困惑。 So here is the code: 所以这是代码:

here is the main: 这是主要的:

#include <iostream>
#include <string>
#include <vector>
#include <set>
#include <algorithm>
#include <fstream>
#include "dictionary.h"
#include "document.h"

using namespace std;

void sentancetoword(string sentance, set<string> words, int lineNum)
{
    dictionary d;
    document doc;
    bool wordCheck;
    string word;
    stringstream ss(sentance);

    while (ss >> word)
    {
        wordCheck = d.findWord(word, words);
        if(!wordCheck)
        {
            doc.missingMap(word, lineNum);
        }
    }
    doc.displayMap();

}

string letterCheck(string sentance)
{
    for(unsigned i = 0; i < sentance.length(); i++)
        {
            if (!isalpha(sentance[i]))
            {
                sentance[i] = ' ';
            }
        }
    return sentance;
}

int main(int argc, char* argv[])
{
    dictionary dic;
    document doc;
    set<string> words;
    set<string>::iterator it;
    string doc_word;
    int lineNum = 1;
    ifstream in;
    in.open(argv[1]);
    string word;

    while (in >> word)
    {
        transform(word.begin(), word.end(), word.begin(), ::tolower);
        words.insert(word);
    }

    in.close();

    //dic.makeSet(words);

    ifstream in2;
    in2.open(argv[2]);
    while (getline(in2, doc_word))
    {
        transform(doc_word.begin(), doc_word.end(), doc_word.begin(), ::tolower);
        doc_word = letterCheck(doc_word);
        sentancetoword(doc_word, words, lineNum);
        lineNum++;

    }

    in2.close();

    system("pause");
    return 0;

}


#include "document.h"


document::document(void){}
document::~document(void){}

void document::missingMap(string word, int lineNum)
{
    misspelled[word].push_back(lineNum);        
}
void document::displayMap()
{
    for (map<string, vector<int>>::iterator i = misspelled.begin(); i != misspelled.end(); i++)
    {
        cout << i->first << ": ";
        for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); j++)
        {
            cout << *j << endl;
        }
    }



}

so the last function is doing the outputting of the map and it outputs as follows: 因此最后一个函数正在执行地图的输出,其输出如下:

debugging: 1
process: 2
removing: 2
programming: 3
process: 4
putting: 4

but i need it to output like this: 但我需要它输出像这样:

debugging: 1
process: 2 4
programming: 3
putting: 4
removing: 2

is there something I'm doing wrong in the code or do i need to add a sort function to sort it by the words? 我在代码中有做错什么吗?还是我需要添加排序功能以按单词对它进行排序? I'm honestly lost and don't know where to go from here to get it to output only the word one time followed by the line numbers it appears on. 老实说,我迷路了,不知道从这里到哪里只能输出一个单词,然后输出出现的行号。 If anyone could help that would be great, and if any more information is needed I'll be happy to add it to the question! 如果有人可以提供帮助,那就太好了,如果需要更多信息,我很乐意将其添加到问题中! Thanks! 谢谢!

Your output doesn't make sense, though I think you will want to do this: 您的输出没有意义,尽管我认为您会这样做:

cout << i->first << ": ";
for (vector<int>::iterator j = i->second.begin(); j != i->second.end(); j++)
{
    cout << *j << " ";
}
cout << "\n"; //endl is okay, but I prefer only for I really do need to flush the stream

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

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