added double jump and sitting state
This commit is contained in:
parent
2e5c5a8dde
commit
90d07dde3f
148 changed files with 13050 additions and 0 deletions
|
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string letter_case_switch(const string & str)
|
||||
{
|
||||
string result = str;
|
||||
if (str.size() == 0)
|
||||
return result;
|
||||
result[0] =
|
||||
isupper(result[0]) ? tolower(result[0]) : toupper(result[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string meow;
|
||||
cin >> meow;
|
||||
cout << letter_case_switch(meow) << endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string letter_case_switch(const string& str) {
|
||||
string result = str;
|
||||
if (str.size() == 0)
|
||||
return result;
|
||||
result[0] = isupper(result[0]) ? tolower(result[0]) : toupper(result[0]);
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string meow;
|
||||
cin >> meow;
|
||||
cout << letter_case_switch(meow) << endl;
|
||||
}
|
||||
BIN
term1/seminar03_initialization/02_repeat/a.out~
Normal file
BIN
term1/seminar03_initialization/02_repeat/a.out~
Normal file
Binary file not shown.
48
term1/seminar03_initialization/02_repeat/main.cpp
Normal file
48
term1/seminar03_initialization/02_repeat/main.cpp
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string repeat1(string_view s)
|
||||
{
|
||||
return string {
|
||||
s}
|
||||
+string {
|
||||
s};
|
||||
}
|
||||
|
||||
void repeat2(string & s)
|
||||
{
|
||||
s += s;
|
||||
}
|
||||
|
||||
void repeat3(string * s)
|
||||
{
|
||||
*s += *s;
|
||||
}
|
||||
|
||||
string *repeat4(string_view s)
|
||||
{
|
||||
string *result = new string;
|
||||
*result = string {
|
||||
s} +string {
|
||||
s};
|
||||
return result;
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
string meow;
|
||||
cin >> meow;
|
||||
|
||||
cout << "test of repeat1:" << endl << repeat1(meow) << endl;
|
||||
|
||||
repeat2(meow);
|
||||
cout << "test of repeat2:" << endl << meow << endl;
|
||||
|
||||
repeat3(&meow);
|
||||
cout << "test of repeat3:" << endl << meow << endl;
|
||||
|
||||
cout << "test of repeat4:" << endl << *repeat4(meow) << endl;
|
||||
}
|
||||
38
term1/seminar03_initialization/02_repeat/main.cpp~
Normal file
38
term1/seminar03_initialization/02_repeat/main.cpp~
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string repeat1(string_view s) {
|
||||
return string{s} + string{s};
|
||||
}
|
||||
|
||||
void repeat2(string& s) {
|
||||
s += s;
|
||||
}
|
||||
|
||||
void repeat3(string* s) {
|
||||
*s += *s;
|
||||
}
|
||||
|
||||
string* repeat4(string_view s) {
|
||||
string* result = new string;
|
||||
*result = string{s} + string{s};
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string meow;
|
||||
cin >> meow;
|
||||
|
||||
cout << "test of repeat1:" << endl << repeat1(meow) << endl;
|
||||
|
||||
repeat2(meow);
|
||||
cout << "test of repeat2:" << endl << meow << endl;
|
||||
|
||||
repeat3(&meow);
|
||||
cout << "test of repeat3:" << endl << meow << endl;
|
||||
|
||||
cout << "test of repeat4:" << endl << *repeat4(meow) << endl;
|
||||
}
|
||||
|
|
@ -0,0 +1,24 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
string operator*(const string str, int n) {
|
||||
string result;
|
||||
for (int i = 0; i < n; ++i)
|
||||
result += str;
|
||||
return result;
|
||||
}
|
||||
|
||||
string operator*(int n, const string str) {
|
||||
string result;
|
||||
for (int i = 0; i < n; ++i)
|
||||
result += str;
|
||||
return result;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string meow;
|
||||
cin >> meow;
|
||||
cout << 3 * meow << endl;
|
||||
}
|
||||
22
term1/seminar03_initialization/04_truncate_to_dot/main.cpp
Normal file
22
term1/seminar03_initialization/04_truncate_to_dot/main.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void truncateToDot(string& s) {
|
||||
s.resize(s.find('.'));
|
||||
s.shrink_to_fit();
|
||||
}
|
||||
|
||||
int main() {
|
||||
std::string a = "cat.dog.mouse.elephant.tiger.lion";
|
||||
std::string b = "wikipedia.org";
|
||||
std::string c = ".com";
|
||||
truncateToDot(a);
|
||||
truncateToDot(b);
|
||||
truncateToDot(c);
|
||||
|
||||
cout << a << endl
|
||||
<< b << endl
|
||||
<< c << endl;
|
||||
}
|
||||
35
term1/seminar03_initialization/05_string_sum/main.cpp
Normal file
35
term1/seminar03_initialization/05_string_sum/main.cpp
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
#include <iostream>
|
||||
#include <cstdio>
|
||||
#include <string>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int string_sum(const string& str) {
|
||||
int res = 0;
|
||||
int x;
|
||||
int n = str.size();
|
||||
int i = 0;
|
||||
while (i < n) {
|
||||
switch (str[i]) {
|
||||
case '[':
|
||||
case ',' :
|
||||
sscanf((str.c_str()) + i + 1, "%d", &x);
|
||||
res += x;
|
||||
break;
|
||||
case ']' :
|
||||
return res;
|
||||
case ' ':
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
++i;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
string meow;
|
||||
getline(cin, meow);
|
||||
cout << string_sum(meow) << endl;
|
||||
}
|
||||
34
term1/seminar03_initialization/06_new/main.cpp
Normal file
34
term1/seminar03_initialization/06_new/main.cpp
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <iostream>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
int *x = new int{123};
|
||||
cout << *x << endl;
|
||||
|
||||
string *str = new string{"Cats and Dogs"};
|
||||
cout << *str << endl;
|
||||
|
||||
int *xs = new int[]{10, 20, 30, 40, 50};
|
||||
for (int i = 0; i < 5; ++i)
|
||||
cout << xs[i] << " ";
|
||||
cout << endl;
|
||||
|
||||
string *strs = new string[]{"Cat", "Dog", "Mouse"};
|
||||
for (int i = 0; i < 3; ++i)
|
||||
cout << strs[i] << " ";
|
||||
cout << endl;
|
||||
|
||||
string_view *str_views = new string_view[]{strs[0], strs[1], strs[2]};
|
||||
for (int i = 0; i < 3; ++i)
|
||||
cout << str_views[i] << " ";
|
||||
cout << endl;
|
||||
|
||||
delete x;
|
||||
delete str;
|
||||
delete[] xs;
|
||||
delete[] strs;
|
||||
delete[] str_views;
|
||||
}
|
||||
22
term1/seminar03_initialization/07_placement/main.cpp
Normal file
22
term1/seminar03_initialization/07_placement/main.cpp
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
#include <iostream>
|
||||
#include "miptstring.cpp"
|
||||
|
||||
using std::cout, std::endl;
|
||||
|
||||
int main() {
|
||||
mipt::String stack{"Cat"};
|
||||
cout << stack << endl;
|
||||
|
||||
mipt::String* heap = new mipt::String{"Dog"};
|
||||
cout << *heap << endl;
|
||||
|
||||
char *x = (char*)malloc(sizeof(mipt::String));
|
||||
mipt::String* px = new(x) mipt::String{"Elephant"};
|
||||
|
||||
cout << *px << endl;
|
||||
|
||||
px->~String();
|
||||
free(px);
|
||||
delete heap;
|
||||
|
||||
}
|
||||
215
term1/seminar03_initialization/07_placement/miptstring.cpp
Normal file
215
term1/seminar03_initialization/07_placement/miptstring.cpp
Normal file
|
|
@ -0,0 +1,215 @@
|
|||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <cstring>
|
||||
|
||||
namespace mipt{
|
||||
|
||||
class String
|
||||
{
|
||||
private:
|
||||
|
||||
std::size_t mSize {0};
|
||||
std::size_t mCapacity {0};
|
||||
char* mpData {nullptr};
|
||||
|
||||
public:
|
||||
|
||||
String(const char* str)
|
||||
{
|
||||
std::size_t strSize = std::strlen(str);
|
||||
resize(strSize);
|
||||
std::memcpy(mpData, str, mSize);
|
||||
|
||||
std::cout << "mipt::String Constructor (" << mpData << ")" << std::endl;
|
||||
}
|
||||
|
||||
String() : String("") {}
|
||||
String(const String& s) : String(s.cStr()) {}
|
||||
|
||||
String(std::size_t n, char a)
|
||||
{
|
||||
resize(n);
|
||||
|
||||
for (std::size_t i = 0; i < mSize; ++i)
|
||||
mpData[i] = a;
|
||||
|
||||
std::cout << "mipt::String Constructor (" << mpData << ")" << std::endl;
|
||||
}
|
||||
|
||||
~String()
|
||||
{
|
||||
std::cout << "mipt::String Destructor (" << mpData << ")" << std::endl;
|
||||
delete [] mpData;
|
||||
}
|
||||
|
||||
void reserve(std::size_t capacity)
|
||||
{
|
||||
if (capacity <= mCapacity)
|
||||
return;
|
||||
|
||||
mCapacity = std::max(2 * mCapacity, capacity);
|
||||
char* newData = new char[mCapacity]; // errorCheckedMalloc(mCapacity);
|
||||
|
||||
if (mpData)
|
||||
std::memcpy(newData, mpData, mSize + 1);
|
||||
|
||||
delete [] mpData;
|
||||
mpData = newData;
|
||||
}
|
||||
|
||||
|
||||
void resize(std::size_t size)
|
||||
{
|
||||
reserve(size + 1);
|
||||
mSize = size;
|
||||
mpData[mSize] = '\0';
|
||||
}
|
||||
|
||||
|
||||
String& operator=(const String& right)
|
||||
{
|
||||
if (this == &right)
|
||||
return *this;
|
||||
|
||||
mSize = right.mSize;
|
||||
resize(mSize);
|
||||
|
||||
std::memcpy(mpData, right.mpData, mSize + 1);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
String operator+(const String& b)
|
||||
{
|
||||
String result;
|
||||
result.resize(mSize + b.mSize);
|
||||
|
||||
std::memcpy(result.mpData, mpData, mSize);
|
||||
std::memcpy(result.mpData + mSize, b.mpData, b.mSize);
|
||||
result.mpData[result.mSize] = '\0';
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
String& operator+=(const String& right)
|
||||
{
|
||||
*this = *this + right;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool operator==(const String& right) const
|
||||
{
|
||||
if (mSize != right.mSize)
|
||||
return false;
|
||||
|
||||
std::size_t i = 0;
|
||||
while (i < mSize && mpData[i] == right.mpData[i])
|
||||
i++;
|
||||
|
||||
return i == mSize;
|
||||
}
|
||||
|
||||
bool operator<(const String& right) const
|
||||
{
|
||||
std::size_t i = 0;
|
||||
while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i])
|
||||
i++;
|
||||
|
||||
return mpData[i] < right.mpData[i];
|
||||
}
|
||||
|
||||
bool operator<=(const String& right) const
|
||||
{
|
||||
std::size_t i = 0;
|
||||
while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i])
|
||||
i++;
|
||||
|
||||
return mpData[i] <= right.mpData[i];
|
||||
}
|
||||
|
||||
bool operator!=(const String& right) const
|
||||
{
|
||||
return !(*this == right);
|
||||
}
|
||||
|
||||
bool operator>(const String& right) const
|
||||
{
|
||||
return !(*this <= right);
|
||||
}
|
||||
|
||||
bool operator>=(const String& right) const
|
||||
{
|
||||
return !(*this < right);
|
||||
}
|
||||
|
||||
char& operator[](std::size_t i)
|
||||
{
|
||||
return mpData[i];
|
||||
}
|
||||
|
||||
const char& operator[](std::size_t i) const
|
||||
{
|
||||
return mpData[i];
|
||||
}
|
||||
|
||||
char& at(std::size_t i)
|
||||
{
|
||||
if (i >= mSize)
|
||||
throw std::out_of_range{"mipt::String::at: index >= this->size()"};
|
||||
return mpData[i];
|
||||
}
|
||||
|
||||
const char& at(std::size_t i) const
|
||||
{
|
||||
if (i >= mSize)
|
||||
throw std::out_of_range{"mipt::String::at: index >= this->size()"};
|
||||
return mpData[i];
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
delete [] mpData;
|
||||
|
||||
mSize = 0;
|
||||
mCapacity = 1;
|
||||
mpData = new char[mCapacity];
|
||||
mpData[0] = '\0';
|
||||
}
|
||||
|
||||
void addCharacter(char c)
|
||||
{
|
||||
if (mSize + 1 == mCapacity)
|
||||
reserve(2 * mCapacity);
|
||||
|
||||
mpData[mSize] = c;
|
||||
resize(mSize + 1);
|
||||
}
|
||||
|
||||
|
||||
std::size_t getSize() const {return mSize;}
|
||||
std::size_t getCapacity() const {return mCapacity;}
|
||||
const char* cStr() const {return mpData;}
|
||||
};
|
||||
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const String& s)
|
||||
{
|
||||
out << s.cStr();
|
||||
return out;
|
||||
}
|
||||
|
||||
std::istream& operator>>(std::istream& in, String& s)
|
||||
{
|
||||
s.clear();
|
||||
while (true)
|
||||
{
|
||||
char x = in.get();
|
||||
if (std::isspace(x))
|
||||
break;
|
||||
s.addCharacter(x);
|
||||
}
|
||||
return in;
|
||||
}
|
||||
}
|
||||
18
term1/seminar03_initialization/08_stringview/main.cpp
Normal file
18
term1/seminar03_initialization/08_stringview/main.cpp
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
#include <iostream>
|
||||
#include "miptstring.h"
|
||||
#include "miptstringview.h"
|
||||
using namespace std;
|
||||
|
||||
int main() {
|
||||
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(2);
|
||||
cout << av << endl;
|
||||
mipt::String meow = av;
|
||||
cout << "sv to string: " << meow << endl;
|
||||
}
|
||||
218
term1/seminar03_initialization/08_stringview/miptstring.cpp
Normal file
218
term1/seminar03_initialization/08_stringview/miptstring.cpp
Normal file
|
|
@ -0,0 +1,218 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include "miptstring.h"
|
||||
#include "miptstringview.h"
|
||||
using std::cout, std::cin, std::endl, std::size_t;
|
||||
|
||||
namespace mipt{
|
||||
|
||||
char* errorCheckedMalloc(size_t newCapacity)
|
||||
{
|
||||
char* result = static_cast<char*>(std::malloc(newCapacity * sizeof(char)));
|
||||
if (result == NULL)
|
||||
{
|
||||
cout << "Error! Out of memory" << endl;
|
||||
std::exit(1);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
String::String(const char* str)
|
||||
{
|
||||
size_t strSize = std::strlen(str);
|
||||
resize(strSize);
|
||||
std::memcpy(mpData, str, mSize);
|
||||
}
|
||||
|
||||
String::String() : String("") {}
|
||||
String::String(const String& s) : String(s.cStr()) {}
|
||||
String::String(const StringView& sv) {
|
||||
mSize = sv.size();
|
||||
(*this).reserve(mSize);
|
||||
for(int i = 0; i < mSize; ++i)
|
||||
mpData[i] = sv[i];
|
||||
mpData[mSize] = '\0';
|
||||
|
||||
}
|
||||
|
||||
String::String(size_t n, char a)
|
||||
{
|
||||
resize(n);
|
||||
|
||||
for (size_t i = 0; i < mSize; ++i)
|
||||
mpData[i] = a;
|
||||
}
|
||||
|
||||
String::~String()
|
||||
{
|
||||
std::free(mpData);
|
||||
}
|
||||
|
||||
void String::reserve(size_t capacity)
|
||||
{
|
||||
if (capacity <= mCapacity)
|
||||
return;
|
||||
|
||||
mCapacity = std::max(2 * mCapacity, capacity);
|
||||
char* newData = errorCheckedMalloc(mCapacity);
|
||||
|
||||
if (mpData)
|
||||
std::memcpy(newData, mpData, mSize + 1);
|
||||
|
||||
std::free(mpData);
|
||||
mpData = newData;
|
||||
}
|
||||
|
||||
|
||||
void String::resize(size_t size)
|
||||
{
|
||||
reserve(size + 1);
|
||||
mSize = size;
|
||||
mpData[mSize] = '\0';
|
||||
}
|
||||
|
||||
|
||||
String& String::operator=(const String& right)
|
||||
{
|
||||
if (this == &right)
|
||||
return *this;
|
||||
|
||||
mSize = right.mSize;
|
||||
resize(mSize);
|
||||
|
||||
std::memcpy(mpData, right.mpData, mSize + 1);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
String String::operator+(const String& b)
|
||||
{
|
||||
String result;
|
||||
result.resize(mSize + b.mSize);
|
||||
|
||||
std::memcpy(result.mpData, mpData, mSize);
|
||||
std::memcpy(result.mpData + mSize, b.mpData, b.mSize);
|
||||
result.mpData[result.mSize] = '\0';
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
String& String::operator+=(const String& right)
|
||||
{
|
||||
*this = *this + right;
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool String::operator==(const String& right) const
|
||||
{
|
||||
if (mSize != right.mSize)
|
||||
return false;
|
||||
|
||||
size_t i = 0;
|
||||
while (i < mSize && mpData[i] == right.mpData[i])
|
||||
i++;
|
||||
|
||||
return i == mSize;
|
||||
}
|
||||
|
||||
bool String::operator<(const String& right) const
|
||||
{
|
||||
size_t i = 0;
|
||||
while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i])
|
||||
i++;
|
||||
|
||||
return mpData[i] < right.mpData[i];
|
||||
}
|
||||
|
||||
bool String::operator<=(const String& right) const
|
||||
{
|
||||
size_t i = 0;
|
||||
while (i < mSize && i < right.mSize && mpData[i] == right.mpData[i])
|
||||
i++;
|
||||
|
||||
return mpData[i] <= right.mpData[i];
|
||||
}
|
||||
|
||||
bool String::operator!=(const String& right) const
|
||||
{
|
||||
return !(*this == right);
|
||||
}
|
||||
|
||||
bool String::operator>(const String& right) const
|
||||
{
|
||||
return !(*this <= right);
|
||||
}
|
||||
|
||||
bool String::operator>=(const String& right) const
|
||||
{
|
||||
return !(*this < right);
|
||||
}
|
||||
|
||||
char& String::operator[](size_t i)
|
||||
{
|
||||
return mpData[i];
|
||||
}
|
||||
|
||||
const char& String::operator[](size_t i) const
|
||||
{
|
||||
return mpData[i];
|
||||
}
|
||||
|
||||
char& String::at(size_t i)
|
||||
{
|
||||
if (i >= mSize)
|
||||
{
|
||||
cout << "Error! Index is out of bounds." << endl;
|
||||
}
|
||||
return mpData[i];
|
||||
}
|
||||
|
||||
void String::clear()
|
||||
{
|
||||
std::free(mpData);
|
||||
|
||||
mSize = 0;
|
||||
mCapacity = 1;
|
||||
mpData = errorCheckedMalloc(mCapacity);
|
||||
mpData[0] = '\0';
|
||||
}
|
||||
|
||||
void String::addCharacter(char c)
|
||||
{
|
||||
if (mSize + 1 == mCapacity)
|
||||
reserve(2 * mCapacity);
|
||||
|
||||
mpData[mSize] = c;
|
||||
resize(mSize + 1);
|
||||
}
|
||||
|
||||
|
||||
size_t String::getSize() const {return mSize;}
|
||||
size_t String::getCapacity() const {return mCapacity;}
|
||||
const char* String::cStr() const {return mpData;}
|
||||
|
||||
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const String& s)
|
||||
{
|
||||
out << s.cStr();
|
||||
return out;
|
||||
}
|
||||
|
||||
std::istream& operator>>(std::istream& in, String& s)
|
||||
{
|
||||
s.clear();
|
||||
while (true)
|
||||
{
|
||||
char x = in.get();
|
||||
if (x == ' ' || x == '\n' || x == '\t')
|
||||
break;
|
||||
s.addCharacter(x);
|
||||
}
|
||||
return in;
|
||||
}
|
||||
|
||||
}
|
||||
43
term1/seminar03_initialization/08_stringview/miptstring.h
Normal file
43
term1/seminar03_initialization/08_stringview/miptstring.h
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
#pragma once
|
||||
#include <iostream>
|
||||
|
||||
namespace mipt {
|
||||
|
||||
class StringView;
|
||||
class String
|
||||
{
|
||||
private:
|
||||
size_t mSize {0};
|
||||
size_t mCapacity {0};
|
||||
char* mpData {nullptr};
|
||||
public:
|
||||
String(const char* str);
|
||||
String();
|
||||
String(const String& s);
|
||||
String(const mipt::StringView& sv);
|
||||
String(size_t n, char a);
|
||||
~String();
|
||||
void reserve(size_t capacity);
|
||||
void resize(size_t size);
|
||||
String& operator=(const String& right);
|
||||
String operator+(const String& b);
|
||||
String& operator+=(const String& right);
|
||||
bool operator==(const String& right) const;
|
||||
bool operator<(const String& right) const;
|
||||
bool operator<=(const String& right) const;
|
||||
bool operator!=(const String& right) const;
|
||||
bool operator>(const String& right) const;
|
||||
bool operator>=(const String& right) const;
|
||||
char& operator[](size_t i);
|
||||
const char& operator[](size_t i) const;
|
||||
char& at(size_t i);
|
||||
void clear();
|
||||
void addCharacter(char c);
|
||||
size_t getSize() const;
|
||||
size_t getCapacity() const;
|
||||
const char* cStr() const;
|
||||
};
|
||||
|
||||
std::ostream& operator<<(std::ostream& out, const String& s);
|
||||
std::istream& operator>>(std::istream& in, String& s);
|
||||
}
|
||||
|
|
@ -0,0 +1,81 @@
|
|||
#include <iostream>
|
||||
#include <algorithm>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include "miptstring.h"
|
||||
#include "miptstringview.h"
|
||||
using std::cout, std::cin, std::endl, std::size_t;
|
||||
|
||||
namespace mipt {
|
||||
|
||||
StringView::StringView() {;
|
||||
mSize = 0;
|
||||
mpData = nullptr;
|
||||
}
|
||||
StringView::StringView(const StringView& str) {
|
||||
mSize = str.mSize;
|
||||
mpData = str.mpData;
|
||||
}
|
||||
StringView::StringView(const mipt::String& s) {
|
||||
mSize = s.getSize();
|
||||
mpData = s.cStr();
|
||||
}
|
||||
StringView::StringView(const char* s) {
|
||||
mpData = s;
|
||||
mSize = strlen(s);
|
||||
}
|
||||
const char& StringView::at(size_t i)
|
||||
{
|
||||
if (i >= mSize)
|
||||
{
|
||||
throw std::out_of_range("Error! Index is out of bounds.");
|
||||
std::exit(1);
|
||||
}
|
||||
return mpData[i];
|
||||
}
|
||||
const char& StringView::operator[](size_t i) const
|
||||
{
|
||||
return mpData[i];
|
||||
}
|
||||
bool StringView::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 StringView::size() const {
|
||||
return mSize;
|
||||
}
|
||||
StringView 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 StringView::remove_prefix(size_t n) {
|
||||
mSize -= n;
|
||||
}
|
||||
void StringView::remove_suffix(size_t n) {
|
||||
mSize -= n;
|
||||
mpData += n;
|
||||
}
|
||||
/*std::ostream& StringView::operator<<(std::ostream& out, mipt::StringView sv) {
|
||||
size_t size = sv.size();
|
||||
for (int i = 0; i < size; ++i)
|
||||
out << sv[i];
|
||||
return out;
|
||||
}*/
|
||||
std::ostream& operator<<(std::ostream& out, const mipt::StringView& sv) {
|
||||
size_t size = sv.size();
|
||||
for (int i = 0; i < size; ++i)
|
||||
out << sv[i];
|
||||
return out;
|
||||
}
|
||||
};
|
||||
|
||||
|
|
@ -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);
|
||||
|
||||
}
|
||||
BIN
term1/seminar03_initialization/homework_initialization.pdf
Normal file
BIN
term1/seminar03_initialization/homework_initialization.pdf
Normal file
Binary file not shown.
Reference in a new issue