• Home
  • About
    • Seokmin.Lee photo

      Seokmin.Lee

      Hello, I am a master's student in the Department of Convergence Security (Samsung Advanced Security) at Korea University.After graduation, I am expected as a security developer or researcher member of Samsung SDS.

    • Learn More
    • LinkedIn
    • Github
  • Posts
    • All Tags

[ps]co_zy_0001

18 Aug 2024

1. MAP, Insert

cpp에서 map은 python의 dictionary 와 같은 역할을 할 수 있다.

key, value로 형성된 STL이다.

#include <map>
map<long long, int> m;
m.insert({1000000,10})

이렇게 진행되게 되면,

int a = m[1000000];
print(a) //10

insert로 넣었던 값을 갖고 올 수 있게 된다.

특징이 반복되는 Key 값은 삽입 시킬 수 없다 -> runtime error

insert를 통해 {} pair 객체로 넣어주어야 오류가 없다.

2. MAP_at,find,erase

map에서는 선언한 방식에 따라서 value값을 수정할 수 있으며 –> 2 또는 at method를 활용하여 변경 가능하다. –> 1.

map[10000] = 1;
map.at(10000) = 1;

또한 iterator가 vector 와 동일하게 동작한다. find method를 통해서 찾고자 하는 key의 값을 호출 시킬 수 있으며, 반환 하는 것은 그 위치의 iterator 객체를 전달한다. (찾고자 하는 값이 없으면, map.end()를 반한다.)

따라서 지우는 로직은 두개가 가능하다.

map.erase(100000)
map.erase(map.at(100000))

3. MAP 기타 Method

  • map.clear() : vector와 동일하게 모두 지움
  • map.size() : map에 들어가 있는 size 반환
  • map.end(), map.begin() 동일하게 활용가능


PSCPPCOZYMAP Share Tweet +1