ERROR : narrowing conversion of ‘1952541746’ from ‘int’ to ‘char’.

Can anyone help me debugging this code snippet. Thank you!

#include <iostream>
#include <tuple>
#include <map>
#include <list>

int main() {

std::map<std::tuple<int,int,int,int>,std::array<char,3>> mp;
mp.insert({{1,0,0,0},{'a','b','c'}});
mp.insert({{1,1,0,0},{'d','e','f'}});
for(auto itr = mp.begin(); itr!= mp.end(); ++itr){
std::cout<<itr->first;
}
}

I just want to print the key and values of map.I am getting an error at cout. ERROR : narrowing conversion of ‘1952541746’ from ‘int’ to ‘char’.
1
2
   auto t = itr->first;
   std::cout << std::get<0>( t ) << std::get<1>( t ) << std::get<2>( t ) << std::get<3>( t )<< '\n';


You are going to a need a very up-to-date compiler, I think.

https://onlinegdb.com/SJDKnfDAI
Last edited on
Thank you for your reply. how can I get both key and value as the output. And how to return this dict/map to the other function. Please help me through this.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <tuple>
#include <map>
#include <list>

int main()
{
   std::map<std::tuple<int,int,int,int>,std::array<char,3>> mp;
   mp.insert({{1,0,0,0},{'a','b','c'}});
   mp.insert({{1,1,0,0},{'d','e','f'}});

   for(auto itr = mp.begin(); itr!= mp.end(); ++itr)
   {
      auto t = itr->first;
      std::cout << std::get<0>( t ) << std::get<1>( t ) << std::get<2>( t ) << std::get<3>( t ) << "  ";
      for ( char c : itr->second ) std::cout << c;
      std::cout << '\n';
   }
}


1000  abc
1100  def



What are you actually trying to do, @Shruthi? There are far better ways of representing things made up of only 0 and 1. This is rapidly turning into an XY problem.

If you want to go on printing general tuples then check out some of the answers in
https://stackoverflow.com/questions/6245735/pretty-print-stdtuple

It's my personal opinion only, but tuples work well in Python but they are very hard work in C++.
Last edited on
I am trying to replicate the python module into c++. So I will explain you the existing python code. The input to this function TEST is _dict_pattern. I am trying to create this _dict_pattern in the above thread I have explained.

Right now I am trying to generate the dict pattern which is the input to the function TEST i.e., _dict_pattern.
But yet to implement the rest of the code.

_dict_pattern = {(0,1,1):['a','b','c'],(1,0,0):['c','d','e'],(1,1,1):['s','h','f']}

def TEST (_dict_pattern):

list_1 = []
_dict_2 = {}

for key in sorted(_dict_pattern.keys()):
list_1 = []
list_1.append(orient)

for i in range(0,len(key)): // What is the equivalent line of code in c++
result = (xor((list_1[-1]), key[i]))
list_1.append(result)

_dict_2[key] = list_1

return(_dict_2)

Is there a better way to do it? Could you help me in converting the above python code to c++.
Last edited on
What is your code supposed to do?

What is "orient"?

Why are you XORing a char with an int?
You can use structure binding and templated range for ntuples. ( requires C++17 )

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <tuple>
#include <array>
#include <map>
#include <list>

template< typename Tuple , typename Functor , size_t index = 0 >
void foreach( const Tuple& tpl , const Functor& f )
{
  constexpr auto tuple_size = std::tuple_size_v<Tuple>;
  if constexpr( index < tuple_size )
  {
    f( std::get<index>(tpl) );
    foreach< Tuple , Functor , index+1 >(tpl, f);
  }
}

int main()
{
   std::map< std::tuple<int,int,int,int> , std::array<char,3> > mp;
   mp.insert({{1,0,0,0},{'a','b','c'}});
   mp.insert({{1,1,0,0},{'d','e','f'}});

   for( const auto& [key,value] : mp )
   {
      foreach(key,[](const auto& v){ std::cout << v; }); std::cout << " ";
      for( char c : value ){ std::cout << c; } std::cout << std::endl;
   }
}

https://wandbox.org/permlink/Jtsnz0NwI27ZMJR1
Orient is set to 0 by default.

I am XORing list_1 value with the key[i] = Looping through the key in the map (which contains tuples).

Input : {(0,1,1):['a','b','c'],(1,0,0):['c','d','e'],(1,1,1):['s','h','f']}

Expected Output : {(0,1,1):[0,0,1,0],(1,0,0):[0,1,1,1]}

Here the input_dict value is like a dummy value. We are not using it anywhere.

Both the input and output_dict key is same.

But the value of the output_dict is by XORing the input_dict keys.

I Hope this clarifies your questions. Please let me know if you have any other concerns.
I am XORing list_1 value with the key[i]

Please explain.

What is this supposed to achieve?

Why does your input have lists like ['a','b','c']? These aren't used - you are simply using the keys, not the values from the input. The values might as well be null.

I repeat - WHAT IS YOUR CODE ACTUALLY TRYING TO DO?



Let's investigate your Python (with corrected indentation):
def TEST ( _dict_pattern ):                      # suppose you send it a map containing things like { ( 0, 1, 1 ) : anything }

   list_1 = []                                   # initialises output value as empty
   _dict_2 = {}

   for key in sorted( _dict_pattern.keys() ):    # iterates through the map; let's start with key ( 0, 1, 1 )
      list_1 = []                                # (re)-initialise the output value as empty
      list_1.append(orient)                      # now the list is [0]

      for i in range(0,len(key)):                # i ranges through 0, 1, 2 (the positions in the key)
         result = (xor((list_1[-1]), key[i]))    # xor the current last value in the list with the latest key position
                                                 #         List starts [0]
                                                 #            XOR 0 with 0 ... still 0, so
                                                 #         List becomes [0,0]
                                                 #            XOR 1 with 0 ... giving 1, so
                                                 #         List becomes [0,0,1]
                                                 #            XOR 1 with 1 ... giving 0, so
                                                 #         List becomes [0,0,1,0]
         list_1.append(result)                   # So now this particular pair is (0,1,1) : [0,0,1,0]

      _dict_2[key] = list_1                      # Changes this particular dictionary entry to (0,1,1) : [0,0,1,0]



Nope, I still don't see what it is trying to do, other than that the 1s in the key toggle the output sequences between 0 and 1.
Last edited on
I think your python code would do exactly the same as
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <map>
using namespace std;


string test( const string &key )
{
   string value = "0";
   for ( char c : key ) value += ( c == '0' ? value.back() : "01"[value.back()=='0'] );
   return value;
}


int main()
{
   map<string,string> dict;
   for ( string s : { "011", "100" } ) dict.insert( { s, test( s ) } );

   for ( auto pr : dict ) cout << pr.first << "  " << pr.second << '\n';
}


011  0010
100  0111



But I haven't a clue what you would use it for.
Yes that's exactly what I needed. Your code explanation was perfect. Can you please help me in getting this c++ version of the code.

This code is just taking input as dictionary and results dictionary as output as you explained in the previous thread.


The program is intended to output the XOR value of the input_dict keys and gives the output_dict.
Shruthi LS wrote:
Can you please help me in getting this c++ version of the code.

Looks like our posts crossed. Please look at
http://www.cplusplus.com/forum/beginner/271486/#msg1170416
Yes. Thank you so much.
Topic archived. No new replies allowed.