-2

I most often work with python and therefore I'm used to being able to put a bool and integer in a single list. I realize that C++ has a different paradigm, however, I imagine that there is a workaround to this issue. Ideally I want a vector that could contain data that looks like {1, 7, true, 8, false, true, 9}. So this vector would have to be defined with syntax like (vector int bool intBoolsVec), however, I realize that isn't proper syntax.

I see that some people suggest using variant that was introduced in C++17, is this the best solution? Seems like this would be a common problem if C++ doesn't easily allow you to work with heterogenous containers, even if those containers are constrained to a couple defined types like a vector that only takes only ints and bools.

What is the easiest way to create a vector that contains both integers and booleans in C++? If someone could also provide me more insight on why C++ doesn't have an easy/obvious way to do this, that might help me better understand C++ as well.

  • 4
    What is your use case? Sounds more like X-Y problem... (I can't imagine what do you want to do with it). Other than that, yes, std::variant or std::any can be used – KIIV Feb 26 '22 at 16:55
  • Standard c++ offers a `std::any` type. Though I am not sure, that's really what you're after. – πάντα ῥεῖ Feb 26 '22 at 17:00
  • Mixing up etherogeneous types like that it's a design smell, I wonder what's his use case. *type safety* is an invaluable feature. – MatG Feb 26 '22 at 17:13
  • 2
    [`std::variant`](https://en.cppreference.com/w/cpp/utility/variant) or [`std::any`](https://en.cppreference.com/w/cpp/utility/any) are the standard options depending on your requirements – Alan Birtles Feb 26 '22 at 17:13
  • Or better create your own structure with `void **` and then add like `` – Darth-CodeX Feb 26 '22 at 18:14
  • 1
    why doesnt c++ have it. Primarily because its a strongly typed language. But as others have pointed out you can actually do it – pm100 Feb 26 '22 at 18:16
  • We all wonder, for what cause different types are mixed. In C++ the architecture of a program should be built according to the real-world problem, so the solution very much depends. 1. Another general way would be two types that are derived/inherit from the same base class - polymorphism (but does not directly work for primitive types, you would encapsulate) and you would store pointers to instances of this class. 2. Or you could create a class that contains both an int and a bool and it contains both of them optionally. 3. You could also store a serialized version or a string representation. – Sebastian Feb 26 '22 at 18:20
  • 1
    Tell us what you are trying to represent, not what the solution you would do in python looks like. Why do you need a list like that? We can then post 'this is what an experinced c++ dev would do ...' – pm100 Feb 26 '22 at 18:20
  • A lot of the (5 to 6 presented) ways are easy/obvious. You just need to get used to any new language. – Sebastian Feb 26 '22 at 18:24
  • I used a vector containing ints and bools as a simple example to illustrate the overall concept. In Python you can make a data structure like a list and put ints and bools in it. Should I think about approaching this completely different when it comes to C++? Also I installed the Mingw compiler for Windows from sourceforge.com and I have compiled c++ prorams with it so I know it is on path and everything. When I try to put #include variant at the top of my program it says no such file or directory when I attempt to compile. Does Mingw for Windows support C++17 or later? – Riley Blackwell Feb 26 '22 at 18:24
  • ***Does Mingw for Windows support C++17 or later?*** c++17 probably is not the default setting. – drescherjm Feb 26 '22 at 18:28
  • ***Also I installed the Mingw compiler for Windows from sourceforge.com*** Install it from msys2. to get a much newer mingw with gcc 11.2 [https://www.msys2.org/](https://www.msys2.org/) more info of the install process in this question: [https://stackoverflow.com/questions/30069830/how-to-install-mingw-w64-and-msys2](https://stackoverflow.com/questions/30069830/how-to-install-mingw-w64-and-msys2) – drescherjm Feb 26 '22 at 18:29
  • And do not forget the angle brackets `#include `. A statically typed language tries to express meaning with types. It helps to catch programming errors at compile time, it limits the types of variables, on the other hand you can be sure what (type) you have and how it reacts. Many language features behave differently depending on the static type, e.g. overloaded functions, templates. This enables metaprogramming - the libraries behave differently depending on the type used, but in a positive (expected, adapted) way and this behavior is known and can be tested at compile time. – Sebastian Feb 26 '22 at 18:40
  • Side note: in our proprietary framework, arrays are (by default) heterogeneous. That is often very handy, for example when reading JSON files. I could write `auto array = Array(1, 7, true, 8, false, true, 9);` – prapin Feb 26 '22 at 18:51
  • I was able to download msys2 from msys2.org and that fixed the issue I was having with including variant in my file. The comments on the vector example did clear up some of the confusion for me when it comes to how C++ handles types. I suppose that I should have just given the example of the problem that I am actually working on but I thought the vector example was analogous and a bit simpler. What I am actually doing is building a chess game. I have a base class ChessPiece and it has sub classes like Pawn, Bishop that are instantiated. I want to put the subclasses in an unordered map. – Riley Blackwell Feb 26 '22 at 22:25
  • Keys for unordered map: int cord[2] = {x, y} to represent a coordinate for where a chess piece is on the chess board. Values for the unordered map: objects of the subclasses Pawn, Bishop, etc which all inherit from the base ChessPiece class. – Riley Blackwell Feb 26 '22 at 22:38
  • If you have custom classes derived from `ChessPiece` as keys, just use `std::map>` (sorted as tree) or `std::unsorted_map>` (hashed). You would need to provide a comparison function or a hash function for `ChessPiece`, respectively. The chess pieces could store their type as (class) enum and can use that identifier to compare or hash. To use the structure for performance comparable to current programs (chess engine with min-max algorithm) you should reconsider and use a more low-level bit-representation with hand-tuned instructions. – Sebastian Feb 27 '22 at 03:14
  • @Sebastian I need the unordered map to be the opposite. The hashed keys should be an array of ints length 2 and the values for each entry in the map should be a object that is a a subclass of ChessPiece. I keep getting errors when I attempt to compile. Do I need to hash the array so it can be a unique key? If I create a hashtable like `unordered_map`and then try to get the value back out using `auto bishop = ["someKey"];` I also get an error. So in total I have 2 different questions, how do I make the key an array and how do I get the value and store it as a variable? – Riley Blackwell Mar 08 '22 at 19:11

1 Answers1

1

My approach would probably be to create my own class, which does exactly what I want it to do. This might be your easiest solution, besides using std::any. Also, you could combine those by creating a custom array of std::any which only allows integers and booleans at certain entries for your example. This would be similar, but not equal to an array of std::variant. Also, In C++ you can store 2 types in a std::pair, if that fits your use-case.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770