[C++][중급] std::string 과 std::string_view 차이


[C++][중급] std::string 과 std::string_view 차이

오늘은 std::string 과 std::string_view의 차이에 대해 알아보겠습니다. std::string_view는 C++17에서부터 추가되었습니다. string_view는 내부적으로 문자열에 대한 pointer와 길이만 가지므로 임시객체를 생성하지 않고 문자열을 생성할 수 있습니다. 주의할 점은 string_view는 내부적으로 null 종료 문자를 가지지 않습니다. 이는 길이 정보가 있기 때문이죠. 먼저 짧은 예제를 보겠습니다. #include <string> #include <string_view> void foo(const std::string& s) { } int main() { foo("Practice makes Perfect!"); } 분명히 "Practice makes Perfect!"란 문자열을 foo() 함수의 인자로 넘겼는데, string 객체로 받는다? 이상하죠? 이 원리는요. 컴파일러가 문자열을 받으면 string 생성자로 보내서 임시객체를 만드는 것...


#string #string_view #임시객체

원문링크 : [C++][중급] std::string 과 std::string_view 차이