indirection

How to get the keys and values of an associative array indirectly in Bash?

百般思念 提交于 2019-12-08 04:28:23
问题 In Bash, given only a variable that contains the name of an associative array, $ declare -A dict=([abc]=125 [def]=456) $ dictvar="dict" how can we retrieve the keys and values of the associative array? 回答1: In Bash, to get keys of an associative array via indirection, given the name of the array in variable dictvar one can leverage declare or local (original source): $ declare -a 'keys=("${!'"$dictvar"'[@]}")' # or 'local' Then, to get the values $ for key in ${keys[@]}; do $ value_var="$

Is There a Indirection Functor?

别说谁变了你拦得住时间么 提交于 2019-12-07 00:28:27
问题 I'm looking for a unary functor which will dereference it's argument and return the result. Of course I can write one, it just seemed like something should already exist. So given the code: const auto vals = { 0, 1, 2, 3 }; vector<const int*> test(size(vals), nullptr); iota(begin(test), end(test), data(vals)); transform(cbegin(test), cend(test), ostream_iterator<int>(cout, " "), [](const auto& i){ return *i; }); Live Example I was hoping that there was a functor that I could use instead of

Is There a Indirection Functor?

无人久伴 提交于 2019-12-05 05:47:50
I'm looking for a unary functor which will dereference it's argument and return the result. Of course I can write one, it just seemed like something should already exist. So given the code: const auto vals = { 0, 1, 2, 3 }; vector<const int*> test(size(vals), nullptr); iota(begin(test), end(test), data(vals)); transform(cbegin(test), cend(test), ostream_iterator<int>(cout, " "), [](const auto& i){ return *i; }); Live Example I was hoping that there was a functor that I could use instead of the lambda. Does such a thing exist, or do I need to just use the lambda? Assuming that by "functor" you

Is there a convention for pointer declarations in C? [closed]

 ̄綄美尐妖づ 提交于 2019-12-04 10:10:10
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . When declaring pointers in C, there are 3 variants: Variant A: int* ptr; Variant B: int *ptr; Variant C: int * ptr; In A, the

Does the C preprocessor remove instances of “&*”?

空扰寡人 提交于 2019-12-03 14:42:47
问题 I was playing around with gcc and tried the following bit of code: int A = 42; int *B = &A; int *C = &*B; And C == &A , as expected. But when I try: int *B = NULL; int *C = &*B; Turns out C == NULL , and no segfault. So &*B is not actually dereferencing B before taking its address. My guess is that the preprocessor is stripping out instances of &* and *& before they even get to the compiler since they negate each other, but I can't find any documentation to verify whether this is standard C

Does the C preprocessor remove instances of “&*”?

自古美人都是妖i 提交于 2019-12-03 04:31:21
I was playing around with gcc and tried the following bit of code: int A = 42; int *B = &A; int *C = &*B; And C == &A , as expected. But when I try: int *B = NULL; int *C = &*B; Turns out C == NULL , and no segfault. So &*B is not actually dereferencing B before taking its address. My guess is that the preprocessor is stripping out instances of &* and *& before they even get to the compiler since they negate each other, but I can't find any documentation to verify whether this is standard C or compiler-specific. Is the preprocessor stripping out &* and *& , and can I expect this behavior from

Accessing variables indirectly [duplicate]

老子叫甜甜 提交于 2019-12-02 13:12:06
问题 This question already has answers here : Dynamically access object property using variable (13 answers) Closed 4 years ago . Within my code (javascript in a firefox extension), i have a list of some variables, like this: var myApp = { var1: true, var2: false, var3: true, var4: false }; I want to access these variables to get their value indirectly using a function: var myApp = { var1: true, var2: false, var3: true, var4: false, varGetter: function(aName) { // code return myApp.aName.value; }

Accessing variables indirectly [duplicate]

北战南征 提交于 2019-12-02 04:51:24
This question already has an answer here: Dynamically access object property using variable 13 answers Within my code (javascript in a firefox extension), i have a list of some variables, like this: var myApp = { var1: true, var2: false, var3: true, var4: false }; I want to access these variables to get their value indirectly using a function: var myApp = { var1: true, var2: false, var3: true, var4: false, varGetter: function(aName) { // code return myApp.aName.value; } }; I call this function like this for example: if(myApp.varGetter("var2")) {alert("true")}; Now, how this function can be

Javascript - set a variable using concatenation of strings [duplicate]

心已入冬 提交于 2019-12-01 05:47:51
This question already has an answer here: Use dynamic variable names in JavaScript 15 answers Is it possible to set a variable by concatenating two strings together to form the name? If at all possible I'd like to determine what variable to set based on the class names of the objects that the user clicks. I know I can hard code a bunch of if/else if statements, but it would be really cool if I could reference the variables indirectly. I was thinking something like this: var owner_read; var group_read; function setVariableIndirectly(object){ var second = object.className; // returns "read" var

How to overload the indirection operator? (C++)

霸气de小男生 提交于 2019-12-01 03:37:47
I'm trying to create an iterator class as a member-class for a list class, and am trying to overload the indirection operator (*) to access the list it's pointing to: template<class T> T list<T>::iterator::operator*(iterator& iter) { return ((iter.lstptr)->current)->data; } where lstptr is a pointer to a list, current is a pointer to a node class, and the node class contains the data member data of type T . Iterator is declared like this: template<class T> class list { public: class iterator; }; template<class T> class list<T>::iterator { //stuff }; I am able to compile the function definition