Last active
October 16, 2015 07:17
-
-
Save k06a/d8ff5afcb8a6b4808db8 to your computer and use it in GitHub Desktop.
std::string substring reference without copying
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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