diff --git a/seminar03_initialization/08_stringview/main.cpp b/seminar03_initialization/08_stringview/main.cpp index b54382e..7ccf460 100644 --- a/seminar03_initialization/08_stringview/main.cpp +++ b/seminar03_initialization/08_stringview/main.cpp @@ -2,15 +2,17 @@ #include "miptstring.cpp" #include "miptstringview.cpp" + using namespace std; int main() { - mipt::String str = "Meow!!"; - cout << str << endl; - mipt::StringView sv = str; - cout << "string view: " << sv << endl; - char a[] = "nyaa!"; - mipt::StringView sv1 = a; - cout << "string view: " << sv1 << endl; - cout << sv1.at(200) << endl; + mipt::String a = "abcd"; + mipt::String b = "abce"; + mipt::StringView av = a; + mipt::StringView bv = b; + //cout << (b < a) << endl; + //cout << (bv < av) << endl; + cout << av.substr(1,10) << endl; + av.remove_suffix(5); + cout << av << endl; } diff --git a/seminar03_initialization/08_stringview/miptstring.cpp b/seminar03_initialization/08_stringview/miptstring.cpp index 4234748..1e7e84d 100644 --- a/seminar03_initialization/08_stringview/miptstring.cpp +++ b/seminar03_initialization/08_stringview/miptstring.cpp @@ -4,6 +4,8 @@ #include #include #include +//#include "miptstringview.cpp" + using std::cout, std::cin, std::endl, std::size_t; namespace mipt{ @@ -39,7 +41,15 @@ public: String() : String("") {} String(const String& s) : String(s.cStr()) {} - +/* String(const mipt::StringView& sv) { + mSize = sv.size(); + self.reserve(mSize); + for(int i = 0; i < mSize; ++i) + mpData[i] = sv[i]; + mpData[mSize] = '\0'; + + } +*/ String(size_t n, char a) { resize(n); @@ -218,4 +228,5 @@ std::istream& operator>>(std::istream& in, String& s) return in; } + } diff --git a/seminar03_initialization/08_stringview/miptstringview.cpp b/seminar03_initialization/08_stringview/miptstringview.cpp index 770aa16..5727b60 100644 --- a/seminar03_initialization/08_stringview/miptstringview.cpp +++ b/seminar03_initialization/08_stringview/miptstringview.cpp @@ -44,12 +44,39 @@ public: { return mpData[i]; } + bool operator<(const StringView& right) const + { + size_t i = 0; + while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i]) + i++; + + return mpData[i] < right.mpData[i]; + } size_t size() { return mSize; } + StringView substr(size_t pos, size_t count) { + if (pos > mSize) + throw std::out_of_range("Error! Index is out of bounds."); + if (pos + count > mSize) + count = mSize - pos; + StringView result; + result.mpData = mpData + pos; + result.mSize = count; + return result; + } + void remove_prefix(size_t n) { + mSize -= n; + } + void remove_suffix(size_t n) { + mSize -= n; + mpData += n; + } + +}; }; -} + std::ostream& operator<<(std::ostream& out, mipt::StringView sv) { size_t size = sv.size();