首页 > 解决方案 > 将指针数组传递给函数

问题描述

这种分配称为共现问题。我的程序的重点是从文件中读取句子并忽略任何标点符号。然后,我将读取用户输入,其中用户将输入仅由空格分隔的单词,并且我必须在文件的所有句子中搜索那些确切的单词并返回找到所有单词的行号。

我现在的方法是创建一个指针数组,指向包含每个句子的单词的其他数组。

ifstream read;
string filename; 
string **txtPtr = nullptr;
int numLines = 0;

getFileName();
getNumLines(read, fileName); //stores # of lines into numLines

txtPtr = new string*[numLines];

string *lines我的问题是,我可以将指针作为or传递给函数string *lines[]吗?

标签: c++

解决方案


我会解析输入文件并建立一个索引,然后我会在该索引中查找用户输入的单词。索引将是 std::map ,其中 std::string 作为键,“Entry”结构作为值:

struct Entry {
    int line;
    int sentence;
};

typedef std::map<std::string, Entry> Index;

这就是插入的样子:

Index index;

Entry val;
val.line = 1;
val.sentence = 2;

std::string word = "hi";
index.insert(Index::value_type(word, val));

这是查找的样子:

Index::iterator it = index.find(word);
if (it != index.end())
    std::cout << "found:" << it->second.line;

我知道这不是您问题的答案,但无论如何它可能会有所帮助..


推荐阅读