c++ - 将指针数组传递给函数
问题描述
这种分配称为共现问题。我的程序的重点是从文件中读取句子并忽略任何标点符号。然后,我将读取用户输入,其中用户将输入仅由空格分隔的单词,并且我必须在文件的所有句子中搜索那些确切的单词并返回找到所有单词的行号。
我现在的方法是创建一个指针数组,指向包含每个句子的单词的其他数组。
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[]
吗?
解决方案
我会解析输入文件并建立一个索引,然后我会在该索引中查找用户输入的单词。索引将是 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;
我知道这不是您问题的答案,但无论如何它可能会有所帮助..
推荐阅读
- python - 如何使两个数组连续,以便 Numba 可以加快 np.dot()
- three.js - 三个js对象遍历不更新child
- java - JPackage 更新“PATH”环境变量
- javascript - 无法快速返回流数据
- wordpress - 删除wordpress上图片网址中的日期?
- java - 参考队列始终为空
- dart - 如何使用 dart 编程语言在 dart 控制台中显示这种输出
- python - 函数'对象在Django中没有属性'as_view
- ios - 如果声音持续时间为 24 秒,是否有任何方法可以停止播放以前的通知声音?
- php - PHP/Laravel 获取具有特定列值的行