Skip to content

Instantly share code, notes, and snippets.

@k06a
Last active October 16, 2015 07:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k06a/d8ff5afcb8a6b4808db8 to your computer and use it in GitHub Desktop.
Save k06a/d8ff5afcb8a6b4808db8 to your computer and use it in GitHub Desktop.
std::string substring reference without copying
struct RefString
{
RefString(const string & s, int i, int l) : s(s), i(i), l(l) {}
const char & operator [] (int x) const {
return s[i+x];
}
size_t length() const {
return l;
}
bool operator < (const RefString & s2) const {
return s.compare(i, l, s2.s, s2.i, s2.l) < 0;
}
private:
const string & s;
int i;
int l;
};
ostream & operator << (ostream &stream, const RefString & ms) {
for (int i = 0; i < ms.length(); i++)
stream << ms[i];
return stream;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment