C++ string management convert raw string through dictionary matching -


what code given string, through array attached @ bottom if string matches 1 of array element, new bits of string combined form new string , gets returned! in other words, converts long , raw string looking in "dictionary" return interpreted new strnig.

i trying add code @ // comment if encounter raw strings don't match on record, in array, call function deal it. thinking logging can updated dictionary.

thanks!

string string_look_up(string data) { string loc_string = "";  std::stringstream ss(data);  std::string line; while (std::getline(ss, line, '0')) {     if (line.empty())     {         continue;     }      cout << line << endl;     (int = 0; < 82; i++)     {         if (line == dictionary_array_strings_raw[i])         {             loc_string = loc_string + dictionary_array_strings_digits[i];         }      }      /// new code want should      cout << loc_string << endl;     cout << "######" << endl; }  return loc_string; }  const string dictionary_array_strings_raw[] = { // x                                                 "25643663",                                                 // y                                                 "2346442", "2446442",                                                 // z                                                 "3676764",                                                 // :                                                 "4",                                                 // -                                                 "111" }  const string dictionary_array_strings_digits[] = { // x                                                    "x",                                                    // y                                                    "y", "y",                                                    // z                                                    "z",                                                    // :                                                    ":",                                                    // -                                                    "-",                                                    // 1  } 

you can use std::find locate raw string within array. index digits array subtract begin of array iterator returned std::find.

std::find requires 2 iterators (begin , end) search range. plain pointers can used iterators (because support incrementing (++) , dereferencing (*)). return value either interator pointing first element found or end-iterator pointing end (one past last element).

the std::function parameter allows specify callback lines not found in raw string array. can use function or lambda matches signature (const std::string& parameter , std::string return value).

warning: following code not correct. uses 82 number of dictionary entries (like code original post) defines 6 dictionary entries. result in undefined behaviour if using unknown raw strings. should add 76 dictionary entries or reduce entry-count 82 6.

#include <string> #include <map> #include <sstream> #include <algorithm> #include <functional> #include <iostream> #include <cassert>  using namespace std;  const string dictionary_array_strings_raw[] = { // x     "25643663",     // y     "2346442", "2446442",     // z     "3676764",     // :     "4",     // -     "111" };  const string dictionary_array_strings_digits[] = { // x     "x",     // y     "y", "y",     // z     "z",     // :     ":",     // -     "-",     // 1   };  string string_look_up(string data, std::function<std::string(const std::string&)> callback) {     assert(callback != nullptr);      const auto dictionary_array_strings_raw_end = dictionary_array_strings_raw + 82;      std::string loc_string = "";      std::stringstream ss(data);      std::string line;     while (std::getline(ss, line, '0'))     {         if (line.empty())         {             continue;         }          cout << line << endl;         const auto = std::find(dictionary_array_strings_raw, dictionary_array_strings_raw_end, line);          if (it == dictionary_array_strings_raw_end)             loc_string += callback(line);         else             loc_string += dictionary_array_strings_digits[it - dictionary_array_strings_raw];          cout << loc_string << endl;         cout << "######" << endl;     }      return loc_string; } 

the following example shows use of lambda unknown raw strings ('99' in example).

int main(int argc, char **argv) {     const auto result = string_look_up("25643663023464420990", [](const std::string& line) { return '<' + line + '>'; });     std::cout << "result:\n" << result;      return 0; } 

when string_look_up encounters line containing '99' tries find matching raw-string. when not find matching raw string calls callback-function "99" parameter. lambda defined in main adds '<' before , '>' after line value , returns result string_look_up() adds return value result.

the input 25643663023464420990 results in output xy<99> (and lot of debug output)

i recommend using std::map translation table (raw strings keys, digits values). map use find() member function iterator (let's call 'it') , it->second digits string.

here example static initialization of map:

const std::map<std::string, std::string> translationtable = {     {"25643663", "x"},     {"2346442", "y"},     {"2446442", "y" }     // ... }; 

you can search value const auto = translationtable.find(line); , compare translationtable.end() check if search successfull. it->first contains raw string (i.e. "25643663") while it->second contains digits string (i.e. "x")


Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -