'C++' std::clear

토스트·2024년 12월 25일

'C++' std::string

목록 보기
4/12

clear

C++11 ~

void clear() noexcept;

: 문자열의 내용을 지워서 빈 문자열이 됩니다.

<example>

#include <iostream>
#include <string>

using namespace std;

int main() {
	string str = "Hello, World!";
    
    str.clear();
    
    cout << "str : " <<str << endl;
    cout << "length : " <<str.length() << endl;
    
	return 0;
}

결과값

str.clear()와 str = ""; 모두 문자열을 비우는 데 사용할 수 있지만, clear()는 더 효율적으로 메모리를 관리할 수 있는 방법입니다. 그러나 대부분의 경우 둘의 차이는 미미합니다.

0개의 댓글