0

I have a string as "Bla bla *.h *.cpp *.png" . Now I want to put all the file names in vector. So my vector will have

vec[0] = "*.h"; vec[1] = "*.cpp"; vec[2] = "*.png"; 

So below is what I wrote.

void FileMgr::parseCmdLineForTextSearch(std::string b) { for (int i = 0; i < b.length(); i++) { std::size_t pos = b.find(" *."); std::cout << "pos:" << pos << "\n"; std::size_t pos_ = b.find(pos, " "); // this line does not // work, but I want to somehow get the position where // the blank space again started so that I can get the // the substring from **pos** to just before that blank space //thus giving me the exact end point of *.cpp or *.h std::string str2 = b.substr(pos, pos_); //^^ this pos_ will help // //me to get the required substring i.e, *.cpp or *.h patternVector.push_back(str); } } 

P.S: there might be other better way to implement it. But I somehow want to do it the way I mentioned above. Also I need to make sure that even if I am finding the end point of blank space it should always be after the whole text. I dont want to end up finding the blank space which is present in between the text bla bla.Is it possible?

1
  • If you search for something starting at the position where you already found it, it will just give you the same position again. Commented Feb 11, 2015 at 21:03

1 Answer 1

1

Your loop doesn't make any sense. Your logic is done one time for each character in b, but the logic itself doesn't depend on i. So whatever it is the body of your loop does will just add the same string b.length() times to patternVector.

What you want to do instead is take advantage of what you know about b: it's a bunch of words, space-separated, some of which start with "*.". So let's do it that way:

void FileMgr::parseCmdLineForTextSearch(const std::string& b) // don't need a copy ^^^^^^^^^^^^^^^^^^ { std::istringstream iss(b); std::string word; while (iss >> word) { // for each word in b if (word.find("*.") == 0) { // if it starts with *. patternVector.push_back(word); // add it } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

I get squiggly at iss "which says incomplete type not allowed".
@tanz Add #include <sstream>

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.