Containers in C + + can hold various data types at the same time

Source: Internet
Author: User

Original C + + makes a generic container (can hold various types of objects)

2014-5-4 Read 494 comments 0

If you want an object that can hold all kinds of objects, you can basically say no in C + +, or you can use vector<boost::any> or something else to simulate, I say that's not good. The problem is that my type will increase dynamically at runtime, and you won't know what type I'll add, and my header file won't give it to you.

Now you don't think C + + generics are used, yes, C + + generics are essentially reusing similar code, doing things the same thing, but only dealing with types of differences. This situation is still relatively small, such as vector,queue,map and other containers are the best place to use generics. But did you ever think that these types of objects are always one, that is vector<t>, queue<t> I want a type that can hold a variety of objects, TA,TB, ... What to do?

Think about it. I think of the mandatory type of C language loaded Ta ta; (TB) Ta; this seemingly generic thing, in fact, is very powerful, he is a C + + reinterpret_cast prototype, its function is to any memory address of the content of the re-interpretation. In other words, no matter what your memory is, I'll force it to explain what kind of object it is.

So, in fact, I just need a string to save any type of object, right?

Then vector<string> is the container of this generic type. Where do you store that type? Remember Zhu always said: The statute is larger than the configuration, you can use the int type (char) 1 tag, the TA type (char) 2 mark, and so on, you can customize the type you intend to put in the ID number of the container, when you take the first to determine the number of what type of object. is not very hi leather, I do not know some people are not disdain, or say C + + has a perfect solution and I do not know, welcome criticism.

You should put the object in a container like this:

String String_record ();
String_record.append (String (char*) &data1, (char*) &data1 + sizeof (e_eledata));

You will take out your object and use:

cout<< "type:" << (int) data_array1[i][0]<<endl;
cout<< "message:" <<* (e_eledata*) (Data_array1[i].c_str () +1) <<endl;
string S;
Get_decode ((int) data_array1[i][0])->decode (Data_array1[i].substr (1,data_array1[i].size ()-1), s);

These are just a few, welcome to the experts to give a better way

The idea source of this article: Thank colleague Chuling gives the use of less or unified into a string type to represent generics; thanks to Chen Fan streaming platform provides string as the data type of the message data to complete a variety of data upload and download; Thanks to Zhu's "statute greater than configuration" Thank the Chen Lin Engineer for the whole idea; thank Zhu Yongjin for C's dedication let me know that character arrays can actually represent various types

Containers in C + + can hold various data types at the same time

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.