简体   繁体   English

STL映射输出迭代器查找值

[英]STL map output iterator lookup value

I am trying to make a function that is generic and takes an output iterator to any container type (that is, a particular kind of map regardless of allocator). 我试图做一个通用的函数,并且将输出迭代器带到任何容器类型(即,与分配器无关的一种特殊类型的映射)。

However, I would like to know whether the map referenced by itOut already contains an element, and if so, update it 但是,我想知道itOut引用的地图是否已经包含一个元素,如果是,请对其进行更新。

template<typename OutputIterator>
static void foo(const std::wstring& rstr, OutputIterator itOut)
{
    // Insert two items into the map that itOut references
    itOut++ = TokenPair(t, 1);
    itOut++ = TokenPair(t, 2);

    // This here I don't know how to do
    if(somehow check the map already contains t)
    {
        map[t] = 5;
    }
}

Surely there must be a way for this? 当然一定有办法吗?

It seems to be the best way because other parts of the program are trying to pass maps that have identical pairs, but different allocators. 这似乎是最好的方法,因为程序的其他部分正在尝试传递具有相同对但分配器不同的映射

I went with the solution that Andy Prowl suggested, where the method is templated to take the appropriate map type. 我采用了安迪·普罗(Andy Prowl)提出的解决方案,其中该方法被模板化以采用适当的地图类型。 That way the allocator becomes irrelevant. 这样分配器就变得无关紧要了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM