본문 바로가기
C++

[C++] 네임스페이스와 std

by ybin.im 2025. 2. 24.

 

- namespace

네임스페이스는 객체, 클래스, 함수 등을 저장 및 구분해주는 공간이다.

 

#include <iostream>

using namespace std;

int main(){

	std::cout << "일반적인 cout" << std::endl;
	cout << "using namespace std" << endl;

}​

 

밑의 코드와 같이 namespace를 이용해서 다른 네임스페이스에 같은 print함수를 만들었다.
#include <iostream>
#include <string>

namespace test {
    void print() {
        std::cout << "namespace test" << std::endl;
    }
}
namespace hello {
    void print() {
        std::cout << "hello" << std::endl;
    }
}


int main() {

	test::print();
	hello::print();

	return 0;
    }

 

namespace를 통해서 같은 함수이름을 사용했지만, 충돌이 나지 않는걸 볼 수 있다
출력결과

- using namespace std;

using namespace std; 를 선언해주면,
std:: namespace를 계속 붙여주지 않아도 된다.

 

- std

std는 standard의 약자로 C++에서 기본적으로 제공하는 표준 라이브러리이다.
<string>, <iostream> 등 다양한 헤더를 포함한다.