본문 바로가기

String

string::shrink_to_fit C++11 부터 std::string 에 shrink_to_fit() 이라는 함수가 추가 되었다. resize() 가 말그대로 size를 줄인다면, shrink_to_fit() 는 capacity를 줄이는 함수이다. 따라서 문자열을 축소할때 resize와 shrink_to_fit를 동시에 사용하면 실제 길이와 용량을 적절하게 줄일 수 있다. 그런데, shrink_to_fit 를 호출한다고 해서 반드시 capacity가 size와 일치하지는 않는다. VC에서 string의 기본메모리인 15이하는 줄어들지 않으며, 실제테스트 해 본 결과, 약간의 버퍼를 유지하고 있다. Shrink to fitRequests the string to reduce its capacity to fit its size. The r.. 더보기
C# String Object 의 불변성 String Object는 한번 할당이 되면 변하지 않는다.(불변성 존재) 그렇다면 이렇게 하면 어떻게 되는 것일까? string s1 = "A string is more "; string s2 = "than the sum of its chars."; s1 += s2; System.Console.WriteLine(s1); // 출력: A string is more than the sum of its chars. 분명 출력은 s1이 s2와 합쳐진 형태이다.그렇다면, s1이 변한것이 아닌가? 라고 생각할 수도 있지만 그렇지 않다. 결과적으로 처음에 할당된 s1과 s2 개체는 변하지 않았다.+= 연산이 진행됐을때 새 개체(s1)가 생성이 된 것이다.s1과 s2의 합쳐진 형태의 새 string object가 생.. 더보기
C# String Null 문자열 string str = "hello"; string nullStr = null; string emptyStr = String.Empty; string tempStr = str + nullStr; Console.WriteLine(tempStr); // hello 가 출력된다. 그렇다면, 이건 어떨까? bool b = (emptyStr == nullStr); Console.WriteLine(b); // false 당연한거지만 false 이다. emptyStr 은 빈문자열을 가지고 있는 포인터이고, nullStr 은 포인터자체가 null이기 때문이다. string newStr = emptyStr + nullStr;이렇게 하면 새 빈 문자열이 생성된다. Console.WriteLine(emptyStr.Lengt.. 더보기