比较向量的元素
comparing elements of a vector
我是c++的初学者,我能够动态地将单词从文件添加到向量数组,但是我想取每个单词并找出该单词在文件中出现的次数,并只打印单词一次并列出每次出现的行号。我不知道该从我的文字向量中走出来。有没有比较每个字符串元素的方法?这是我的源代码:
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <sstream>#include
using namespace std;
int main() {
ifstream inFile, testStream;
ofstream outFile;
vector<string> words;
string temp, choice, inFileName, outFileName, word, trash;
int idx = 0, lineCount = 0;
bool outputOpened = false;
stringstream wordStream;
for (;;) {
cout << "Options: "<< endl << "1. Index" << endl << "2. Quit" << endl
<< "Please enter an option: ";
getline(cin, temp);
choice.resize(temp.length());
transform(temp.begin(), temp.end(), choice.begin(), ::toupper);
if (choice.compare("INDEX") == 0 || choice.compare("1") == 0) {
do {
inFileName.clear();
cout << "Index Program" << endl
<< "==============" << endl << endl;
cout << "Input file name: ";
getline(cin, inFileName);
inFile.open(inFileName.c_str());
if(inFile.fail()) {
cout << "Can't open file" << endl;
if(inFile.bad()) {
cout << "Bad" << endl;
}
inFile.clear();
}
}
while (!inFile.is_open());
do {
cout << "Output file name: ";
getline( cin, outFileName);
testStream.clear();
testStream.open(outFileName.c_str());
if(testStream.good()) {
cout << "That file already exists, try again" << endl;
testStream.clear();
testStream.close();
}
else {
testStream.clear();
testStream.close();
outFile.open(outFileName.c_str());
if (outFile.good()) {
outputOpened = true;
}
}
}
while (!outputOpened);
while (inFile.peek() != EOF) {
getline(inFile,word, ' ');
lineCount++;
words.push_back(word); // now the vector 'words' contains all words in the file
}
for (idx = 0; idx < words.size(); idx++) {
outFile << words[idx] << endl;
}
}
else if (choice.compare("QUIT") == 0 || choice.compare("2") == 0) {
return 0;
}
else {
cout << temp << " is an unrecognized option, please try again" << endl;
}
}
return 0;
}
以下是一些提示:
- 代替
vector
,考虑使用map
-这将允许您将计数与给定的单词 关联。 - 当插入一个单词时,查看map是否包含该单词,如果包含,则增加计数,否则插入一个计数为1的新条目。
- 最后,遍历地图并打印单词和计数
关于你的具体问题。std::string
已经实现了operator==
,所以你可以简单地比较是否相等,例如
std::string f("foo");
std::string b("bar");
if (f == b)
std::cout << "foobar" << std::endl;
其他提示:
使用流操作一次读取一个单词,而不是用于EOF的peek()
,例如:
// assume fin is a file input stream
std::string word;
while(fin >> word)
{
if (!word.empty())
{
// do stuff with word...
}
}
对于您想要实现的目标,有一个更好的方法:std::map。您使用映射(就像链接中的示例一样),每次要添加新元素时,首先搜索它是否存在。如果没有,则用1初始化它。
yourMap[yourString]=1;
如果该字符串已经存在,则递增该计数器:
yourMap[yourString]=1+yourMap[yourString];
相关文章:
- 向量元素的引用地址与它所指向的向量元素的地址不同.为什么
- C++:如何循环通过向量中的整数元素
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 不允许在向量中添加更多元素
- 基于范围的 for 循环:迭代使用一个元素扩展的向量
- 使用并行参数向量调用元素向量的成员函数
- C++ 查找字符串中存在的元素向量
- 包含 std::threads 的元素向量
- 避免从单一元素向量转换为基元类型
- 对自定义元素向量进行排序时出现意外(至少对我来说)行为
- 如何为对元素(向量和int)配对创建unique_ptr也是unique_ptr
- 元素向量乘法 C++(代码不起作用)
- 时间和空间复杂性在二叉树的每个级别创建元素向量(NON-BST)
- 遍历结构元素向量
- 犰狳C++中的元素向量或矩阵乘法
- 为什么'std::vector<int> b{2};'创建 1 元素向量,而不是 2 元素向量?
- 可移动元素向量的大小调整是否有效?
- 唯一元素向量的c++模板函数
- 获取索引和元素-向量问题
- 定义一个生成元素向量的函数时,正确的方法是什么?