-3

first of all i am a beginner to cpp. How can we define a map inside a map like

std::  map<std ::string,int> map_0={{"hello",1},{"a",3},{"b",5}}
std::  map<std ::string,int> map_1={{"hi",2},{"2",3},{"3",4}} 

now i want to assign new element which can refer to my map_0 like

     map_1["print"] = (int) & map_0;

(just wrote down i don't know correct expression to express here) so that i can do some thing like $map_1["print"]=\%map_0; in perl

  • 3
    That is highly irregular and will probably not work very well (especially on 64-bit systems)! Can you perhaps elaborate on what the *actual* problem you try to solve is? *Why* do you want to do something like this? Please take some time to read about [the XY problem](http://xyproblem.info/) and think about how it relates to your question (asking about help with a specific solution to an unknown problem). Also please take some time to [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). – Some programmer dude Jun 15 '17 at 09:54
  • @Someprogrammerdude i have gone through them already the actual thing was this sort of line is to be converted into c++ i want to get help ` txCtrlEntry["scheduleId"] = \%d; ` – Saketh Pothukuchi Jun 15 '17 at 10:00
  • What you want to do first of all breaks [the strict aliasing rule](http://stackoverflow.com/questions/98650/what-is-the-strict-aliasing-rule). It will not work on today's 64-bit systems (where `int` is usually 32 bits and pointers are 64 bits). And we still don't know what problem you're trying to solve, why you want to do this. – Some programmer dude Jun 15 '17 at 10:05
  • did u understand Mr? – Saketh Pothukuchi Jun 15 '17 at 10:06
  • You can't trivially do this in a strongly statically typed language like C++. You could use something like `std::variant` in C++17 to do this, by letting your map elements be an `int` or another `map`, or maybe `std::any` (also in C++17) so let the map contain anything at all (the closest to the perl behaviour you're likely to get). – Rook Jun 15 '17 at 10:15
  • @Rook: C++ isn't strongly typed. – IInspectable Jun 15 '17 at 10:18
  • 1
    @IInspectable not everyone agrees with you there. https://stackoverflow.com/questions/26753483/is-c-considered-weakly-typed-why – Rook Jun 15 '17 at 10:20
  • @Rook: I don't see how that Q&A would be in disagreement with me. It merely states, that the reasoning of some paper is wrong, but C and C++ *"may still be considered weakly-typed"*. Just not for the reasons and rationale outlined in the referenced paper. – IInspectable Jun 15 '17 at 10:23
  • @IInspectable you may feel free to consider it weakly typed. – Rook Jun 15 '17 at 10:24
  • @Someprogrammerdude , IInspectable,gsmaras ,dan, baun mit augen whats wrong with u guys keeping this in hold as the question was clear and some one well expert in perl and C++(Rook) suggested me a good solution and whats wrong with my question and why was it given -3 it is supposed to be a general doubt when u do such stuff like conversion don't go judging on your own everything think for others too . What you are doing may make a new join in SO to quit it just cause he cant expect a good replies and getting more negatives up please hide yourselves when u dont have good replies – Saketh Pothukuchi Jun 17 '17 at 13:42
  • Questions asking "I have this single line of unknown code in one programming language, and I want you to translate it into a completely different language" isn't a very good fit here on SO. Asking a question like "I have this problem that I solved this way in one programming language, and now I try to solve it in another programming language using that way way but I get these problems, *please* help me" are better. And you need to describe the actual problem you try to solve in the first language, with context. Different languages might mean different solutions! – Some programmer dude Jun 17 '17 at 13:52
  • And please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). – Some programmer dude Jun 17 '17 at 13:53
  • @Rook Mr Rook is there a possible way to do this type of conversion i perl to c ++ `$lastTxCtlEntry->{status} = \@tmp;` – Saketh Pothukuchi Jun 19 '17 at 05:38

1 Answers1

2

Key points: trying to naively port perl code to C++ may be quite difficult; the way datastructures work in the two langauges is quite different. You say you are a C++ beginner... this means you might not be the best person to do this sort of language translation. That said, here are some possibilities.


You can't trivially do this in a strongly statically typed language like C++. You could use something like std::variant in C++17 to do this, by letting your map elements be an int or another map, or maybe std::any (also in C++17) so let the map contain anything at all (the closest to the perl behaviour you're likely to get). Neither of these types are entirely straightforward to use, compared to perl... you need to check what type was stored in the container, and use an appropriate accessor function or you'll get a compile error or runtime error, depending on circumstances. The docs should definitely be read!

Here's a std::variant example. You can see that the nested type declarations can get pretty spammy pretty quickly if you want multiple levels of maps, so it might not be totally suitable for your needs.

  std::map<std::string, std::variant<int, std::map<std::string, int>>> m;

  m["foo"] = 1;
  m["bar"] = std::map<std::string, int>{ {"baz", 2} };
  m["bar"] = 3;

  std::cout << std::get<int>(m["bar"]) << "\n";

You can use std::variant::index to find out which one of the list of types is currently stored in the variant, amongst other ways.

There are various libraries which offer a variant type that would work under older compilers and standard libraries, or you could even roll your own (though thius might not be worth it). There are also things called "tagged unions" which are very similar.

Here's a std::any example. This has the advantage that you can multiple levels of nested map without needing huge typedefs.

  std::map<std::string, std::any> m2;

  m2["foo"] = 1;
  m2["bar"] = std::map<std::string, int>{ { "baz", 2 } };
  m2["bar"] = 3;
  m2["qux"] = m;

  std::cout << std::any_cast<int>(m2["bar"]) << "\n";

You can use std::any::type to see which particular type the any holds at a particular type, so you know which kind of any_cast to use.

You'll need a newish compiler to use these features... I've used visual studio 2017 with /std:c++latest and g++ 7.1 with -std=c++17. These features may well be unavailable to you if you're using older compilers.

Rook
  • 5,734
  • 3
  • 34
  • 43