// There are several endianness/ordering issues to consider here. // * Machine endianness is irrelevant. (That affects how an unsigned int is stored // as a sequence of bytes. While all of our supported architectures are little-endian, // that's irrelevant as long as we avoid reinterpreting unsigned int as a sequence of bytes.) // * Appending bits to vector<bool> eventually appends words to its underlying storage. // For example, vb[10] is stored within vb._Myvec[0], while vb[100] is stored within vb._Myvec[3]. // This allows us to translate lexicographical comparisons from theoretical bits to physical words. // * Unsigned integers are written and compared as big-endian (most significant bit first). // For example, 0x10u > 0x07u. // * However, vector<bool> packs bits into words as little-endian (least significant bit first). // For example, vector<bool>{false, true, true, true} stores 0b0000'0000'0000'0000'0000'0000'0000'1110u. // We could bit-reverse words before comparing, but we just need to find the least significant bit that differs.
ניתן לראות שבמימוש שלהם הם משתמשים ב-unsigned int אשר מכיל 32 בתים. ולכן כל 32 איברים הוא ייקצה unsigned int נוסף ובכך אנחנו חוסכים הרבה מקום!
מה ניתן ללמוד מאופטימיזציות אלו:
ניתן לחסוך בזיכרון כשיכולים.
מימוש פרטני לאלגוריתמים גנריים מעניק יכולות חדשות מבלי לגרוע מהממשק.
כדי לממש אופטימציות צריך לצלול למימוש הספציפי - לא תמיד ניתן לעשות את זה באופן גנרי.
המימוש הגנרי של ווקטור לא אופטימלי לכל המקרים.
זהו עוד מקרה פרטני ב-CPP שהופך את השפה ואת ספריית הסטנדרט לעוצמתיות.