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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
// a variable, a string if the next word is "string" a int if its "number"
// we could use a union-like-class here, but we will keep it simple for now
struct variable // string or number
{
enum var_type { STRING, INT };
var_type type ; // the type of the variable
std::string str ; // we'll use this member if type==STRING; ignore it otherwise
int number ; // we'll use this member if type==INT; ignore it otherwise
// for convenience, we will write a pair of constructors
variable( int i = 0 ) : type(INT), number(i) {} ; // construct with a number, also the default constructor
variable( const std::string& s ) : type(STRING), str(s) {} // construct with a string
};
// we need to keep track of the names and values of variables.
// a map where the key is the name of the variable, and the mapped value is the variable itself
// for convenience, we will use a type-alias
using symbol_table = std::map< std::string, variable > ;
// my code successfully converts it to set, string, test, "test is populated"
// and now i want the code to do this: string test = "test is populated";
void set_value( symbol_table& sym_tab, const std::string& varable_name, const std::string& str_value )
{
// look up the variable_name, if present update it to STRING with value str_value
// if the variable_name is not found, create it and set the value to STRING, str_value
sym_tab[varable_name] = str_value ; // note: converting constructor variable( const std::string& )
}
// and another function to set INT values
void set_value( symbol_table& sym_tab, const std::string& varable_name, int int_value )
{
sym_tab[varable_name] = int_value ; // note: converting constructor variable(int)
}
// helper functions to print out the contents of the symbol_table
// first, a function to print out a single variable
std::ostream& operator<< ( std::ostream& stm, const variable& var )
{
if( var.type == variable::INT ) return stm << "type: INT value: " << var.number ;
else return stm << "type: STRING value: " << std::quoted(var.str) ;
}
// print symbol table
std::ostream& operator<< ( std::ostream& stm, const symbol_table& sym_tab )
{
for( const auto& pair : sym_tab ) // for each name-variable pair in sym_tab
{
stm << "name: " << std::quoted(pair.first) << ' ' // print out its name
<< pair.second << '\n' ; // and its value
}
return stm ;
}
int main()
{
symbol_table sym_tab ;
// set, string, test, "test is populated"
set_value( sym_tab, "test", "test is populated" ) ;
// set, number, count, "73"
set_value( sym_tab, "count", std::stoi("73") ) ;
set_value( sym_tab, "name", "modkip" ) ; // set, string, name, "modkip"
set_value( sym_tab, "num_posts", std::stoi("2") ) ; // set, number, num_posts, "2"
// print out the contents of sym_tab
std::cout << sym_tab << '\n' ;
// change the value of test to "this is the new value"
set_value( sym_tab, "test", "this is the new value" ) ;
// change the type and value of variable "count" to string, "one million"
set_value( sym_tab, "count", "one million" ) ;
// print out the contents of sym_tab
std::cout << "------------------\n" << sym_tab << '\n' ;
}
|