C ++ is equivalent to $ obj & # 8594; {$ var}

I am looking for the C ++ equivalent for the following PHP code

$obj = new stdClass();
$obj->test = "aaaa";
$var = "test";
echo $obj->{$var};

Maybe even in C? I keep looking for a watch and no luck.

thanks

+4
source share
1 answer

Try:

#include <unordered_map>
#include <iostream>
#include <string>

using namespace std;

int main() {
    unordered_map<string, string> obj;
    obj["test"] = "aaaa";
    string var = "test";
    cout << obj[var] << endl;
}

This is not exactly the same as the test string in both cases. If the difference between a “test” and a simple test is important, then the answer becomes a little more complicated.

See also: How to choose a map and unordered_map? to explain the discussion in the comments.

+6
source

Source: https://habr.com/ru/post/1615474/


All Articles