기본 콘텐츠로 건너뛰기

글

라벨이 map인 게시물 표시

Powershell Array, Map 정리

Powershell에서 Array, Map에 대한 정리. (매번 찾기 귀찮) 참조: Bash Array, Map 정리 Array 참조:  배열에 대해 알고 싶은 모든 것 # 생성 $empty_array = @() $ar = @("haha", "hoho", "baba", "long string haha hoho") # $ar = "hello", "world, "byebye" # 접근 Write-Output "`$ar[0]=$($ar[0])" Write-Output "all as array=$($ar)" # 기본 Write-Output "all as one=$($ar -join ' ')" # 띄어쓰기를 포함하여 문자열로 합치기 Write-Output "all as one=$(-join $ar)" # 띄어쓰기 없이 문자열로 합치기 Write-Output "length=$($ar.Count)" Write-Output "last=$($ar[-1])" # 현재 상황 Write-Output $ar $ar[-1] = "last" Write-Output $ar # 마지막 원소를 지우고 대입한다. $ar[-2] = "whoami" Write-Output $ar # 마지막-1 원소를 지우고 대입한다. # 추가 $ar += @("blar") $ar += @("keke", "123", "456") $ar += @("long long long string", "another long long long string") Write-Output $ar # Powershell은 Array에 대해 삭제를...

Bash Array, Map 정리

Bash에서 Array, Map에 대한 정리. (매번 찾기 귀찮) 찾아보진 않았지만, Bash에서 Array든 Map이든 동일하게 Map(C++에서 Unordered Map)으로 동작하는 것 같다. 왜냐하면, Array의 Index가 연속하지 않아도 동작한다. 그저 Key가 0 이상의 정수인 Map이랑 비슷하게 동작한다. 예) 1, 2, 3, 9, 10 Array # 생성 declare -a empty_array declare -a ar=(haha hoho baba "long string haha hoho") # 접근 echo "ar[0]=${ar[0]}" echo "all as array=${ar[@]}" # 큰따옴표 안에서 각 원소를 따로따로 전달한다. echo "all as one=${ar[*]}" # 큰따옴표 안에서 각 원소를 문자열 하나로 합쳐 전달한다. echo "indexes=${!ar[@]}" echo "indexes=${!ar[*]}" echo "length=${#ar[@]}" echo "length=${#ar[*]}" echo "last=${ar[-1]}" echo "last=${ar[@]: -1}" # 콜론 뒤에 빈 칸이 꼭 필요하다. 옛 방식 # 현재 상황 declare -p ar #(출력) declare -a ar=([0]="haha" [1]="hoho" [2]="baba" [3]="long string haha hoho") ar[100]=hello # 인덱스를 건너 뛰어도 동작한다. declare -p ar #(출력) declare -a ar=([0]="haha" [1]="hoho" [2]="baba" [3]=...

map::find(const string&) const 피하는 방법 없을까?

STL에 map과 string을 엮어서 자주 사용하는 편인데, 일반적인 규모에서는 흡족할만한 성능을 보여주기에 별 불만이 없다. 하지만 대용량처리(초당 1,000,000건 정도?)를 하다보면 실망스럽기 서울역에 그지 없다. 다음 소스를 보면 암묵적 형변환 에 의해 얼마나 그지 같이 느려지는지 알 수 있다. testFunc(size_t cnt) { map<string,void*> tmpcont; const char* key_1("Hello, world! What are you doing?"); const string key_2(key_1); // 100만개 아이템을 우겨넣는다. (생략) // C-style string key t1 = getTimestamp(); for (size_t i(0); i<cnt; i++) { tmpcont.find(key_1); } t2 = getTimestamp(); cerr << t2-t1 << endl; // STL string key t1 = getTimestamp(); for (size_t i(0); i<cnt; i++) { tmpcont.find(key_2); } t2 = getTimestamp(); cerr << t2-t1 << endl; } 결과는 약 5배 차이가 난다. -_-; 5배 차이라고 해봤자, 1,000,000건에 0.1초 차이이지만, 대용량 처리에서 저런게 누적하기 시작하면 대략 난감하다. map에서 반드시 key_type과 동일한 타입을 인자로 갖는 비교 함수를 쓰지 않는다면 어떻게 해보겠지만, 정말이지 초난감할 뿐이다. 어떻게 하지...? 원본 위치: http://purewell.egloos.com/3400058

GNU의 std::string은 같은 문자열을 공유한다.

보통 std::map에서 key로 std::string을 많이 쓰는데, 쓸 때마다 부담스러웠다. 이유는 key는 std::map에서 node를 만들 때 instance를 새로 만들고 복제하기 때문이다. 그러나 GNU의 std::string은 동일한 문자열은 같은 메모리영역을 공유하도록 만들었다. 따라서 복제가 이뤄져도 실제로 데이터를 복제하는 것이 아니라 내부 레퍼런스 카운터만 올라간다. /usr/include/c++/x.x.x/bits/basic_string.[h|tcc] 파일을 까보면 _M_is_shared()라는 private method가 assign을 비롯하여 여기저기 쓰여 있는 걸로 보아 확실하다. 이를 나름 증명해보자면, 아래 소스를 컴파일해서 돌려보자. #include <iostream> #include <string> #include <map> using namespace std; map<string,void*> gMap; int main(int, char**) { string str1("Hello, world!"); string str2(str1); string str3; str3 = str1; string str4; str4.assign(str1); gMap.insert(map<string,void*>::value_type(str1, NULL)); cout << "str1: " << (void*)str1.c_str() << endl; cout << "str2: " << (void*)str2.c_str() << endl; cout << "str3: " << (void*)str3.c_str() << endl; cout << "str4:...