0

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)?

3
  • can't understand what's you problem
    – Jimmy Guo
    Commented Dec 14, 2020 at 6:03
  • There are two questions here. 1). How to create and use array of strings inside the Struct (std::vector<char>) 2).Is the above way of coding correct. Commented Dec 14, 2020 at 10:25
  • You can use 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.
    – conr2d
    Commented Jan 6, 2021 at 18:09

1 Answer 1

0

You can store an array of strings by using std::vector< std::string >.

If you want a vector of chars, then the code you have written looks fine.

You can verify that the data was serialized correctly by looking at the ABI file after compilation to make sure that the struct has the appropriate data in it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.