how to seach line by line and return matched lines in C++ -
i need search key word "stack overflow" in std::string , return lines sting found.
so std::string might this,
ghfhfhfhf dghfhf dtgd \n dgdhfghfgfh stack overflow \n stack overflow dgdfgdgdg \n dgdgh dgdrgdrgdg \n stack overflow dffdbdb \n notice, thee multiple lines in std::string "\n" indicating end of line. function search "stack overflow" , return vector of sting contains lines match.
for example, return vector like,
v[0] dgdhfghfgfh stack overflow \n v[1] stack overflow dgdfgdgdg \n v[2] stack overflow dffdbdb \n the goal know how many lines had hit , did line like. can please me this?
this needs run on linux redhat.
you can put each line std::string and, using find("stack overflow"), search substring. once found, push vector:
while (std::getline(file, line)) { if (line.find("stack overflow") != line.end()) v.push_back(line); } the above works strings if wrap string std::stringstream , use in place of file.
Comments
Post a Comment