My Sample Struct and its method
// Department Table
struct [[eosio::table]] department_info {
uint32_t ID;
std::string deptname;
std::string deptcode;
std::string teamlead;
std::vector<char> services;
auto primary_key() const { return ID; }
EOSLIB_SERIALIZE( department_info, (ID)(deptname)(deptcode)(teamlead)(services))
};
typedef eosio::multi_index<name("departments"), department_info> departments_table;
departments_table _departments;
[[eosio::action]]
void setdept(uint32_t ID, std::string deptname, std::string deptcode, std::string teamlead, std::vector<char> services);
// DEPARTMENT
void marketplays::setdept(uint32_t deptID, std::string departname, std::string departcode, std::string departteamlead, std::vector<char> departservices) {
require_auth( get_self() );
auto dept_iterator = _departments.find(deptID);
if ( dept_iterator == _departments.end() ) {
dept_iterator = _departments.emplace( get_self(), [&](auto& new_department) {
new_department.ID = deptID;
new_department.deptname = departname;
new_department.deptcode = departcode;
new_department.teamlead = departteamlead;
new_department.services = departservices;
});
}
}
void marketplays::getdept(uint32_t deptID) {
require_auth( get_self() );
auto dept_iterator = _departments.find(deptID);
if ( dept_iterator != _departments.end() )
{
print(dept_iterator->ID, ' ');
print(dept_iterator->deptname, ' ');
print(dept_iterator->deptcode, ' ');
print(dept_iterator->teamlead, ' ');
print(dept_iterator->services, ' ');
}
else {
print( "Department Not Found" );
}
}
How to create and use array of strings inside the Struct (std::vector)?
std::vector<std::string>
. BTW, storing many duplicated strings in contract will consume much memory. I don't it is a very good idea, unless you need to fetch data from chain directly.