์ ์์๋ฅผ ์ฝ์
ํ๋ ๋ค ๊ฐ์ง ๋ฐฉ๋ฒ์ ํ์ธํ์ต๋๋ค std::map
.
std::map<int, int> function;
function[0] = 42;
function.insert(std::map<int, int>::value_type(0, 42));
function.insert(std::pair<int, int>(0, 42));
function.insert(std::make_pair(0, 42));
๊ทธ ์ค ์ด๋ ๊ฒ์ด ์ ํธ๋๋ / ๊ด์ฉ์ ์ธ ๋ฐฉ๋ฒ์ ๋๊น? (๊ทธ๋ฆฌ๊ณ ๋ด๊ฐ ์๊ฐํ์ง ์์ ๋ค๋ฅธ ๋ฐฉ๋ฒ์ด ์์ต๋๊น?)
๋ต๋ณ
์ฐ์ , operator[]
๋ฐ insert
๋ฉค๋ฒ ํจ์๋ ๊ธฐ๋ฅ์ ์ผ๋ก ๋์ผํ์ง ์์ต๋๋ค :
- ๋
operator[]
๊ฒ์ ๋๋ค ๊ฒ์ ํค์ ์ฝ์ ๊ธฐ๋ณธ ๊ตฌ์ฑ ๋ฐ๊ฒฌ๋์ง ์๋ ๊ฒฝ์ฐ๋ ๊ฐ์, ๊ทธ๋ฆฌ๊ณ ๋น์ ์ด ๊ฐ์ ์ง์ ํ๋์ ๋ํ ์ฐธ์กฐ๋ฅผ ๋ฐํํฉ๋๋ค.mapped_type
๊ธฐ๋ณธ ๊ตฌ์ฑ ๋ฐ ํ ๋น ๋์ ์ง์ ์ด๊ธฐํ๋๋ ์ด์ ์ ์ป์ ์ ์๋ค๋ฉด ์ด๊ฒ์ ๋นํจ์จ์ ์ผ ์ ์์ต๋๋ค . ์ด ๋ฐฉ๋ฒ์ ์ฌ์ฉํ๋ฉด ์ฝ์ ์ด ์ค์ ๋ก ๋ฐ์ํ๋์ง ๋๋ ์ด์ ์ ์ฝ์ ๋ ํค์ ๊ฐ๋ง ๋ฎ์ด ์ผ๋์ง ์ฌ๋ถ๋ฅผ ํ์ธํ ์ ์์ต๋๋ค. insert
๋ฉค๋ฒ ํจ์๋ ์ข ์ข ์๊ณ ์์ง๋ง, ํค๊ฐ ์ด๋ฏธ ๋งต์ ์กด์ฌํ๋ ๊ฒฝ์ฐ ์๋ฌด๋ฐ ์ํฅ์ ๋ฏธ์น์ง ๊ฒ, ๋ฐํstd::pair<iterator, bool>
๊ด์ฌ์ด์์ ์์๋๊ฐ (์ฝ์ ์ค์ ๋ก ์ํ ๋ ๊ฒฝ์ฐ ํนํ ๊ฒฐ์ ).
๋์ด๋ ๋ชจ๋ ๊ฐ๋ฅ์ฑ์์๋ฅผ ํธ์ถ insert
ํ๋ฉด ์ธ ๊ฐ์ง ๋ชจ๋ ๊ฑฐ์ ๋์ผํฉ๋๋ค. ๋ค์ ํ๋ฒ insert
ํ์ค ์์ ์๋ช
์ ์ดํด ๋ณด๊ฒ ์ต๋๋ค .
typedef pair<const Key, T> value_type;
/* ... */
pair<iterator, bool> insert(const value_type& x);
๊ทธ๋ ๋ค๋ฉด ์ธ ํธ์ถ์ ์ด๋ป๊ฒ ๋ค๋ฅธ๊ฐ์?
std::make_pair
ํ ํ๋ฆฟ ์ธ์ ๊ณต์ ์ ์์กดํ๊ณ ์์๋ค (์ด ๊ฒฝ์ฐ ๊ฒ์ ๋๋ค ) ์ค์ ์๋ ๋ค๋ฅธ ์ ํ์ ์์ฐ ๋ฌด์ธ๊ฐvalue_type
์ ๋ํ ์ถ๊ฐ ํธ์ถ์ด ํ์ํฉ๋๋ค์ง๋์std::pair
๋ก ๋ณํํ๊ธฐ ์ํด ํ ํ๋ฆฟ ์์ฑ์value_type
(์ : ์ถ๊ฐconst
๋กfirst_type
)std::pair<int, int>
๋ํ์ ํ ํ๋ฆฟ ์์ฑ์์ ์ถ๊ฐ ํธ์ถ์ ํ์std::pair
๋ก ๋งค๊ฐ ๋ณ์๋ฅผ ๋ณํํ๊ธฐ ์ํดvalue_type
(์ : ์ถ๊ฐconst
๋กfirst_type
)std::map<int, int>::value_type
insert
๋ฉค๋ฒ ํจ์์์ ์์ํ๋ ๋งค๊ฐ ๋ณ์ ์ ํ์ด๋ฏ๋ก ์์ฌ ํ ์ฌ์ง๊ฐ ์ ํ ์์ต๋๋ค .
๊ฒฐ๊ตญ operator[]
๊ธฐ๋ณธ ์์ฑ ๋ฐ ํ ๋น์ ์ถ๊ฐ ๋น์ฉ์ด์๊ณ mapped_type
์ ํค๊ฐ ํจ๊ณผ์ ์ผ๋ก ์ฝ์
๋์๋์ง ์ฌ๋ถ๋ฅผ ๊ฒฐ์ ํ๋ ๋ฐ ์ ๊ฒฝ ์ฐ์ง ์๋ ํ, ๋ชฉ์ ์ด ์ฝ์
์ผ ๋ ์ฌ์ฉ ํ๋ ๊ฒ์ ํผํ ๊ฒ์
๋๋ค . ๋ฅผ ์ฌ์ฉํ ๋ insert
a๋ฅผ ๋ง๋๋ value_type
๊ฒ์ด ์๋ง๋ ๊ฐ ๊ธธ์ผ ๊ฒ์
๋๋ค.
๋ต๋ณ
C ++ 11๋ถํฐ๋ ๋ ๊ฐ์ง ์ฃผ์ ์ถ๊ฐ ์ต์
์ด ์์ต๋๋ค. ๋จผ์ insert()
๋ชฉ๋ก ์ด๊ธฐํ ๊ตฌ๋ฌธ๊ณผ ํจ๊ป ์ฌ์ฉํ ์ ์์ต๋๋ค .
function.insert({0, 42});
์ด๊ฒ์ ๊ธฐ๋ฅ์ ์ผ๋ก ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
function.insert(std::map<int, int>::value_type(0, 42));
๊ทธ๋ฌ๋ ํจ์ฌ ๋ ๊ฐ๊ฒฐํ๊ณ ์ฝ๊ธฐ ์ฝ์ต๋๋ค. ๋ค๋ฅธ ๋ต๋ณ์์ ์ธ๊ธํ๋ฏ์ด ์ด๊ฒ์ ๋ค๋ฅธ ํ์์ ๋นํด ๋ช ๊ฐ์ง ์ฅ์ ์ด ์์ต๋๋ค.
operator[]
์ ๊ทผ ๋ฐฉ์์ ํญ์ ๊ทธ๋ฐ ๊ฒ์ํ์ง ์์, ํ ๋น ํ ๋งคํ ์ ํ์ ํ์๋กํ๋ค.- ์ด
operator[]
์ ๊ทผ ๋ฐฉ์์ ๊ธฐ์กด ์์๋ฅผ ๋ฎ์ด ์ธ ์ ์์ผ๋ฉฐ ์ด๊ฒ์ด ๋ฐ์ํ๋์ง ์ฌ๋ถ๋ฅผ ์ ์์๋ ๋ฐฉ๋ฒ์ ์ ๊ณตํ์ง ์์ต๋๋ค. insert
๋์ดํ ๋ค๋ฅธ ํ์์ ์์ ์ ํ์ ๋ณํ์ ํฌํจํ๋ฏ๋ก ์ฝ๋ ์๋๊ฐ ๋๋ ค์ง ์ ์์ต๋๋ค.
๊ฐ์ฅ ํฐ ๋จ์ ์์ด ์์์ด ํค์ ๊ฐ์ ๋ณต์ฌ ํ ์ ์์ด์ผํ๊ธฐ ๋๋ฌธ์ unique_ptr
๊ฐ์ด ์๋ ๋งต์์๋ ์๋ํ์ง ์๋๋ค๋ ๊ฒ์
๋๋ค . ์ด๋ ํ์ค์์ ์์ ๋์์ง๋ง ์์ง ํ์ค ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๊ตฌํ์ ๋๋ฌํ์ง ์์์ ์ ์์ต๋๋ค.
๋์งธ, ๋ค์ emplace()
๋ฐฉ๋ฒ์ ์ฌ์ฉํ ์ ์์ต๋๋ค .
function.emplace(0, 42);
์ด๊ฒ์์ ์ด๋ค ํํ๋ณด๋ค ๋ ๊ฐ๊ฒฐํ๊ณ insert()
, ๊ฐ์ ์ด๋ ์ ์ฉ ์ ํ์์ ์ ์๋ unique_ptr
ํ๋ฉฐ ์ด๋ก ์ ์ผ๋ก๋ ์ฝ๊ฐ ๋ ํจ์จ์ ์ผ ์ ์์ต๋๋ค (๊ด์ฐฎ์ ์ปดํ์ผ๋ฌ๊ฐ ์ฐจ์ด๋ฅผ ์ต์ ํํด์ผํ์ง๋ง). ์ ์ผํ ์ฃผ์ ๋จ์ ์ emplace
๋ฐฉ๋ฒ์ด ์ผ๋ฐ์ ์ผ๋ก ๊ทธ๋ฐ ๋ฐฉ์์ผ๋ก ์ฌ์ฉ๋์ง ์๊ธฐ ๋๋ฌธ์ ๋
์๋ฅผ ์ฝ๊ฐ ๋๋ผ๊ฒ ํ ์ ์๋ค๋ ๊ฒ์
๋๋ค.
๋ต๋ณ
์ฒซ ๋ฒ์งธ ๋ฒ์ :
function[0] = 42; // version 1
๊ฐ 42๋ฅผ ๋งต์ ์ฝ์
ํ๊ฑฐ๋ ์ฝ์
ํ์ง ์์ ์ ์์ต๋๋ค. ํค 0
๊ฐ ์์ผ๋ฉด ํด๋น ํค์ 42๋ฅผ ํ ๋นํ์ฌ ํด๋น ํค์ ๊ฐ์ ๋ฎ์ด ์๋๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ํค / ๊ฐ ์์ ์ฝ์
ํฉ๋๋ค.
์ฝ์ ๊ธฐ๋ฅ :
function.insert(std::map<int, int>::value_type(0, 42)); // version 2
function.insert(std::pair<int, int>(0, 42)); // version 3
function.insert(std::make_pair(0, 42)); // version 4
๋ฐ๋ฉด์ ํค๊ฐ 0
์ด๋ฏธ ๋งต์์๋ ๊ฒฝ์ฐ ์๋ฌด ์์
๋ ์ํํ์ง ๋ง์ญ์์ค . ํค๊ฐ ์์ผ๋ฉด ํค / ๊ฐ ์์ ์ฝ์
ํฉ๋๋ค.
์ธ ๊ฐ์ง ์ฝ์
๊ธฐ๋ฅ์ ๊ฑฐ์ ๋์ผํฉ๋๋ค. std::map<int, int>::value_type
๋ IS typedef
์ ๋ํ std::pair<const int, int>
, ๊ทธ๋ฆฌ๊ณ std::make_pair()
๋ถ๋ช
ํ์ ์์ฐ std::pair<>
ํ
ํ๋ฆฟ ๊ณต์ ๋ง๋ฒ์ ํตํด. ๊ทธ๋ฌ๋ ์ต์ข
๊ฒฐ๊ณผ๋ ๋ฒ์ 2, 3 ๋ฐ 4์์ ๋์ผํด์ผํฉ๋๋ค.
์ด๋ ๊ฒ์ ์ฌ์ฉํ ๊น์? ๊ฐ์ธ์ ์ผ๋ก ๋ฒ์ 1์ ์ ํธํฉ๋๋ค. ๊ฐ๊ฒฐํ๊ณ โ์์ฐ ์ค๋ฝ๋คโ. ๊ทธ ๋ฎ์ด ์ฐ๊ธฐ ํ๋์ด ์๊ตฌ๋์ง ์๋ ๊ฒฝ์ฐ๋ ๋จ์ผ์ด ๋๋ ๋ชฐ๋ผ ๋ฒ์ 2์ 3 ๋ฏธ๋ง์ ์
๋ ฅ์ ํ์๋กํ๊ธฐ ๋๋ฌธ์ ๋ฌผ๋ก , ๊ทธ๋๋ ๋ฒ์ 4๋ฅผ ์ ํธ ์ฌ์ค์ ์ ํค / ๊ฐ ์์ ์ฝ์
ํ๋ ๋ฐฉ๋ฒ std::map
.
์์ฑ์ ์ค ํ๋๋ฅผ ํตํด ๋งต์ ๊ฐ์ ์ฝ์ ํ๋ ๋ ๋ค๋ฅธ ๋ฐฉ๋ฒ :
std::map<int, int> quadratic_func;
quadratic_func[0] = 0;
quadratic_func[1] = 1;
quadratic_func[2] = 4;
quadratic_func[3] = 9;
std::map<int, int> my_func(quadratic_func.begin(), quadratic_func.end());
๋ต๋ณ
ํค 0์ผ๋ก ์์๋ฅผ ๋ฎ์ด ์ฐ๋ ค๋ฉด
function[0] = 42;
๊ทธ๋ ์ง ์์ผ๋ฉด:
function.insert(std::make_pair(0, 42));
๋ต๋ณ
C ++ 17 std::map
์ ๋ ๊ฐ์ง ์๋ก์ด ์ฝ์
๋ฐฉ๋ฒ์ ์ ๊ณต ํ๊ธฐ ๋๋ฌธ์ sp2danny ์ ์ฃผ์ ์์๋ ์ธ๊ธํ๋ฏ์ด insert_or_assign()
๋ฐ .try_emplace()
insert_or_assign()
๊ธฐ๋ณธ์ ์ผ๋ก์ insert_or_assign()
์ โ๊ฐ์ ๋โ๋ฒ์ ์
๋๋ค operator[]
. ๋์กฐ์ ์ผ๋ก operator[]
, insert_or_assign()
๊ธฐ๋ณธ ์๋ ๋ ์ง๋์ ๊ฐ ์ ํ์ ํ์๋กํ์ง ์์ต๋๋ค. ์๋ฅผ ๋ค์ด, ๋ค์ ์ฝ๋๋ MyClass
๊ธฐ๋ณธ ์์ฑ์๊ฐ ์๊ธฐ ๋๋ฌธ์ ์ปดํ์ผ ๋์ง ์์ต๋๋ค.
class MyClass {
public:
MyClass(int i) : m_i(i) {};
int m_i;
};
int main() {
std::map<int, MyClass> myMap;
// VS2017: "C2512: 'MyClass::MyClass' : no appropriate default constructor available"
// Coliru: "error: no matching function for call to 'MyClass::MyClass()"
myMap[0] = MyClass(1);
return 0;
}
๊ทธ๋ฌ๋ myMap[0] = MyClass(1);
๋ค์ ์ค๋ก ๋ฐ๊พธ๋ฉด ์ฝ๋๊ฐ ์ปดํ์ผ๋๊ณ ์๋ ํ๋๋ก ์ฝ์
์ด ์ํ๋ฉ๋๋ค.
myMap.insert_or_assign(0, MyClass(1));
๋ํ, ์ ์ฌ insert()
, insert_or_assign()
๋ฐํํฉ๋๋ค pair<iterator, bool>
. ๋ถ์ธ ๊ฐ์ true
์ฝ์
์ด ๋ฐ์ํ false
๊ฒฝ์ฐ์ ํ ๋น์ด ์๋ฃ๋ ๊ฒฝ์ฐ์
๋๋ค. ๋ฐ๋ณต๊ธฐ๋ ์ฝ์
๋๋ ์
๋ฐ์ดํธ ๋ ์์๋ฅผ ๊ฐ๋ฆฌ ํต๋๋ค.
try_emplace()
์์ ์ ์ฌํ๊ฒ์ try_emplace()
โ๊ฐ์ โ์
๋๋ค emplace()
. ๋์กฐ์ ์ผ๋กํ๋ emplace()
, try_emplace()
์ฝ์
์ ์ด๋ฏธ ๋งต์ ์กด์ฌํ๋ ํค ๋๋ฌธ์ ์คํจ ํ ๊ฒฝ์ฐ ์ธ์๋ฅผ ์์ ํ์ง ์์ต๋๋ค. ์๋ฅผ ๋ค์ด ๋ค์ ์ฝ๋๋ ๋งต์ ์ด๋ฏธ ์ ์ฅ๋ ํค๋ก ์์๋ฅผ ๋ฐฐ์นํ๋ ค๊ณ ํฉ๋๋ค (* ์ฐธ์กฐ).
int main() {
std::map<int, std::unique_ptr<MyClass>> myMap2;
myMap2.emplace(0, std::make_unique<MyClass>(1));
auto pMyObj = std::make_unique<MyClass>(2);
auto [it, b] = myMap2.emplace(0, std::move(pMyObj)); // *
if (!b)
std::cout << "pMyObj was not inserted" << std::endl;
if (pMyObj == nullptr)
std::cout << "pMyObj was modified anyway" << std::endl;
else
std::cout << "pMyObj.m_i = " << pMyObj->m_i << std::endl;
return 0;
}
์ถ๋ ฅ (์ต์ VS2017 ๋ฐ Coliru์ ๊ฒฝ์ฐ) :
pMyObj๊ฐ ์ฝ์ ๋์ง ์์์ต๋๋ค.
pMyObj๊ฐ ์ด์จ๋ ์์ ๋์์ต๋๋ค.
๋ณด์๋ค์ํผ๋ pMyObj
๋ ์ด์ ์๋ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌ ํค์ง ์์ต๋๋ค. ๊ทธ๋ฌ๋ auto [it, b] = myMap2.emplace(0, std::move(pMyObj));
๋ค์ ์ฝ๋๋ก ๋์ฒดํ๋ฉด pMyObj
๋ณ๊ฒฝ๋์ง ์์ ์ํ๋ก ์ ์ง ๋๋ฏ๋ก ์ถ๋ ฅ์ด ๋ค๋ฅด๊ฒ ๋ณด์
๋๋ค .
auto [it, b] = myMap2.try_emplace(0, std::move(pMyObj));
์ฐ์ถ:
pMyObj๊ฐ ์ฝ์ ๋์ง ์์์ต๋๋ค.
pMyObj pMyObj.m_i = 2
์ฐธ๊ณ :์ด ๋ต๋ณ์ ๋ง๊ฒ ์ค๋ช ์ ๊ฐ๋ฅํ ํ ์งง๊ณ ๊ฐ๋จํ๊ฒ ์ ์งํ๋ ค๊ณ ํ์ต๋๋ค. ๋ณด๋ค ์ ํํ๊ณ ํฌ๊ด์ ์ธ ์ค๋ช ์ ์ํด Fluent C ++ ์ ๋ํ ์ด ๊ธฐ์ฌ ๋ฅผ ์ฝ๋ ๊ฒ์ด ์ข์ต๋๋ค .
๋ต๋ณ
์์์ ์ธ๊ธ ํ ๋ฒ์ ๊ฐ์ ์๊ฐ ๋น๊ต๋ฅผ ์คํํ์ต๋๋ค.
function[0] = 42;
function.insert(std::map<int, int>::value_type(0, 42));
function.insert(std::pair<int, int>(0, 42));
function.insert(std::make_pair(0, 42));
์ธ์ํธ ๋ฒ์ ๊ฐ์ ์๊ฐ ์ฐจ์ด๊ฐ ์๋ค๋ ๊ฒ์ด ๋ฐํ์ก์ต๋๋ค.
#include <map>
#include <vector>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
class Widget {
public:
Widget() {
m_vec.resize(100);
for(unsigned long it = 0; it < 100;it++) {
m_vec[it] = 1.0;
}
}
Widget(double el) {
m_vec.resize(100);
for(unsigned long it = 0; it < 100;it++) {
m_vec[it] = el;
}
}
private:
std::vector<double> m_vec;
};
int main(int argc, char* argv[]) {
std::map<int,Widget> map_W;
ptime t1 = boost::posix_time::microsec_clock::local_time();
for(int it = 0; it < 10000;it++) {
map_W.insert(std::pair<int,Widget>(it,Widget(2.0)));
}
ptime t2 = boost::posix_time::microsec_clock::local_time();
time_duration diff = t2 - t1;
std::cout << diff.total_milliseconds() << std::endl;
std::map<int,Widget> map_W_2;
ptime t1_2 = boost::posix_time::microsec_clock::local_time();
for(int it = 0; it < 10000;it++) {
map_W_2.insert(std::make_pair(it,Widget(2.0)));
}
ptime t2_2 = boost::posix_time::microsec_clock::local_time();
time_duration diff_2 = t2_2 - t1_2;
std::cout << diff_2.total_milliseconds() << std::endl;
std::map<int,Widget> map_W_3;
ptime t1_3 = boost::posix_time::microsec_clock::local_time();
for(int it = 0; it < 10000;it++) {
map_W_3[it] = Widget(2.0);
}
ptime t2_3 = boost::posix_time::microsec_clock::local_time();
time_duration diff_3 = t2_3 - t1_3;
std::cout << diff_3.total_milliseconds() << std::endl;
std::map<int,Widget> map_W_0;
ptime t1_0 = boost::posix_time::microsec_clock::local_time();
for(int it = 0; it < 10000;it++) {
map_W_0.insert(std::map<int,Widget>::value_type(it,Widget(2.0)));
}
ptime t2_0 = boost::posix_time::microsec_clock::local_time();
time_duration diff_0 = t2_0 - t1_0;
std::cout << diff_0.total_milliseconds() << std::endl;
system("pause");
}
์ด๊ฒ์ ๋ฒ์ ์ ๋ํด ๊ฐ๊ฐ ์ ๊ณตํฉ๋๋ค (ํ์ผ์ 3 ๋ฒ ์คํ ํ์ผ๋ฏ๋ก ๊ฐ๊ฐ์ ๋ํด 3 ๊ฐ์ ์ฐ์ ์๊ฐ ์ฐจ์ด๊ฐ ์์).
map_W.insert(std::pair<int,Widget>(it,Widget(2.0)));
2198ms, 2078ms, 2072ms
map_W_2.insert(std::make_pair(it,Widget(2.0)));
2290ms, 2037ms, 2046ms
map_W_3[it] = Widget(2.0);
2592ms, 2278ms, 2296ms
map_W_0.insert(std::map<int,Widget>::value_type(it,Widget(2.0)));
2234ms, 2031ms, 2027ms
๋ฐ๋ผ์ ์๋ก ๋ค๋ฅธ ์ธ์ํธ ๋ฒ์ ๊ฐ์ ๊ฒฐ๊ณผ๋ ๋ฌด์ ๋ ์ ์์ต๋๋ค (๊ฐ์ค โโํ ์คํธ๋ฅผ ์ํํ์ง ์์)!
์ด map_W_3[it] = Widget(2.0);
๋ฒ์ ์ ์์ ฏ์ ๋ํ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์ฌ์ฉํ ์ด๊ธฐํ๋ก ์ธํด์ด ์์ ์์ ์ฝ 10-15 % ๋ ๋ง์ ์๊ฐ์ด ๊ฑธ๋ฆฝ๋๋ค.
๋ต๋ณ
๊ฐ๋จํ ๋งํด, []
์ฐ์ฐ์๋ ๊ฐ ์ ํ์ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ํธ์ถ ํ ๋ค์ ์ ๊ฐ์ ํ ๋นํ๋ ๊ฒ์ด ํฌํจ๋๊ธฐ ๋๋ฌธ์ ๊ฐ์ ์
๋ฐ์ดํธํ๋ insert()
๋ฐ ๋ ํจ์จ์ ์ด๋ฉฐ ๊ฐ์ ์ถ๊ฐ ํ๋ ๋ฐ ๋ ํจ์จ์ ์
๋๋ค.
ํจ๊ณผ์ ์ธ STL ์์ ์ธ์ฉ ๋ ์ค ๋ํซ : Scott Meyers ์ ํ์ค ํ ํ๋ฆฟ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ฌ์ฉ์ ๊ฐ์ ํ๋ 50 ๊ฐ์ง ํน์ ๋ฐฉ๋ฒ , ํญ๋ชฉ 24๊ฐ ๋์์ด ๋ ์ ์์ต๋๋ค.
template<typename MapType, typename KeyArgType, typename ValueArgType>
typename MapType::iterator
insertKeyAndValue(MapType& m, const KeyArgType&k, const ValueArgType& v)
{
typename MapType::iterator lb = m.lower_bound(k);
if (lb != m.end() && !(m.key_comp()(k, lb->first))) {
lb->second = v;
return lb;
} else {
typedef typename MapType::value_type MVT;
return m.insert(lb, MVT(k, v));
}
}
์ผ๋ฐ ํ๋ก๊ทธ๋๋ฐ์ด์๋ ๋ฒ์ ์ ์ ํํ๊ธฐ๋ก ๊ฒฐ์ ํ ์ ์์ง๋ง ์์ ์์ด ํจ๋ฌ๋ค์ ( โ์ถ๊ฐโ์ โ์ ๋ฐ์ดํธโ๋ฅผ ๊ตฌ๋ณ)์ด ๋งค์ฐ ์ ์ฉํ๋ค๋ ๊ฒ์ ๋๋ค.