You can use your own wrapper class, like this one:
struct RefString { RefString(const std::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 std::string & s; int i; int l; }; std::ostream & operator << (std::ostream &stream, const RefString & ms) { for (int i = 0; i < ms.length(); i++) stream << ms[i]; return stream; }
And use it like this, for example, to create a set unique substrings:
std::string s = "hello"; std::set<RefString> st; for (int i = 0; i < s.length(); i++) for (int j = i; j < s.length(); j++) st.insert(RefString(s, i, j-i+1));
source share