Chunchunmaru 6 Posted October 5, 2017 Share Posted October 5, 2017 hi everyone, i use that with lua but i need that c++ code. How can i use that ? Lua : list = { [5] = { 110,123123,1,false,0}, [150] = { 1103,1232123,1,false,0}, [3500] = { 1120,123123,1,true,0}, [12] = { 1110,123123,1,false,0}, } i want use with c++ just like lua list[5][1] Link to comment Share on other sites More sharing options...
Developer Ikarus_ 2426 Posted October 6, 2017 Developer Share Posted October 6, 2017 First of all you should create the data structure you need, which Lua does automatically but the c++ no. struct MyStructData { int MyInt; BYTE MyByte; bool MyBool; char szName[24]; } Then you have to decide how you want to use this data. You can use them in a list (optimized if you have to remove and put members very often) You can use them in a vector (optimized for random access) You can use them in a map (optimized with key search -> value) example of list : struct MyStructData { int MyInt; BYTE MyByte; bool MyBool; char szName[24]; } std::list<MyStructData> MyList; //I declare a list and set it a type of data (in this case mystructdata) //i declare a new MyStructData and set it value MyStructData Example; Example.MyInt = 0; Example.MyByte = 0xFF; Example.MyBool = false; strncpy( Example.szName , "My string is this." , sizeof(Example.szName) ); //add my new data in list MyList.push_back( Example ); //remove last member inserted MyList.pop_back(); example of vector (same of list) struct MyStructData { int MyInt; BYTE MyByte; bool MyBool; char szName[24]; } std::list<MyStructData> MyVector; //I declare a vector and set it a type of data (in this case mystructdata) //i declare a new MyStructData and set it value MyStructData Example; Example.MyInt = 0; Example.MyByte = 0xFF; Example.MyBool = false; strncpy( Example.szName , "My string is this." , sizeof(Example.szName) ); //add my new data in vector MyVector.push_back( Example ); //remove last member inserted in vector MyVector.pop_back(); The different between list and vector is : List is efficient to add and remove member in list , is not efficient for access to members of the list . Vector is efficient to do the random access ( [5] , [100] , [150] ) Spoiler Example of access to member memory for vector and list : //if vector size > x : myVec[x].MyInt = 10; myVec[x].MyBool = true; //for list i need a iterator. std::list<MyStructData>::iterator it = MyList.begin(); //to declare a iterator the sintax is std::CONTEINER_TYPE<DATA_TYPE>::iterator MyList.begin() -> pointer to first member of list for( /* already declared it */ ; it != MyList.end() ; it++) //this for cicle scrolls the entire list { if(it->MyInt == 10) //use -> because it is a pointer { it->MyBool = true; } } Example of map: struct MyStructData { int MyInt; BYTE MyByte; bool MyBool; char szName[24]; } std::map<BYTE , MyStructData> MyMap; //I declare a map and set it a type of data and a type of key (in this case mystructdata and Byte) //i declare a new MyStructData and set it value MyStructData Example; Example.MyInt = 0; Example.MyByte = 0xFF; Example.MyBool = false; strncpy( Example.szName , "My string is this." , sizeof(Example.szName) ); //insert new member in map BYTE key = 0xFA; MyMap.insert( std::make_pair( key , Example ) ); // insert key 0xFA (aka 250 in decimal) with value Example //access to member MyMap[250].MyInt = 100; MyMap[250].MyBool = true; //access in member by key with iterator std::map<BYTE , MyStructData>::iterator it = MyMap.find(250); it->second.MyInt = 100; it->second.MyBool = true; //access to all member of map std::map<BYTE , MyStructData>::iterator another_it = MyMap.begin(); for( ; another_it != MyMap.end() ; another_it++) { if(another_it->second.MyInt != 100) { another_it->second.MyBool = false; } } There are many possibilities, you have to find the most useful one for your needs. You can understand more about it by enjoying STL Conteiner Bye Bye 1 My youtube channel on which you can see my works here Link to comment Share on other sites More sharing options...
Recommended Posts
Please sign in to comment
You will be able to leave a comment after signing in
Sign In Now