C++ Vector<String> swap lines

Hi experts,
I have a program that reads in a  file, stores this into a vector<string> lines construct, but I am wanting to be to perform some kind of search / replace functionality, I have looked into string.find using npos but I am unsure how to go about this.

Not sure how I would handle if the 'replace' text is longer than the original text, how would i handle that...
How would I be able to find all occurances or the word (and their start and end position on the line) and then how to replace it.

Any assistance or links to resources would be of great help.
Seraph.

P.S: I am using Ubuntu Linux as my OS... (Just so you know)
LVL 1
seraph_matrix_631Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>> How would I be able to find all occurances or the word (and their start and end position on the line) and then how to replace it.

The idea is to use 'find' in a loop, and for each found substring, call 'replace' to replace it with the new one.

        http://www.cplusplus.com/reference/string/string/find/
        http://www.cplusplus.com/reference/string/string/replace/
0
Infinity08Commented:
Something like :
int replace_all(std::string& str, const std::string& from, const std::string& to) {
  size_t pos = 0;
  int cnt = 0;
  while ((pos = str.find(from, 0)) != std::string::npos) {
    str.replace(pos, from.length(), to);
    pos += to.length();
    ++cnt;
  }
  return cnt;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
The str.find(from, 0) should have been str.find(from, pos) obviously.
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

seraph_matrix_631Author Commented:
Ahh ok! Brilliant!
I will test this soon and get back to you.

Thank you for the quick responses =D
0
seraph_matrix_631Author Commented:
Hey ya
When I use that function and pass in the parameters required I get a nice long error:
 warning: multi-character character constant
 warning: character constant too long for its type
 In function ‘int main()’:
error: invalid initialization of reference of type ‘std::string&’ from expression of type ‘std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,   std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >’
  error: in passing argument 1 of ‘int replace_all(std::string&, const std::string&, const std::string&)’

I am using namespace std; so i removed the std:: bits from the function, if that would cause this error?

The parameters I am passing in are:
   replace_all(lines, 'TEXT', 'REPLACED');


lines is of type vector<string> lines;


0
Infinity08Commented:
>>  warning: multi-character character constant
>>  warning: character constant too long for its type

single quotes are used for character constants. For string constants, you need to use double quotes. ie. :

        replace_all(lines, "TEXT", "REPLACED");


>> error: invalid initialization of reference of type ‘std::string&’ from expression of type ‘std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >,   std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >’
>>   error: in passing argument 1 of ‘int replace_all(std::string&, const std::string&, const std::string&)’

The first parameter of the function is a string, not a vector of strings. If you want, you can call the function for each string in the vector, using a loop.
0
seraph_matrix_631Author Commented:
Brilliant
Made the loop and a few minor changes and all works!

Thank you! :D
0
seraph_matrix_631Author Commented:
Quick and accurate replies. Very pleased
0
Infinity08Commented:
Great :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.