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 8663349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:58:02+00:00 2026-06-12T16:58:02+00:00

I’d like to compare STL strings that are allocated with different allocators , e.g.

  • 0

I’d like to compare STL strings that are allocated with different allocators, e.g. an ordinary std::string with a string using a custom STL allocator. Unfortunately, it seems that usual operator==() doesn’t work in this case:

// Custom STL allocator to allocate char's for string class
typedef MyAllocator<char> MyCharAllocator;

// Define an instance of this allocator
MyCharAllocator myAlloc;

// An STL string with custom allocator
typedef std::basic_string
<
    char, 
    std::char_traits<char>, 
    MyCharAllocator
> 
CustomAllocString;

std::string s1("Hello");
CustomAllocString s2("Hello", myAlloc);

if (s1 == s2)  // <--- ERROR: doesn't compile
   ...

In particular, MSVC10 (VS2010 SP1) emits the following error message:

error C2678: binary ‘==’ : no operator found which takes a left-hand
operand of type ‘std::string’ (or there is no acceptable conversion)

So, a lower-level (less readable) code like this:

if (strcmp(s1.c_str(), s2.c_str()) == 0)
   ...

should be used.

(This is also particularly annoying in cases where e.g. there are std::vector‘s of differently-allocated strings, where the usual simple v[i] == w[j] syntax can’t be used.)

This doesn’t seem very good to me, since a custom allocator changes the way string memory is requested, but the interface of a string class (including comparison with operator==()) is independent from the particular way a string allocates its memory.

Is there something I am missing here?
Is it possible to keep the C++ high-level interface and operator overloads in this case?

  • 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-12T16:58:05+00:00Added an answer on June 12, 2026 at 4:58 pm

    Use std::lexicographical_compare for less-than comparison:

    bool const lt = std::lexicographical_compare(s1.begin(), s1.end(),
                                                 s2.begin(), s2.end());
    

    For equality comparison you can use std::equal:

    bool const e = s1.length() == s2.length() &&
                   std::equal(s1.begin(), s1.end(), s2.begin());
    

    Alternatively, you can just fall back on strcmp (or actually memcmp, since that has the correct seman­tics; remember that the C++ string is more general than a C string), as you suggested, which can poten­tially employ some lower-level magic like comparing an entire machine word at a time (though the above algorithm may also be specialized thus). Measure and compare, I’d say. For short strings, the standard library algorithms are at least nicely self-descriptive.


    Based on @Dietmar’s idea below, you could wrap those functions into a templated overload:

    #include <string>
    #include <algorithm>
    
    template <typename TChar,
              typename TTraits1, typename TAlloc1,
              typename TTraits2, typename TAlloc2>
    bool operator==(std::basic_string<TChar, TTraits1, TAlloc1> const & s1,
                    std::basic_string<TChar, TTraits2, TAlloc2> const & s2)
    {
        return s1.length() == s2.length() &&
               std::equal(s1.begin(), s1.end(), s2.begin());
    }
    

    Usage example:

    #include <ext/malloc_allocator.h>
    int main()
    {
        std::string a("hello");
        std::basic_string<char, std::char_traits<char>, __gnu_cxx::malloc_allocator<char>> b("hello");
        return a == b;
    }
    

    In fact, you could define such an overload for most standard containers. You could even template it on a template, but that would be extreme.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
For some reason, after submitting a string like this Jack’s Spindle from a text
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I would like to count the length of a string with PHP. The string
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I have a small JavaScript validation script that validates inputs based on Regex. I
I want to count how many characters a certain string has in PHP, but
link Im having trouble converting the html entites into html characters, (&# 8217;) i

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.