Как я могу попробовать структуру POD на карте Std ::?C++

Программы на C++. Форум разработчиков
Ответить
Anonymous
 Как я могу попробовать структуру POD на карте Std ::?

Сообщение Anonymous »

У меня есть карта int -> {basic types} < /code> Мне нужно сохранить. }; и хранить значения напрямую, построив структуру in situ во время магазина. Я не ожидаю, что будут какие -либо повторяющиеся ключи, поэтому Try_emplace < /code> кажется идеальным. < /P>

Я написал этот код: < /p>

// mcve.cpp
#include
#include

struct various { int f1, f2; };
using map_t = std::map;

void
example()
{
map_t dict;

//dict.try_emplace(1, 2);
dict.try_emplace(1, 1, 2);
//dict.try_emplace(1, {1, 2});
}
< /code>

Но ни один из этих вариантов не работает. (Версия: Clang версия 5.0.1 (теги/release_501/final))

/opt/local/libexec/llvm-5.0/include/c++/v1/tuple:1365:7: error: no matching
constructor for initialization of 'various'
second(_VSTD::forward(_VSTD::get(__second_args))...)
^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

### ... many lines ...

mcve.cpp:14:7: note: in instantiation of function template specialization
'std::__1::map::try_emplace' requested here
dict.try_emplace(1, 1, 2);
^
mcve.cpp:4:8: note: candidate constructor (the implicit copy constructor) not
viable: requires 1 argument, but 2 were provided
struct various { int f1, f2; };
^
mcve.cpp:4:8: note: candidate constructor (the implicit move constructor) not
viable: requires 1 argument, but 2 were provided
mcve.cpp:4:8: note: candidate constructor (the implicit default constructor) not
viable: requires 0 arguments, but 2 were provided
1 error generated.
< /code>

Использование G ++ Я получаю подобные ошибки. (Версия: G ++ (Macports GCC7 7.2.0_0) 7.2.0): < /p>

In file included from /opt/local/include/gcc7/c++/bits/node_handle.h:40:0,
from /opt/local/include/gcc7/c++/bits/stl_tree.h:72,
from /opt/local/include/gcc7/c++/map:60,
from mcve.cpp:1:
/opt/local/include/gcc7/c++/tuple: In instantiation of 'std::pair::pair(std::tuple&, std::tuple&, std::_Index_tuple, std::_Index_tuple) [with _Args1 = {int&&}; long unsigned int ..._Indexes1 = {0}; _Args2 = {int&&, int&&}; long unsigned int ..._Indexes2 = {0, 1}; _T1 = const int; _T2 = various]':

### ... many lines ...

mcve.cpp:14:26: required from here
/opt/local/include/gcc7/c++/tuple:1652:70: error: no matching function for call to 'various::various(int, int)'
second(std::forward(std::get(__tuple2))...)
^
mcve.cpp:4:8: note: candidate: various::various()
struct various { int f1, f2; };
^~~~~~~
mcve.cpp:4:8: note: candidate expects 0 arguments, 2 provided
mcve.cpp:4:8: note: candidate: constexpr various::various(const various&)
mcve.cpp:4:8: note: candidate expects 1 argument, 2 provided
mcve.cpp:4:8: note: candidate: constexpr various::various(various&&)
mcve.cpp:4:8: note: candidate expects 1 argument, 2 provided
< /code>

Итак, как я могу эффективно хранить данные в своей карте (в идеале, используя простой, читаемый код)? < /p>

Подробнее здесь: https://stackoverflow.com/questions/483 ... n-a-stdmap
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «C++»