以特定顺序输出地图
Outputting a map in a specific order
希望我可以准确地解释发生了什么,但是基本上,我在程序上读取的文档中有一张单词及其相应的线号。我可以输出地图和所有单词及其线号的所有内容,但是我对如何更改它们输出的方式感到困惑。因此,这是代码:
这是主要的:
#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;
}
}
}
因此,最后一个功能是执行地图的输出,并且输出如下:
debugging: 1
process: 2
removing: 2
programming: 3
process: 4
putting: 4
,但我需要这样的输出:
debugging: 1
process: 2 4
programming: 3
putting: 4
removing: 2
我在代码中做错了什么,还是我需要添加一个排序函数才能通过单词进行排序?老实说,我迷失了方向,不知道从这里去哪里可以让它仅输出单词一次,然后是它出现的行号。如果有人能提供帮助,那将是很棒的,如果需要更多信息,我将很乐意将其添加到问题中!谢谢!
您的输出没有意义,尽管我认为您想这样做:
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
相关文章:
- 为什么不;名字在地图上是按顺序排列的吗
- 基于多个条件处理地图中的所有元素
- 在C++中将矢量转换为嵌套地图
- 替换基于地图的所有引用
- 如何区分地图中的 0 和 false?
- 地图计数确实很重要,或者只是检查是否存在
- 如何从地图中删除矢量对象
- 是否有任何C++功能可以对地图进行排序?
- 如何使用 std::variant 打印地图键/值?
- 从矢量或地图中删除共享指针
- 在 c++ 中,有一种方法可以创建一个包含地图作为值的树状地图?
- 无限嵌套具有变体的地图
- C++一会儿循环读到地图上 2 行?
- 如何在cpp中使用地图显示给定日期范围内(在下面的问题中)的费率?
- 如何检查变量是否是C++中的地图?
- 如何使地图按值C++排序
- 修改地图内矢量中的值
- 为什么我在地图中搜索STL时差很大?
- 我正在尝试按降序对地图进行排序,但没有得到预期的结果?
- 如何包装(撰写)增强 hana 地图并访问括号运算符(运算符 [])?