Of course, you can choose one method of C++ likes reinterpret which is described in below topic:
https://www.geeksforgeeks.org/reinterpret_cast-in-c-type-casting-operators/
However, it is easily to find out one big problem in here is:
reinterpret_cast is a very special and dangerous type of casting operator
In embedded system, this problem is not allowed. Then, we should convert it by manually.
The idea is, you push each structure to vector by passing number of structure to function which is used for converting.
https://www.geeksforgeeks.org/reinterpret_cast-in-c-type-casting-operators/
However, it is easily to find out one big problem in here is:
reinterpret_cast is a very special and dangerous type of casting operator
In embedded system, this problem is not allowed. Then, we should convert it by manually.
The idea is, you push each structure to vector by passing number of structure to function which is used for converting.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | #include <iostream> #include <list> #include <vector> #include <string.h> using namespace std; #define STR_NUM 3 //Number of structures typedef struct { char text_id[16]; char name[16]; }st_A; /* Show vector data after convert */ void vector_show( std::vector<st_A> pvIn) { int size = pvIn.size(); cout << "Number of structure in vector:" <<size << endl; for(int i = 0; i<size; i++) { cout << pvIn[i].text_id; cout << pvIn[i].name << endl; } } /* Convert from pointer of structure to vector with given number of structure */ void convert_func( st_A *psIn, int st_no) { std::vector<st_A> stTemp; for(int i = 0; i<st_no; i++) { stTemp.push_back(psIn[i]); } vector_show(stTemp); } /* It is main */ int main() { //Declare structure with 10 members st_A stA_in[STR_NUM]; //Set value for all member as example strcpy(stA_in[0].text_id,"MrPres 0:"); strcpy(stA_in[0].name,"Trumpy"); strcpy(stA_in[1].text_id,"MrPres 1:"); strcpy(stA_in[1].name,"Putty"); strcpy(stA_in[2].text_id,"MrPres 2:"); strcpy(stA_in[2].name,"Xiy"); //Convert structure to vector convert_func(stA_in,STR_NUM); while (true); } |
Comments
Post a Comment