简体   繁体   中英

Passing an array of pointers to a function

This assignment is called a co-currence problem. The point of my program is to read in sentences from a file and ignore any punctuation. Then, I will read user input where the user will enter words separated by a space only and I have to search for those exact words in all the sentences of the file and return the line number where all the words are found.

My approach now is to create a pointer array to other arrays that contain the words for each sentence.

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

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

txtPtr = new string*[numLines];

My question is, can I pass the pointer to a function as string *lines or string *lines[] ?

I would parse the input file and build an index, and then I would look up user-entered words in that index. The index would be std::map with std::string as a key and with "Entry" struct as a value:

struct Entry {
    int line;
    int sentence;
};

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

This is how insertion would look like:

Index index;

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

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

This is how lookup would look like:

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

I know it's not the answer for your question, but it might help anyway..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM