Skip to content

fix_string#2

Open
sikalovaliza wants to merge 5 commits into
mainfrom
fix_task
Open

fix_string#2
sikalovaliza wants to merge 5 commits into
mainfrom
fix_task

Conversation

@sikalovaliza

Copy link
Copy Markdown
Owner

No description provided.

Comment thread string.h Outdated
memset(str, c, size_of_string);
}

String();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

= default?

Comment thread string.h Outdated

String(std::initializer_list<char> lst) {
size_of_string = lst.size();
capacity = 2 * size_of_string;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Битовый сдвиг пожалуйста

Comment thread string.h Outdated

String& operator+=(const String& rhs) {
while (size_of_string + rhs.size_of_string >= capacity) {
str = (char*)realloc(str, 2 * size_of_string);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Аллокация от capacity

Comment thread string.h Outdated
friend std::istream& operator>>(std::istream &in, String &String);

String substr(size_t start, size_t count) {
String new_string;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Не поправила по комменту из предыдущего pull request'а

Comment thread string.h Outdated

void push_back(char add) {
if (size_of_string + 1 == capacity) {
str = (char*)realloc(str, size_of_string * 2);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Аллокация от capacity

Comment thread string.h Outdated
void push_back(char add) {
if (size_of_string + 1 == capacity) {
str = (char*)realloc(str, size_of_string * 2);
capacity *= 2;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Битовый сдвиг

Comment thread string.h Outdated
}
void clear() {
size_of_string = 0;
str = nullptr;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Память течет. Зачем зануляешь указатель? Лучше str[0] = '\0'

Comment thread string.h Outdated

void Shrink_to_fit() {
capacity = size_of_string + 1;
str = (char*)realloc(str, size_of_string);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Аллокация от capacity

Comment thread string.h Outdated
return new_string;
}

size_t find(String substring) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Я бы переписал здесь в двойной цикл. Как будто должно работать, но выглядит очень непрозрачно. Я бы точно потестил на разных входах. (Попробуй в том числе пустые строки и другие краевые случаи).

исправила операторы, изменила везде "*2"
Comment thread string.cpp
friend std::ostream& operator<<(std::ostream &out, const String &String);
friend std::istream& operator>>(std::istream &in, String &String);

String& substr(size_t start, size_t count) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ты возвращаешь ссылку на объект, время жизни которого привязано к блоку функции. Так делать нельзя, так как то что ты вернешь уже перестанет существовать. Думаю здесь нужно возвращать не ссылочный тип, а обычный String.

Comment thread string.cpp
return new_string;
}

size_t find(String& substring) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Метод find должен быть по смыслу константный. Ты ведь не собираешься в нем менять свою строку. Также и аргумент нужно пометить константным const String& substring.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кроме того, у тебя не сможет создаться временный объект, если ты не пометишь этот аргумент как константный.
То есть, когда ты вызываешь

str.find("44");

У тебя создается временный объект String, который существует до конца выполнения функции (или говорят еще фрейма).

Comment thread string.cpp
return size_of_string;
}

size_t rfind(String& substring) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Здесь те же замечания, что и про find.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants