...
Code Block |
---|
|
#include <string>
void f(const std::string &input) {
std::string email;
std::string::iterator loc = email.begin();
// Copy input into email converting ";" to " "
std::string::iterator loc = email.begin();
for (auto I = input.begin(), E = input.end(); I != E; ++I, ++loc) {
email.insert(loc, *I != ';' ? *I : ' ');
}
} |
...
Code Block |
---|
|
#include <string>
void f(const std::string &input) {
std::string email;
std::string::iterator loc = email.begin();
// Copy input into email converting ";" to " "
std::string::iterator loc = email.begin();
for (auto I = input.begin(), E = input.end(); I != E; ++I, ++loc) {
loc = email.insert(loc, *I != ';' ? *I : ' ');
}
}
|
Compliant Solution (std::replace()
)
In this This compliant solution , the manual loop is replaced with uses a standard algorithm that performs to perform the replacement. Using generic algorithms is generally When possible, using a generic algorithm is preferable to inventing your own solution when possible.
Code Block |
---|
|
#include <algorithm>
#include <string>
void f(const std::string &input) {
std::string email{input};
std::replace(email.begin(), email.end(), ';', ' ');
} |
...