Raven
Raven

Reputation: 41

Storing Data to Map

I have created a map with wstring as key on a struct for data but when i tried to run iterator for showing data of the map, it skips the first data that i added. Here is the code.

typedef struct
{
public:
wstring source;
wstring synthetic;
int operation;
int divisor;
}SSParams;

map<wstring, SSParams>SSParameters;

for(int x=0;x<sizeofdata;x++)
{
 SSParameters[srcsymbol[x]].source = srcsymbol[x];
 SSParameters[srcsymbol[x]].synthetic = synsymbol[x];
 SSParameters[srcsymbol[x]].operation = operation[x];
 SSParameters[srcsymbol[x]].divisor = divisor[x];
 m_api->LoggerOut(Log, L"Source: %s Synth: %s, Operation: %d, Value: %d, Total: %d", SSParameters[srcsymbol].source, SSParameters[srcsymbol].synthetic, SSParameters[srcsymbol].operation, SSParameters[srcsymbol].divisor,SSParameters.size());
}

map<wstring, SSParams>::iterator iter;
for (iter = SSParameters.begin(); iter != SSParameters.end(); ++iter)
{
    m_api->LoggerOut(Log, L"Data Source: %s, Synth: %s, Operation: %d, Divisor: %d", iter->second.source, iter->second.synthetic, iter->second.operation, iter->second.divisor);
}

Output (Insertion loop):

Source: HCBC Synth: HCBCx, Operation:1, Value: 100, Total: 1
Source: HCBC Synth: HCBCv, Operation:1, Value: 100, Total: 1

Output (Iterator loop):

Data Source: HCBC Synth: HCBCv, Operation:1, Value: 100

As you can see, i added values as well as the key to the map using for loop and the log shows that i added the data successfully but when i try to run the iterator, i shows the data but skip the first one.

Upvotes: 0

Views: 107

Answers (3)

sp2danny
sp2danny

Reputation: 7644

You can test that keys that are inserted are unique using:

for(int x=0;x<sizeofdata;x++)
{
    assert(SSParameters.count(srcsymbol[x])==0);
    SSParameters[srcsymbol[x]].source = srcsymbol[x];

requires <cassert>

Upvotes: 1

Petar Petrovic
Petar Petrovic

Reputation: 414

From the output of insert loop, srcsymbol[x] always equal to HCBC. Hence the program only added one element to the map.

Upvotes: 0

Jesper Juhl
Jesper Juhl

Reputation: 31457

Since srcsymbol does not change in the loop you are just adding one element and then continuously overwriting its contents. End result; the map holds only a single element.

Upvotes: 1

Related Questions