From 2da7623dd3e30f6ea6abb5da639fe9e74743f388 Mon Sep 17 00:00:00 2001 From: nihonium Date: Sat, 22 Oct 2022 22:03:40 +0300 Subject: [PATCH] finished stringview --- .../08_stringview/main.cpp | 10 +++---- .../08_stringview/miptstring.cpp | 7 +++-- .../08_stringview/{mipt.h => miptstring.h} | 24 +--------------- .../08_stringview/miptstringview.cpp | 3 +- .../08_stringview/miptstringview.h | 28 +++++++++++++++++++ 5 files changed, 40 insertions(+), 32 deletions(-) rename seminar03_initialization/08_stringview/{mipt.h => miptstring.h} (68%) create mode 100644 seminar03_initialization/08_stringview/miptstringview.h diff --git a/seminar03_initialization/08_stringview/main.cpp b/seminar03_initialization/08_stringview/main.cpp index cfec7e6..beaa948 100644 --- a/seminar03_initialization/08_stringview/main.cpp +++ b/seminar03_initialization/08_stringview/main.cpp @@ -1,8 +1,6 @@ #include -//#include "miptstring.cpp" -//#include "miptstringview.cpp" -#include "mipt.h" - +#include "miptstring.h" +#include "miptstringview.h" using namespace std; int main() { @@ -13,6 +11,8 @@ int main() { //cout << (b < a) << endl; //cout << (bv < av) << endl; cout << av.substr(1,10) << endl; - av.remove_suffix(5); + av.remove_suffix(2); cout << av << endl; + mipt::String meow = av; + cout << "sv to string: " << meow << endl; } diff --git a/seminar03_initialization/08_stringview/miptstring.cpp b/seminar03_initialization/08_stringview/miptstring.cpp index 19fa257..ef07c94 100644 --- a/seminar03_initialization/08_stringview/miptstring.cpp +++ b/seminar03_initialization/08_stringview/miptstring.cpp @@ -2,7 +2,8 @@ #include #include #include -#include "mipt.h" +#include "miptstring.h" +#include "miptstringview.h" using std::cout, std::cin, std::endl, std::size_t; namespace mipt{ @@ -29,12 +30,12 @@ String::String() : String("") {} String::String(const String& s) : String(s.cStr()) {} String::String(const StringView& sv) { mSize = sv.size(); - self.reserve(mSize); + (*this).reserve(mSize); for(int i = 0; i < mSize; ++i) mpData[i] = sv[i]; mpData[mSize] = '\0'; - } +} String::String(size_t n, char a) { diff --git a/seminar03_initialization/08_stringview/mipt.h b/seminar03_initialization/08_stringview/miptstring.h similarity index 68% rename from seminar03_initialization/08_stringview/mipt.h rename to seminar03_initialization/08_stringview/miptstring.h index 74c54fa..7a6d37f 100644 --- a/seminar03_initialization/08_stringview/mipt.h +++ b/seminar03_initialization/08_stringview/miptstring.h @@ -1,9 +1,9 @@ #pragma once - #include namespace mipt { +class StringView; class String { private: @@ -40,26 +40,4 @@ public: std::ostream& operator<<(std::ostream& out, const String& s); std::istream& operator>>(std::istream& in, String& s); - -class StringView -{ -private: - const char* mpData; - size_t mSize; -public: - StringView(); - StringView(const StringView& str); - StringView(const mipt::String& s); - StringView(const char* s); - const char& at(size_t i); - const char& operator[](size_t i) const; - bool operator<(const StringView& right) const; - size_t size() const; - StringView substr(size_t pos, size_t count); - void remove_prefix(size_t n); - void remove_suffix(size_t n); -}; - -std::ostream& operator<<(std::ostream& out, const StringView& sv); - } diff --git a/seminar03_initialization/08_stringview/miptstringview.cpp b/seminar03_initialization/08_stringview/miptstringview.cpp index 74892cb..9d903b3 100644 --- a/seminar03_initialization/08_stringview/miptstringview.cpp +++ b/seminar03_initialization/08_stringview/miptstringview.cpp @@ -2,7 +2,8 @@ #include #include #include -#include "mipt.h" +#include "miptstring.h" +#include "miptstringview.h" using std::cout, std::cin, std::endl, std::size_t; namespace mipt { diff --git a/seminar03_initialization/08_stringview/miptstringview.h b/seminar03_initialization/08_stringview/miptstringview.h new file mode 100644 index 0000000..3dfd7ac --- /dev/null +++ b/seminar03_initialization/08_stringview/miptstringview.h @@ -0,0 +1,28 @@ +#pragma once + +#include + +namespace mipt { +class String; +class StringView +{ +private: + const char* mpData; + size_t mSize; +public: + StringView(); + StringView(const StringView& str); + StringView(const mipt::String& s); + StringView(const char* s); + const char& at(size_t i); + const char& operator[](size_t i) const; + bool operator<(const StringView& right) const; + size_t size() const; + StringView substr(size_t pos, size_t count); + void remove_prefix(size_t n); + void remove_suffix(size_t n); +}; + +std::ostream& operator<<(std::ostream& out, const StringView& sv); + +}