Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recursive Standard C++ Library Container Types?

74 views
Skip to first unread message

Pavel

unread,
Feb 18, 2021, 8:48:02 PM2/18/21
to
Is it possible to define Standard C++ Library Container Types UM, OM and L such
that:

- UM is an std::unordered_map with key_type int and mapped_type iterator to OM
- OM is an std::map with key_type int and mapped_type L
- L is an std::list of iterators to UM

?

TIA,
-Pavel.

Bonita Montero

unread,
Feb 19, 2021, 8:56:59 AM2/19/21
to
#include <unordered_map>
#include <map>
#include <list>

template<typename L>
using OM = std::map<int, L>;
template<typename L>
using UM = std::unordered_map<int, typename OM<L>::iterator>;
template<typename T>
using L = std::list<typename OM<T>::iterator>;

Ralf Goertz

unread,
Feb 19, 2021, 11:14:03 AM2/19/21
to
Am Fri, 19 Feb 2021 14:56:43 +0100
schrieb Bonita Montero <Bonita....@gmail.com>:
And how would you instantiate those? How do I declare the three
variables om, um and l? (I had tried something similar but hadn't
managed to get it compiled.)

Bonita Montero

unread,
Feb 19, 2021, 11:44:53 AM2/19/21
to
Am 19.02.2021 um 17:13 schrieb Ralf Goertz:
> Am Fri, 19 Feb 2021 14:56:43 +0100
> schrieb Bonita Montero <Bonita....@gmail.com>:
>
>> Am 19.02.2021 um 02:47 schrieb Pavel:
>>> Is it possible to define Standard C++ Library Container Types UM,
>>> OM and L such that:
>>> - UM is an std::unordered_map with key_type int and mapped_type
>>> iterator to OM
>>> - OM is an std::map with key_type int and mapped_type L
>>> - L is an std::list of iterators to UM
>>
>> #include <unordered_map>
>> #include <map>
>> #include <list>
>>
>> template<typename L>
>> using OM = std::map<int, L>;
>> template<typename L>
>> using UM = std::unordered_map<int, typename OM<L>::iterator>;
>> template<typename T>
>> using L = std::list<typename OM<T>::iterator>;

f.e.
L<int> li;

Pavel

unread,
Feb 20, 2021, 12:36:43 AM2/20/21
to
Bonita Montero wrote:
> Am 19.02.2021 um 17:13 schrieb Ralf Goertz:
>> Am Fri, 19 Feb 2021 14:56:43 +0100
>> schrieb Bonita Montero <Bonita....@gmail.com>:
>>
>>> Am 19.02.2021 um 02:47 schrieb Pavel:
>>>> Is it possible to define Standard C++ Library Container Types UM,
>>>> OM and L such that:
>>>> - UM is an std::unordered_map with key_type int and mapped_type
>>>> iterator to OM
>>>> - OM is an std::map with key_type int and mapped_type L
>>>> - L is an std::list of iterators to UM
>>>
>>> #include <unordered_map>
>>> #include <map>
>>> #include <list>
>>>
>>> template<typename L>
>>> using OM = std::map<int, L>;
>>> template<typename L>
>>> using UM = std::unordered_map<int, typename OM<L>::iterator>;
>>> template<typename T>
>>> using L = std::list<typename OM<T>::iterator>;
>
> f.e.
> L<int> li;

Thanks Bonita, this is not what's needed:

`li' above is

std::list<std::map<int, int>::iterator>

but it needs to be

std::list<iterator-to-UM>

.

(Note that UM should not be std::unordered_map<int, int> but
std::unordered_map<int, iterator-to-OM>)

Pavel

unread,
Feb 20, 2021, 1:14:08 AM2/20/21
to
Just got an acceptable answer at stack overflow: the trick is to box the list.

I think I used this trick before in a slightly simpler situation .. getting
older :-(.

See
https://stackoverflow.com/questions/66271587/recursive-standard-c-library-container-types

-Pavel
0 new messages