Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8002503
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:23:10+00:00 2026-06-04T16:23:10+00:00

i really dont get it: i am reading points, each holds 3 float values,

  • 0

i really dont get it:

i am reading points, each holds 3 float values, out of a binary file.
Saving this points in an unordered_map

therefore i try to create a key out of these 3 float values:

first intention:
just use the exact bits as key:

unordered_map<string, vector<float>> points;
string vecToKey( float* a ) {
char bytes[12];
memcpy(bytes, a, 12);
return string(bytes);
}

the point is that i definitely want to eleminate same points this way
but

in an example project reading about 21374 points
the map result size = 10640 points

using following method as key creation results in the proper result
of 10687 points

string vec3ToKey( float a[3] ) {
float a1[3];
a1[0] = a[0];
a1[1] = a[1];
a1[2] = a[2];
stringstream ss;
boost::archive::text_oarchive oa(ss);
oa << a1;
return ss.str();
}

the problem is the speed. second method needs about 16 seconds and first method just 1-2 seconds…
i just cant explane myself why there even is a difference …

i appreciate every idea 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T16:23:11+00:00Added an answer on June 4, 2026 at 4:23 pm
    string vecToKey( float* a ) {
      char bytes[12];
      memcpy(bytes, a, 12);
      return string(bytes);
    }
    

    The string constructor you’re using stops at the first null byte. Floating point values can contain null bytes. So the string is probably not accurately representing the three floats. You can see by sticking an assert in there:

      string s(bytes);
      assert(s.size() == sizeof bytes);
      return s;
    }
    

    Another problem is that bytes might not contain a null byte, and the program may copy random garbage into the string or otherwise exhibit undefined behavior.

    I would recommend that you just not try to abuse string this way. You want a key that’s three floats, so use a key that represents exactly that: std::array<float,3>. Or better yet use a ‘Point’ class since that’s what the three floats represent.

    Since there’s no built in hash function for arrays you can use something like this:

    // taken from http://stackoverflow.com/questions/6899392/generic-hash-function-for-all-stl-containers
    template <class T>
    inline void hash_combine(std::size_t & seed, const T & v)
    {
      std::hash<T> hasher;
      seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
    }
    
    struct Hasher {
        size_t operator() (std::array<float,3> const &v) {
            size_t h = std::hash<float>()(v[0]);
            hash_combine(h,v[1]);
            hash_combine(h,v[2]);
            return h;
        }
    };
    
    
    std::unordered_map<std::array<float,3>,vector<float>,Hasher> map;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Forgive me if this is a dumb beginners problem, but I really don't get
I keep getting this error and I really dont know why. I have tried
I dont really understand why this is not working, I am being returned to
I have seen some of the other answers on this topic but dont really
I already read this question about socket synchronization but I still dont get it
I really can't get my head around Activity, Intent and start.. even after reading
I don't really get the concept of bytecode interpreter in the context of CPython.
I want to copy an svg group but I really dont know what I
I am not a DBA and so dont really know anything about SQL 2005
im trying to achieve something but i dont really know how I have set

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.