完璧だ。
Classes.h,Classes.cpp として
#pragma once
class CPairList
{
private:
??? typedef std::wstring m_a;
??? typedef std::wstring m_b;
??? typedef std::pair<m_a, m_b> Pair;
??? typedef std::vector<Pair> vec_t;
??? vec_t m_vec;
public:
??? void Add(LPCWSTR szKey, LPCWSTR szValue);
??? void Remove(LPCWSTR szKey);
??? std::wstring operator[](std::wstring szKey);
??? std::wstring GetValue(int i);
??? std::wstring GetKey(int i);
??? int Count();
};
//??? TODO: "StdAfx.h" #include <vector>
#include "StdAfx.h"
#include "Classes.h"
/*
*??? CPairListClass
*/
void CPairList::Add(LPCWSTR szKey, LPCWSTR szValue)
{
??? m_vec.push_back(Pair(szKey, szValue));
}
void CPairList::Remove(LPCWSTR szKey)
{
??? vec_t::iterator it = m_vec.begin();
??? vec_t::iterator itend = m_vec.end();
??? for (; it != itend; it++)?
?? {?
?????? if ( (*it).first == szKey)?
?????? {?
?????????? m_vec.erase(it);
?????????? break;?
?????? }
??? }
}
std::wstring CPairList::operator[](std::wstring szKey)
{
??? std::wstring result;
??? vec_t::iterator it = m_vec.begin();
??? vec_t::iterator itend = m_vec.end();
??? for (; it != itend; it++)?
?? {?
?????? if ( (*it).first == szKey)?
?????? {?
?????????? result = (*it).second;
?????????? break;
??????? }
?? }
??? return result;
}
int CPairList::Count()
{
??? return m_vec.size();
}
std::wstring CPairList::GetValue(int i)
{
??? return m_vec[i].second;
}
std::wstring CPairList::GetKey(int i)
{
??? return m_vec[i].first;
}
と定義しておいて
CPairList aList;
aList.Add(L"おまえ", L"あほ");
wstring ws = aList[L"おまえ"];
な感じで順番は保持されたままの連想配列もどきの完成、STL 恐るべし。
でも現在 ini 保存に std::map を使っているのでココでソートされてしまう…
こっちも変更しなきゃ、あぁ先は長い。
しかしワシもいつのまにかこんなコードが普通に書けるようになった。
思えば二年前に唐突にデルヒャァから C++ に乗り換えして
「やっぱりポインタがワカンネェ!意味は解るけど使い方が全然ワカンネェ!」
だったのにね。