English 中文(简体)
从地图中获取详细信息并将其保存到另一张地图中?
原标题:Taking details from a map and saving it into another?

我有一张这样的地图

map<string, pair< pair<int, int>, string> >

只获取这两个字符串并保存到另一个像这样的映射中最简单的方法是什么

map<string, string>

? I mean is there another way other than something like this??

    map<string, pair< pair<int, int>, string> >  info;
    map<string, pair< pair<int, int>, string> >::iterator i;
    map<string, string> something;


    for(i=info.begin(); i!=info.end(); ++i)
        something[*i).first] = ((*i).second).second;
最佳回答

首先,我定义一个合适的类型:

struct Mapped
{
    int someSignificantName;
    int anotherName;
    std::string yetAnother;
};

There are almost no cases where std::pair is an acceptable solution (except for quick hacks and tests). Given that, you define a mapping functional object:

struct Remap
    : std::unary_operator<std::pair<std::string, Mapped>,
                          std::pair<std::string, std::string> >
{
    std::pair<std::string, std::string> operator()(
            std::pair<std::string, Mapped> const& in ) const
    {
        return std::make_pair( in.first, in.second.yetAnother );
    }
};

,然后使用std::transform

std::transform( in.begin(), in.end(),
                std::inserter( out, out.end() ),
                Remap() );
问题回答
map<string, pair< pair<int, int>, string> > map1 /* = ... */;
map<string, string> map2;

BOOST_FOREACH (const pair<string, pair< pair<int, int>, string> > >& it, map1) {
   map2[it.first] = it.second.second;
}

顺便说一下,EWTYPE。

最简单的方法是编写简单的for循环。

typedef pair<pair<int, int>, string> strange_pair_t;
typedef map<string, strange_pair_t> strange_type_t;
strange_type_t src_map;

map<string, string> dst_map;
for(strange_type_t::const_iterator it = src_map.begin(); it!=src_map.end(); ++it)
{
  dst_map.insert( make_pair( it->first, it->second.second ) );
}

棘手的方式(“一句话”):

std::transform( src_map.begin(), src_map.end(), inserter(dst_map, dst_map.end()), 
    boost::lambda::bind( 
        boost::lambda::constructor<pair<string,string>>(), 
        boost::lambda::bind( &strange_type_t::value_type::first, boost::lambda::_1 ), 
        boost::lambda::bind( &strange_pair_t::second, boost::lambda::bind( &strange_type_t::value_type::second, boost::lambda::_1 ) )
    )
);

C++0x方式:

for_each( src_map.begin(), src_map.end(), [&dst_map](const strange_type_t::value_type& value) {
    dst_map.insert( make_pair( value.first, value.second.second ) ); } );




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签