Re: Best way to pass a map-like container to a function
Am 31.08.2012 23:16, schrieb Marco Guazzone:
> On Friday, August 31, 2012 12:13:36 AM UTC+2, Daniel Kr?gler wrote:
> [cut]
>
>>> But, when the container is a map (or similar), what do you use?
>>
>> It certainly depends what you want to realize. There are clearly
>> reasonable use-cases, where the associative container can be
>> considered as a sequence of elements. This makes sense, because
>> associative containers and unordered containers also satisfy the
>> Container requirements.
>>
>>> Possibile candidates:
>>>
>>> 1. Two parameters: the first/last iterator pair:
>>> map<...,...> m;
>>> foo(m.begin(), m.end())
>>
>> Yes, this is IMO a very intuitive one. But you may need to explain
>> what foo is going to do, before we speculate on that.
>
> You're right.
> In fact, a possible issue with this signature is when you pass
> iterators to multimap. 'foo', to work correctly, should not make
> any assumption on the uniqueness of the first element of each pair.
>
> Maybe, in that case, it would be better to directly pass the
> container?
The reason I was asking about foo is related to that. If foo is a very
general operation that can be performed without having a container at
hand, it makes very much sense in C++11 (without a proper range
concept) to provide an overload accepting a pair of iterators
representing a range.
>>> 2. Four parameters: the first/last iterator pair representing the
> [cut]
>>
>> I do'n see much value of this signature, but I certainly think
>> that it would be nice to have a view on std::map that solely
>> iterates over its keys or over its values. Java's Map interface
>> provides these views and I would say that these two views are
>> quite often used.
>
> Yes I agree. In addition to Java, there're many other languages
> that have them (e.g., D, Perl, PHP and Python, just to name a
> few). It's a pity that C++11 does not have them, or am I wrong?
>
> [cut]
>
>> I would consider as a different approach that accepts a range as
>> argument. Certainly this approach is often very useful as well,
>> for any container. The very advantage is that the caller has no
>> chance to provide a wrong combination of iterator arguments.
>
> Interesting. I assume you're referring to Boost.Range, are you?
Yes, for example. I really think that C++ should introduce a range
template. Unfortunately it didn't make it into C++11.
If such a range template exists, it would be much simpler to add two
functions associated with the map types that return a range over they
key elements or over the mapped value elements.
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]