gpt4 book ai didi

c++指定分隔符以从文本文件中读取单词

转载 作者:行者123 更新时间:2023-11-28 00:08:07 25 4
gpt4 key购买 nike

我有以下代码打印文本文件中的每个唯一单词及其计数(包含 >= 30k 单词),但是它用空格分隔单词,我得到的结果如下:

enter image description here

如何修改代码以指定预期的分隔符?

template <class KTy, class Ty>
void PrintMap(map<KTy, Ty> map)
{
typedef std::map<KTy, Ty>::iterator iterator;
for (iterator p = map.begin(); p != map.end(); p++)
cout << p->first << ": " << p->second << endl;
}

void UniqueWords(string fileName) {
// Will store the word and count.
map<string, unsigned int> wordsCount;

// Begin reading from file:
ifstream fileStream(fileName);

// Check if we've opened the file (as we should have).
if (fileStream.is_open())
while (fileStream.good())
{
// Store the next word in the file in a local variable.
string word;
fileStream >> word;

//Look if it's already there.
if (wordsCount.find(word) == wordsCount.end()) // Then we've encountered the word for a first time.
wordsCount[word] = 1; // Initialize it to 1.
else // Then we've already seen it before..
wordsCount[word]++; // Just increment it.
}
else // We couldn't open the file. Report the error in the error stream.
{
cerr << "Couldn't open the file." << endl;
}

// Print the words map.
PrintMap(wordsCount);
}

最佳答案

您可以使用带有 std::ctype<char> 的流小平面imbue() ed 将您喜欢的任何字符视为空间。这样做看起来像这样:

#include<locale>
#include<cctype>

struct myctype_table {
std::ctype_base::mask table[std::ctype<char>::table_size];
myctype_table(char const* spaces) {
while (*spaces) {
table[static_cast<unsigned char>(*spaces)] = std::ctype_base::isspace;
}
}
};
class myctype
: private myctype_table,
, public std::ctype<char> {
public:
myctype(char const* spaces)
: myctype_table(spaces)
, std::ctype<char>(table) {
};
};

int main() {
std::locale myloc(std::locale(), new myctype(" \t\n\r?:.,!"));
std::cin.imbue(myloc);
for (std::string word; std::cin >> word; ) {
// words are separated by the extended list of spaces
}
}

此代码目前未测试 - 我正在移动设备上输入。我可能误用了一些 std::cypte<char>接口(interface),但在修复名称等之后沿着这些方向的东西应该可以工作。

关于c++指定分隔符以从文本文件中读取单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34402544/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com