Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | |||||
| 3 | 4 | 5 | 6 | 7 | 8 | 9 |
| 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| 17 | 18 | 19 | 20 | 21 | 22 | 23 |
| 24 | 25 | 26 | 27 | 28 | 29 | 30 |
| 31 |
Tags
- dfs
- 탐색기법
- 링크드리스트
- 커스텀로그
- c++자료구조
- 구조체포인터
- TPS
- 내가해냄
- 게임개발
- UE5
- 오늘의에러
- 언리얼
- 언리얼로그
- 얌얌코딩
- 코딩테스트
- 개발자
- 백준
- 게임프로그래밍
- 구조체
- 자료구조
- C++
- 코딩
- unreal
- 미라클모닝
- 프로그래밍
- 개발
- permutation
- 재귀함수
- 연산자오버로딩
- fstring
Archives
- Today
- Total
All is well
[YYBASIC0105/얌얌코딩] 간단한 버전의 std::string 클래스 만들어보기 본문
`<string>` 라이브러리의 `std::string` 클래스의 `size()` 함수를 이용하면 사용자 정의 함수를 구현하여 문자열 길이를 구할 필요가 없어집니다.
```cpp
//YYBASIC01_05
#include <iostream>
#include <string>
using namespace std;
// 문자열 관련된 것들
class MyString
{
public:
// 생성자를 이용한 문자열 생성 및 길이 측정
MyString(const char* str)
{
for (int i = 0; i < 256; i++)
this->str[i] = '\0';
for (int i = 0; i < 256; i++)
{
if (str[i] == '\0')
{
len = i;
break;
}
this->str[i] = str[i];
}
}
int size()
{
return len;
}
// 연산자 오버로딩을 통해 문자열에 문자열 추가하기
void operator+=(const char* str)
{
int idx = 0;
for (int i = len; i < 256; i++)
{
if (str[idx] == '\0')
{
len = i;
break;
}
this->str[i] = str[idx++];
}
}
void Print()
{
cout << str << " " << len << endl;
}
private:
char str[256];
int len;
};
int main()
{
string str = "Hello";
int len = str.size();
str += " World";
MyString str2 = "ABCD";
len = str2.size();
// 이는 다음과 같음 - MyString str2("ABCD");
str2.Print();
str2 += "EFG";
str2.Print();
return 0;
}
```
'C++ > YYBASIC' 카테고리의 다른 글
| [YYBASIC0202/얌얌코딩] DAT(Direct Addressing Table) - Hash Table (0) | 2025.02.04 |
|---|---|
| [YYBASIC0201/얌얌코딩] 기본기 복습 (0) | 2025.02.04 |
| [YYBASIC0104/얌얌코딩] 연산자 오버로딩 기본 (0) | 2025.02.03 |
| [YYBASIC0103/얌얌코딩] 생성자, 소멸자 (0) | 2025.02.02 |
| [YYBASIC0102/얌얌코딩] 멀티패러다임 언어 (2) | 2025.02.02 |