Common C ++ Vector

Is it possible in C ++ to create a vector of several types? I would like to be able to create and iterate over a vector containing many different types. For instance:

vector<generic> myVec; myVec.push_back(myInt); myVec.push_back(myString); etc... 

A vector must be able to store different types of data. Is there any other vector type I that I should use in a C ++ library?

Any direction is appreciated.

+4
source share
1 answer

You can use boost::any . For instance:

 #include <vector> #include <boost/any.hpp> #include <iostream> struct my_class { my_class(int i) : x{i} { } int x; }; int main() { std::vector<boost::any> v; v.push_back(42); v.push_back(std::string{"Hello!"}); v.push_back(my_class{1729}); my_class obj = boost::any_cast<my_class>(v[2]); std::cout << obj.x; } 

If you want to limit the set of allowed types to a specific range, instead of boost::variant :

 #include <vector> #include <boost/variant.hpp> #include <iostream> struct my_class { my_class(int i) : x{i} { } int x; }; int main() { typedef boost::variant<int, std::string, my_class> my_variant; std::vector<my_variant> v; v.push_back(42); v.push_back("Hello!"); v.push_back(my_class{1729}); my_class obj = boost::get<my_class>(v[2]); std::cout << obj.x; } 

boost::variant also supports visiting. You can define a visitor who can handle all possible types in a variant:

 struct my_visitor : boost::static_visitor<void> { void operator () (int i) { std::cout << "Look, I got an int! " << i << std::endl; } void operator () (std::string const& s) { std::cout << "Look, I got an string! " << s << std::endl; } void operator () (my_class const& obj) { std::cout << "Look, I got a UDT! And inside it a " << obj.x << std::endl; } }; 

And then call it as shown below:

 int main() { typedef boost::variant<int, std::string, my_class> my_variant; std::vector<my_variant> v; v.push_back(42); v.push_back("Hello!"); v.push_back(my_class{1729}); my_visitor mv; for (auto const& e : v) { e.apply_visitor(mv); } } 

Here is a living example . The best part about boost::variant is that it will run a compile-time check to make sure your visitor can handle all the types that this option can contain.

+7
source

Source: https://habr.com/ru/post/1482435/


All Articles