finished stringview
This commit is contained in:
parent
ec50d811ff
commit
2da7623dd3
5 changed files with 40 additions and 32 deletions
|
@ -1,8 +1,6 @@
|
|||
#include <iostream>
|
||||
//#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;
|
||||
}
|
||||
|
|
|
@ -2,7 +2,8 @@
|
|||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#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)
|
||||
{
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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);
|
||||
|
||||
}
|
|
@ -2,7 +2,8 @@
|
|||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include "mipt.h"
|
||||
#include "miptstring.h"
|
||||
#include "miptstringview.h"
|
||||
using std::cout, std::cin, std::endl, std::size_t;
|
||||
|
||||
namespace mipt {
|
||||
|
|
28
seminar03_initialization/08_stringview/miptstringview.h
Normal file
28
seminar03_initialization/08_stringview/miptstringview.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
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);
|
||||
|
||||
}
|
Reference in a new issue