// 1
m_pStr; _Other.m_pStr;
// 2
m_MaxLength; _Other.m_MaxLength;
// 3
m_Length; _Other.m_Length;
==
비교할떄 문자열의 주소비교는 필요가 없다.bool Str::operator==(const Str& _Other) const
{
// 1
if (m_Length != _Other.m_Length)
{
return false;
}
// 2
for (int i = 0; i < m_Length; ++i)
{
// 3
if (m_pStr[i] != _Other.m_pStr[i])
{
return false;
}
}
// 4
return true;
}
bool Str::operator!=(const Str& _Other)const
{
return !((*this) == _Other);
}
==
)를 받아서 거꾸로 주면 된다.aa << ab
aa`x`(없다) < aab
aa > x(없다)
bool Str::operator<(const Str& _Other)const
{
// 1
assert(m_Length || _Other.m_Length);
// 2
int MinLen = m_Length < _Other.m_Length ? m_Length : _Other.m_Length;
// 3
for (int i = 0; i < MinLen; ++i)
{
if (m_pStr[i] < _Other.m_pStr[i])
return true;
else if (m_pStr[i] > _Other.m_pStr[i])
return false;
}
// 4
if (m_Length < _Other.m_Length)
return true;
else
// 5
return false;
}
<
기 떄문에 false다.>
구현bool Str::operator>(const Str& _Other)const
{
assert(m_Length || _Other.m_Length);
int MinLen = m_Length < _Other.m_Length ? m_Length : _Other.m_Length;
for (int i = 0; i < MinLen; ++i)
{
if (m_pStr[i] > _Other.m_pStr[i])
return true;
else if (m_pStr[i] < _Other.m_pStr[i])
return false;
}
if (m_Length > _Other.m_Length)
return true;
else
return false;
}
<
의 결과를 뒤집으면 안되고 케이스들을 반대로 돌려야된다.==
가 섞여 있기 떄문이다.<
과 >
테스트 Str str1(L"Father");
// 2 // 6
Str str2(L"Mother");
// 3 // 7
Str str2(L"Father");
// 4 // 8
Str str2(L"Fatherr");
// 1
bool b = str1 < str2;
// 5
bool b = str1 < str2;
<=
구현bool Str::operator<=(const Str& _Other)const
{
// 1
if (!(m_Length || _Other.m_Length))
return true;
int MinLen = m_Length < _Other.m_Length ? m_Length : _Other.m_Length;
// 2
for (int i = 0; i </*2<=*/ MinLen; ++i)
{
if (m_pStr[i] < _Other.m_pStr[i])
return true;
else if (m_pStr[i] > _Other.m_pStr[i])
return false;
}
// 3 // 4
if (m_Length < _Other.m_Length)
return true;
else if (m_Length > _Other.m_Length)
return false;
// 5
else
return true;
// 6
if (m_Length <= _Other.m_Length)
return true;
else
return false;
}
!
해서 true로 바꿔주면 그 경우만 조건문이 true를 반환하게한다.abc
랑 abd
를 비교할떄는 a
랑 a
를 첫번쨰에 비교를 해버리는 순간 두 문자열은 같지않은데 true가 떠버려서 쓰면 안된다.>=
구현bool Str::operator>=(const Str& _Other)const
{
if (!(m_Length || _Other.m_Length))
return true;
int MinLen = m_Length < _Other.m_Length ? m_Length : _Other.m_Length;
for (int i = 0; i < MinLen; ++i)
{
if (m_pStr[i] > _Other.m_pStr[i])
return true;
else if (m_pStr[i] < _Other.m_pStr[i])
return false;
}
if (m_Length >= _Other.m_Length)
return true;
else
return false;
}
<=
대소비교 뒤집어서 구현하면 끝.<=
과 >=
테스트 Str str1(L"Father");
Str str2(L"Father");
bool b = str1 <= str2;
bool b = str1 >= str2;
// 1
BST<Str, int> mybst;
// 2
Str mystr(L"Test");
// 3
mybst.insert(make_bstpair(mystr, 0));
// 4
mybst.insert(make_bstpair(L"Test", 0));
BSTPair<T1, T2> make_bstpair(const T1& _first, const T2& _second)
void BST<T1, T2>::insert(const BSTPair<T1, T2>& _pair)
make_bstpair(L"Test", 0);
make_bstpair<Str, int>(L"Test", 0);
mybst.insert(make_bstpair(L"Test", 0));
mybst.insert(make_bstpair<Str, int>(L"Test", 0));
template<typename PAIR>
void insert(const PAIR& _pair);
///template<typename T1, typename T2>
template<typename PAIR>
//void BST<T1, T2>::insert(const BSTPair<T1, T2>& _pair)
void BST<T1, T2>::insert(const PAIR& _pair)
{
// BST<wchar_t[5], int> == PAIR;
// 1
//BSTNode<T1, T2>* pNewNode = new BSTNode<T1, T2>(_pair);
// 2
BSTNode<T1, T2>* pNewNode = new BSTNode<T1, T2>(_pair.first, _pair.second);
// 아래 구현 부분 생략.
}
BSTNode(const T1* _first, cosnt T2& _second, BSTNode* _Parent = nullptr, BSTNode* _LeftChild = nullptr, BSTNode* _RightChild = nullptr)
: pair{ _first._second }
, pParent(_Parent)
, pLeftChild(_LeftChild)
, pRightChild(_RightChild)
{}
// BST<wchar_t[5], int> == PAIR;
BSTPair<wchar_t[5], int> pair;
Str s = pair.first;
wchar_t arr[5] = {};
wchar_t arr2[5] = {};
arr2 = arr;
string;
wstring
#include <iostream>
#include "BST.h"
#include "Str.h"
#include <map>
using std::map;
using std::make_pair;
#include <string.h>
int main()
{
// 문자열 키값 사용
// 문자열의 타입은 const char* 타입
const wchar_t* pChar = L"asdasd";
map<const wchar_t*, int> mapData;
mapData.insert(make_pair(L"Father", 0));
mapData.insert(make_pair(L"Mother", 1));
mapData.insert(make_pair(L"Brother", 2));
mapData.insert(make_pair(L"Sister", 3));
map<const wchar_t*, int>::iterator iter = mapData.find(L"Father");
iter->first;
iter->second;
wchar_t szName[20] = L"Father";
mapData.find(szName);
map<Str, int> mapString;
mapString.insert(make_pair(L"Father", 0));
mapString.insert(make_pair(L"Mother", 1));
mapString.insert(make_pair(L"Brother", 2));
mapString.insert(make_pair(L"Sister", 3));
Str str1(L"Father");
Str str2(L"Father");
bool b = str1 < str2;
// 이진탐색트리의 키값을 Str 로 지정
BST<Str, int> mybst;
Str mystr(L"Test");
//BSTPair<Str, int> pair;
//mybst.insert();
//make_bstpair(L"Test", 0);
BSTPair<wchar_t[5], int> pair;
Str s = pair.first;
// map
std::string;
std::wstring;
return 0;
}
#pragma once
class Str
{
private:
wchar_t* m_pStr; // 동적할당한 문자열의 주소
int m_MaxLength; // 문자열을 저장할 수 있는 최대 길이
int m_Length; // 문자열의 길이
public:
int Length() { return m_Length; }
const wchar_t* GetStr() { return m_pStr; }
private:
void Realloc();
// 연산자 오버로딩
public:
void operator = (const wchar_t* _str);
template<int Size>
void operator =(const wchar_t(&_buffer)[Size])
{
(*this) = _buffer;
}
Str operator+ (const Str& _string);
Str operator+ (const char* _Str);
void operator +=(const wchar_t* _Str);
bool operator ==(const Str& _Other) const;
bool operator !=(const Str& _Other) const;
bool operator <(const Str& _Other) const;
bool operator >(const Str& _Other) const;
bool operator <=(const Str& _Other) const;
bool operator >=(const Str& _Other) const;
public:
Str();
Str(const wchar_t* _str);
template<int Size>
Str(const wchar_t(&_buffer)[Size])
: m_pStr(nullptr)
, m_MaxLength(10)
, m_Length(0)
{
m_pStr = new wchar_t[m_MaxLength + 1];
(*this) = _buffer;
}
~Str();
};
#include "Str.h"
#include <iostream>
#include <assert.h>
Str::Str()
: m_pStr(nullptr)
, m_MaxLength(10)
, m_Length(0)
{
m_pStr = new wchar_t[m_MaxLength + 1];
}
Str::Str(const wchar_t* _str)
: m_pStr(nullptr)
, m_MaxLength(10)
, m_Length(0)
{
m_pStr = new wchar_t[m_MaxLength + 1];
(*this) = _str;
}
Str::~Str()
{
if (nullptr != m_pStr)
delete m_pStr;
}
void Str::Realloc()
{
// 수용 공간을 2배로 확장하기
m_MaxLength *= 2;
// 새로운 공간을 만들어낸다.
wchar_t* pNew = new wchar_t[m_MaxLength + 1];
// 원래 있던 데이터를 새로운곳으로 옮긴다.
for (int i = 0; i < m_Length; ++i)
{
pNew[i] = m_pStr[i];
}
pNew[m_Length] = '\0';
// 기존 공간을 해제한다.
delete m_pStr;
// 새로운 공간을 가리킨다.
m_pStr = pNew;
}
void Str::operator=(const wchar_t* _str)
{
// 입력되려는 문자열의 문자 개수(길이) 파악
int len = 0;
while ('\0' != _str[len]) { ++len; }
// 입력되려는 문자열의 길이가 최대 수용개수를 넘어서면 저장 공간 확장
while (m_MaxLength < len)
{
Realloc();
}
// 입력 문자열의 값을, 힙 공간으로 하나씩 옮기기
int i = 0;
for (; i < len; ++i)
{
m_pStr[i] = _str[i];
}
// 마지막에 널문자로 막기
m_pStr[i] = '\0';
// 문자열 길이 갱신(입력된 문자열 길이로)
m_Length = len;
}
Str Str::operator+(const Str& _string)
{
Str strNew;
strNew = m_pStr;
strNew += _string.m_pStr;
return strNew;
}
Str Str::operator+(const char* _Str)
{
Str strNew;
return strNew;
}
void Str::operator+=(const wchar_t* _Str)
{
// 뒤에 붙을 문자열의 문자 개수(길이) 파악
int len = 0;
while ('\0' != _Str[len]) { ++len; }
// 원래 문자열 길이 + 새로 뒤에 붙을 문자열의 길이가 최대 저장 크기를 벗어나는지 확인
while (m_MaxLength < m_Length + len)
{
Realloc();
}
// 뒤에붙을 문자열을 가져오기
for (int i = 0; i < len; ++i)
{
m_pStr[i + m_Length] = _Str[i];
}
// 저장하고 있는 문자열 길이 갱신
m_Length += len;
m_pStr[m_Length] = '\0';
}
bool Str::operator==(const Str& _Other) const
{
// 각 객체의 문자열의 길이가 동일하지 않으면 같지 않음
if (m_Length != _Other.m_Length)
return false;
// 모든 문자가 같은지 비교
for (int i = 0; i < m_Length; ++i)
{
// 중간에 하나라도 다르면 두 문자열은 같지 않음
if (m_pStr[i] != _Other.m_pStr[i])
{
return false;
}
}
// 문자열의 길이가 같고, 모든 문자가 동일하면 두 문자열은 같은 문자열이다.
return true;
}
bool Str::operator!=(const Str& _Other)const
{
return !((*this) == _Other);
}
bool Str::operator<(const Str& _Other)const
{
// 비교하려는 두 문자열이 모두 문자가 하나도 없다면 비교할 수 없다.
assert(m_Length || _Other.m_Length);
int MinLen = m_Length < _Other.m_Length ? m_Length : _Other.m_Length;
for (int i = 0; i < MinLen; ++i)
{
if (m_pStr[i] < _Other.m_pStr[i])
return true;
else if (m_pStr[i] > _Other.m_pStr[i])
return false;
}
// 모든 문자가 동일했다면 문자열이 길이가 더 짧은 문자열이 더 작다.
if (m_Length < _Other.m_Length)
return true;
else
return false;
}
bool Str::operator >(const Str& _Other)const
{
// 비교하려는 두 문자열이 모두 문자가 하나도 없다면 비교할 수 없다.
assert(m_Length || _Other.m_Length);
int MinLen = m_Length < _Other.m_Length ? m_Length : _Other.m_Length;
for (int i = 0; i < MinLen; ++i)
{
if (m_pStr[i] > _Other.m_pStr[i])
return true;
else if (m_pStr[i] < _Other.m_pStr[i])
return false;
}
// 모든 문자가 동일했다면 문자열이 길이가 더 짧은 문자열이 더 작다.
if (m_Length > _Other.m_Length)
return true;
else
return false;
}
bool Str::operator<=(const Str& _Other)const
{
// 비교하려는 두 문자열이 모두 문자가 하나도 없다면 비교할 수 없다.
if (!(m_Length || _Other.m_Length))
return true;
int MinLen = m_Length < _Other.m_Length ? m_Length : _Other.m_Length;
for (int i = 0; i < MinLen; ++i)
{
if (m_pStr[i] < _Other.m_pStr[i])
return true;
else if (m_pStr[i] > _Other.m_pStr[i])
return false;
}
// 모든 문자가 동일했다면 문자열이 길이가 더 짧은 문자열이 더 작다.
if (m_Length <= _Other.m_Length)
return true;
else
return false;
}
bool Str::operator>=(const Str& _Other)const
{
// 비교하려는 두 문자열이 모두 문자가 하나도 없다면 비교할 수 없다.
if (!(m_Length || _Other.m_Length))
return true;
int MinLen = m_Length < _Other.m_Length ? m_Length : _Other.m_Length;
for (int i = 0; i < MinLen; ++i)
{
if (m_pStr[i] > _Other.m_pStr[i])
return true;
else if (m_pStr[i] < _Other.m_pStr[i])
return false;
}
// 모든 문자가 동일했다면 문자열이 길이가 더 짧은 문자열이 더 작다.
if (m_Length >= _Other.m_Length)
return true;
else
return false;
}
1차 24.01.11
2차 24.01.12
3차 24.01.15
4차 24.01.16
5차 24.01.17