Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

"Indirect" Addressing - Part 2

18 views
Skip to first unread message

Mike Copeland

unread,
Mar 9, 2017, 6:06:31 PM3/9/17
to
Some basic problems have been solved, but the "root issue" remains.
That is, I need to populate a structure (i.e. std::map) that contains a
pointer to an integer value that can be changed. For example,
#include <map>
#include <string>
int main()
{
int BNoffset = 42;
map<std::string, int> csvMap2;
map<std::string, int>::iterator csvIt;
csvMap2["BIB"] = BNoffset;
csvMap2["NO."] = BNoffset;
csvIt = csvMap2.find("BIB");
if(csvIt != csvMap2.end())
{
csvIt->second = 17;
}
return;
}
The program starts with "BNoffset" establishing a (default) value.
After I populate the std::map, I will need to change that variable's
value - to something I calculate. My dilemma is that I can't declare
the map object's type (I want pass-by-reference?) during
execution...because I can't find a way to declare and use the object's
"second" component. The code compiles, but it doesn't alter the
_contents_ of "BNoffset", which is my goal.
In essence, I don't know how to declare and use pointer addressing in
the way I want. Is it possible? If so, please explain how. TIA




---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

Manfred

unread,
Mar 9, 2017, 6:23:22 PM3/9/17
to
map<std::string, int*> csvMap2;
map<std::string, int*>::iterator csvIt;
csvMap2["BIB"] = &BNoffset;
csvMap2["NO."] = &BNoffset;
csvIt = csvMap2.find("BIB");
if(csvIt != csvMap2.end())
{
*(csvIt->second) = 17;
}
return 0;
}

This would do what seems to be what you are asking here, although it is
not clear if this is what you were looking for in the original post.

Alf P. Steinbach

unread,
Mar 9, 2017, 11:43:42 PM3/9/17
to
Mandred has posted a solution to the technical you're asking about, how
to map from string to pointer.

But possibly you're missing out on the idea of /identifying/ the columns
with numbers, inside your program.

For example, if the BN column has id 3, and if you have an array of
integers called `offsets`, then the array item

offsets[3]

would be your BNoffset variable: you would use offsets[3] where you now
use the individual BNoffset variable directly.

The nice thing about it is that with the array you don't have to refer
to BNoffset by name or by pointer. You can refer to it by id.

So, when you scan a CSV file and find the name "BIB" in header column
42, say, then you can look in your `map` and find that "BIB" maps to
column id 3, and then you can set `offsets[3]` to 42.

Whatever the further processing is, likely it involves looping over
columns. That's easy to do with the id and offset array approach. More
difficult with individually named offset variables.


Cheers!,

- Alf

0 new messages