On Nov 4, 4:44 am, Saeed Amrollahi <amrollahi.sa...@gmail.com> wrote:
Dear all
I have a question. Is there any generic algorithm or solution to
access the keys or values
of a map? I frequently face to such problem and I usually write a
function to copy
the keys/values of a map into a container let say a vector.
Many thanks, in advance, for your help.
#include <string>
#include <iterator>
#include <boost/foreach.hpp>
#include <iostream>
#include <list>
#include <map>
typedef std::map<std::wstring,std::wstring> MyMapType;
typedef std::pair<std::wstring,std::wstring> MapPairType;
MyMapType MyMap;
template<typename U>
std::list<typename U::key_type> getKeys(U const &m)
{
typedef std::pair<typename U::key_type,typename U::mapped_type>
PairType;
std::list<typename U::key_type> tempList;
BOOST_FOREACH(PairType const &i,m)
tempList.push_back(i.first);
return tempList;
}
int main()
{
MyMap[L"one"] = L"Hello World";
MyMap[L"two"] = L"Another string";
std::list<std::wstring> myList = getKeys(MyMap);
BOOST_FOREACH(std::wstring &i,myList)
std::wcout << i << L"\n";
return 0;
}- Hide quoted text -
- Show quoted text -