#std::string

20 messages · Page 1 of 1 (latest)

normal mountain
#

Hey, I am a beginner in C++ and I don't really understand how to do my "homework". I'm asked to recreate std::string size, length, resize ect. Thing is that, I don't really understand how to do it. I'm aware that CPP reference explain how the func works, but I still don't understand how to do it by myself.

static trellisBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

#

@normal mountain

Screenshots!

Your message appears to contain screenshots but no code. Please send code and error messages in text instead of screenshots if applicable!

normal agate
#

@normal mountain you have a char array, right?

#

if you have a char array you can to calc the length/size of the string just go through every char and add 1 to an int per char.

normal mountain
#

sorry for the late answer, I finished my code

#include "ministring.h"

Ministring::Ministring() {}

int Ministring::size()const
{
    return m_length;
}

int Ministring::length() const
{
    return m_length;
}

void Ministring::resize (int n){
    if (n < m_length){
        m_length = n;
        if (n>=0){
            m_str[n] = '\0';
        }
    }else if (n > m_length){
        char *new_str = new char[n+1];
        if (m_str != nullptr) {
            for (int i = 0; i < m_length; ++i) {
                new_str[i] = m_str[i];
            }
            delete[] m_str;
        }
        m_str = new_str;
        m_length = n;
    }
}
#
#ifndef MINISTRING_MINISTRING_H
#define MINISTRING_MINISTRING_H

using namespace std;

#include <iostream>

class Ministring {

public:
    Ministring();
    int size() const;
    int length() const;
    void resize (int n);

private:
    char *m_str;
    int m_length;
};


#endif //MINISTRING_MINISTRING_H
normal mountain
normal agate
#

ok, what functions do you need to implement

normal mountain
#

I've to recreate all those =>

size

  • length
  • resize
  • clear
  • empty
  • at
  • insert
  • replace
  • find
  • substr

from the std::string

#

basically i've done

#
using namespace std;

#include <iostream>

class Ministring {

public:
    Ministring();
    int size() const;
    int length() const;
    void resize (int n);
    void clear();
    bool empty() const;

private:
    char *m_str;
    int m_length;
};
#
#include "ministring.h"

Ministring::Ministring() {}

int Ministring::size()const
{
    return m_length;
}

int Ministring::length() const
{
    return m_length;
}

void Ministring::resize (int n){
    if (n < m_length){
        m_length = n;
        if (n>=0){
            m_str[n] = '\0';
        }
    }else if (n > m_length){
        char *new_str = new char[n+1];
        if (m_str != nullptr) {
            for (int i = 0; i < m_length; ++i) {
                new_str[i] = m_str[i];
            }
            delete[] m_str;
        }
        m_str = new_str;
        m_length = n;
    }
}

void Ministring::clear(){
    if (m_length > 0){
        delete[] m_str;
        m_str = nullptr;
        m_length = 0;
    }
}

bool Ministring::empty() const
{
    if (m_length == 0){
        return true;
    }
    return false;
}
#

But for empty, can't I just do "return (m_length = 0)" ?

normal agate
#

replace is pretty essy, just go through the array like in length and replace any chars that are like the input with some other things

#

i think just return (size() == 0), yeah

#

find is also like replace and length, just go through the array and if you find the char return the index i think

normal mountain
#

Alright, imma try. thaanks for the advices

normal agate
#

yeah, the problem is i am not that good myself with char* and char[]

normal mountain
#

Oh, don't worry, you tried and i'm good with that