0
votes

I am trying below python code in c++. So that I can create a wrapper for my this python code to speed up the execution of the code (the original code is different. Right now I am trying on small piece of code which includes deque and dictionary).

Python:

from collections import deque
d=deque()
d.append({'a':1,'b':2})
d.append({'c':3,'d':4})
print(d)

output: deque([{'a': 1, 'b': 2}, {'c': 3, 'd': 4}])

C++ 14:

#include <deque> 
#include <iostream> 
using namespace std; 

#include <map>

std::map<char, int> my_map = {
    { 'a', 1 },
    { 'b', 2 }   
};

int main() 
{ 
    deque<int> mydeque;
    mydeque.push_back(my_map); 
    return 0; 
} 

This gives me an error which is obvious. I don't know how to put a dictionary element to the deque?

prog.cpp: In function 'int main()': prog.cpp:16:29: error: no matching function for call to 'std::deque::push_back(std::map&)' mydeque.push_back(my_map); ^ In file included from /usr/include/c++/5/deque:64:0, from prog.cpp:1: /usr/include/c++/5/bits/stl_deque.h:1516:7: note: candidate: void std::deque<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = int; _Alloc = std::allocator; std::deque<_Tp, _Alloc>::value_type = int] push_back(const value_type& __x) ^ /usr/include/c++/5/bits/stl_deque.h:1516:7: note: no known conversion for argument 1 from 'std::map' to 'const value_type& {aka const int&}' /usr/include/c++/5/bits/stl_deque.h:1531:7: note: candidate: void std::deque<_Tp, _Alloc>::push_back(std::deque<_Tp, _Alloc>::value_type&&) [with _Tp = int; _Alloc = std::allocator; std::deque<_Tp, _Alloc>::value_type = int] push_back(value_type&& __x) ^ /usr/include/c++/5/bits/stl_deque.h:1531:7: note: no known conversion for argument 1 from 'std::map' to 'std::deque::value_type&& {aka int&&}'

2
Well, that's expected since you've declared deque containing int items.user7860670
You can't push a type map into a deque of ints. You can select one element of your map,Luis Alves
try declaring the deque as std::deque<std::map<char, int>>Jean-François Fabre♦
But In python code I have dictionary elements which carries information like pixel value, xpos, ypos etc which is stored into the list.ninjakx
I am trying to create wrapper for one of the function of python code which returns list of dictionaries(deque). I have to then change the whole code.ninjakx

2 Answers

3
votes

python doesn't care much about the actual type of the elements in the deque since it's just storing references.

In C++, the type of the element held by the deque must match. The deque isn't storing references, but the actual object (unless you're creating a deque of pointer on objects, which isn't the case here). Put the proper type as the template parameter:

std::deque<std::map<char, int>> mydeque;
mydeque.push_back(my_map); 
1
votes

Your container content-type must always be the type you wanna insert in it. Unless you do that, you gonna get an error.

std::deque<(type of your content)> mydeque;
(type of your content) var1;
mydeque.push_back(var1);