0

I create a map with as map< wchar_t , Point3d> StationCoordinates.
I want to avoid the duplicate data in may map , so i check the key map by function ExistStation , but i does not work correctly.
here is my code. what is the problem in function ExistStation?

#include <map>
#include <iostream>

using namespace std;

struct Point3D
{
    double x, y, z;
};
std::map<const wchar_t *, Point3D>  StationCoordinates;

bool ExistStation(const wchar_t* StationName, Point3D & StationCoord)
{
    //std::map<wchar_t *, Point3D>::iterator res;
    auto    res = StationCoordinates.find(StationName);
    if (res != StationCoordinates.end())
    {
        StationCoord = res->second;
        return true;
    }
    return false;
};

void main()
{

    for (size_t j = 0; j < 4; j++)
        for (size_t i = 0; i < 20; i++)
        {
            wchar_t * mapid = new wchar_t[20];
            //wchar_t  mapid[20];
            swprintf_s((mapid), 20, L"ID%04i", i);

            Point3D po;
            if (ExistStation(mapid, po))
                continue;

            po.x = i * 100;
            po.y = i * 1000;
            po.z = i * 1000;

            StationCoordinates[mapid] = po;
        }

    for (auto k : StationCoordinates)
    {
        wcout << k.first << ":" << k.second.x << "," << k.second.y << "," << k.second.z << endl;
    }

    wcout <<"The size of the map is:"<< StationCoordinates.size() << endl;
}

the size of the map must be 20 but it is 80 .

SaeidMo7
  • 1,214
  • 15
  • 22

0 Answers0