C++ boost::vector














































C++ boost::vector



Description:The standard library contains many types of containers holding same data type like a vector contains only int if <int> is used char if <char> is used and same for the rest. But Boost.Fusion makes it possible to store different data types in a single container. Ex - a vector can be created that will hold int in its first index, double in its second index or char in its first index and string in its second index etc.
Vector is same as tuple the basic difference is that it can be accessed directly by index rather than complete iteration hence taking O(1) time complexity unlike tuple which is O(n).

Code:

#include <boost/fusion/container.hpp> #include <boost/fusion/sequence.hpp> #include <boost/mpl/int.hpp> #include <string> #include <iostream> using namespace boost::fusion; int main() { typedef vector<int, std::string, bool, double> vector_type; vector_type v{4, "Vector", true, 3.14}; std::cout << at<boost::mpl::int_<0>>(v) << '\n'; auto v2 = push_back(v, 'B'); std::cout << size(v2) << '\n'; std::cout << back(v2) << '\n'; }

Output:
4
4
B

Comments