0

I want to replace a string in a vector string. I mean, I have a vector string, define vector tmpback , with info like this: name_lastname_phonenumber

I want to replace some last names. For example if someone is john_smith_5551234, I want to replace smith to smith100.

this is my code, o part of it:

vector<string> tmpback = names;
for (Int_t i = 0; i < tmpback.size(); i++) {
   replace(tmpback[i].begin(),tmpback[i].end(),"smith", "smith"+number);
}

(i defined number previously as Int_t number = 0 and give some values later). did someone have any idea of what am I doing wrong?

Thanks

3
  • 1
    replace(...,tmpback[i].end(),...); Commented Mar 12, 2012 at 3:15
  • Please show the definition of number, is it a std::string? Commented Mar 12, 2012 at 3:36
  • @Jesse I defined as Int_t number = 0 and I give some values later Commented Mar 12, 2012 at 3:42

2 Answers 2

1

std::replace does not replace sequences with other sequences. It replaces single elements with other single elements. Besides that, your method of appending a number to a string does not work.

Try boost::replace_first or boost::replace_all along with either boost::lexical_cast or std::to_string(c++11 only) for converting a number to a string.

using namespace boost;
std::string replace_str = std::string("smith") + lexical_cast<std::string>(number);
replace_first(tmpback[i], "smith", replace_str);

You could also search for the sub-string, and if you find it, insert the number (converted to a string) after it:

std::string::size_type pos = tmpback[i].find("smith");
if (pos != std::string::npos)
{
    // adding 5 because that's the length of "smith"
    tmpback[i].insert(pos + 5, std::to_string(number));
}
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your help @Benjamin, I got this error: Error: Symbol lexical_cast is not defined in current scope names.C:400: Error: Symbol string is not defined in current scope. The line 400 is your second line.
@Alejandro: lexical_cast is from boost, as the link I provided suggests. To use that method, you first need to install boost, and you need to include <boost/lexical_cast.hpp>. If you're impatient, there are other methods you can use to convert a number to a string available in the standard library. First try std::to_string, but like I said, that is only available in C++11 mode. If that doesn't work, then you'll have to resort to stringstreams, which are a bit cumbersome.
0

My immediate reaction would be to wonder why you're putting yourself in this situation at all. Instead of jamming three separate items into a string, then manipulating pieces of that string, why not create a struct so you can work with each piece separately?

struct person {
    std::string first_name;
    std::string last_name;
    int record_no;
    std::string phone_number;
};

This way, instead of tacking the record number (or whatever exactly your '100' represents) onto the end of the last name, you just give it its own field, and write an appropriate number as needed:

vector<person> tmpback;

for (int i=0; i<tmpback.size(); i++)
    tmpback[i].record_no = number;

1 Comment

thanks for the answer @Jerry. Actually I am only modifying someone's code. But, yes it sounds more reasonable. I will try it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.