M
massysett
Hello,
I've got a couple of simple functions to examine a vector of strings,
removing from the vector strings that do not begin with a particular
string. They compile just fine when they look like this:
#include <algorithm>
#include <functional>
bool startswith(std::string subject, std::string test)
{
return (subject.substr(0, test.length()) == test);
}
std::vector<std::string> matchAbbrev(std::vector<std::string> strings,
const std::string& abbrev)
{
// isolate all words with abbrev as a prefix
// Takes the list of all strings and removes strings that do NOT
// start with abbrev.
std::vector<std::string>::iterator it =
std::remove_if(strings.begin(), strings.end(),
std::not1(std::bind2nd(std:tr_fun(startswith), abbrev)));
strings.erase(it, strings.end());
return strings;
}
However, if I change the parameter list of the first function so it
looks like this:
bool startswith(std::string& subject, std::string& test)
(note the addition of the references) it won't compile and I get a
slew of errors. Why? Maybe I could work around this with pointers,
which would not be particularly pretty...
I've got a couple of simple functions to examine a vector of strings,
removing from the vector strings that do not begin with a particular
string. They compile just fine when they look like this:
#include <algorithm>
#include <functional>
bool startswith(std::string subject, std::string test)
{
return (subject.substr(0, test.length()) == test);
}
std::vector<std::string> matchAbbrev(std::vector<std::string> strings,
const std::string& abbrev)
{
// isolate all words with abbrev as a prefix
// Takes the list of all strings and removes strings that do NOT
// start with abbrev.
std::vector<std::string>::iterator it =
std::remove_if(strings.begin(), strings.end(),
std::not1(std::bind2nd(std:tr_fun(startswith), abbrev)));
strings.erase(it, strings.end());
return strings;
}
However, if I change the parameter list of the first function so it
looks like this:
bool startswith(std::string& subject, std::string& test)
(note the addition of the references) it won't compile and I get a
slew of errors. Why? Maybe I could work around this with pointers,
which would not be particularly pretty...