Question:
The STL container classification graphic suggests that there is no STL container that is both variable size and heterogeneous. Does C lack a solution for storing data of different types?
Answer:
While C containers are typically designed to hold objects of a single type, there are ways to store different types of data:
Code Example:
<code class="cpp">#include <list> #include <boost/any.hpp> using boost::any_cast; typedef std::list<boost::any> many; int main() { many values; append_int(values, 10); append_string(values, "Hello"); if (is_int(values.front())) { int number = any_cast<int>(values.front()); std::cout << number << std::endl; } return 0; }</code>
The above is the detailed content of Can C Store Data of Different Types in a Single Container?. For more information, please follow other related articles on the PHP Chinese website!