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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:36:05+00:00 2026-05-11T05:36:05+00:00

The following string of mine tried to find difference between two strings. But it’s

  • 0

The following string of mine tried to find difference between two strings. But it’s horribly slow as it iterate the length of string:

#include <string> #include <vector> #include <iostream> using namespace std;   int hd(string s1, string s2) {     // hd stands for 'Hamming Distance'     int dif = 0;      for (unsigned i = 0; i < s1.size(); i++ ) {         string b1 = s1.substr(i,1);         string b2 = s2.substr(i,1);          if (b1 != b2) {             dif++;         }     }        return dif; }  int main() {      string string1 = 'AAAAA';     string string2 = 'ATATT';     string string3 = 'AAAAA';      int theHD12 = hd(string1,string2);     cout << theHD12 << endl;      int theHD13 = hd(string1,string3);     cout << theHD13 << endl; } 

Is there a fast alternative to do that? In Perl we can have the following approach:

sub hd {     return ($_[0] ^ $_[1]) =~ tr/\001-\255//; } 

which is much2 faster than iterating the position.

I wonder what’s the equivalent of it in C++?

  • 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. 2026-05-11T05:36:05+00:00Added an answer on May 11, 2026 at 5:36 am

    Fun with the STL:

    #include <numeric>    //inner_product #include <functional> //plus, equal_to, not2 #include <string>    #include <stdexcept>  unsigned int  hd(const std::string& s1, const std::string& s2) {     // TODO: What should we do if s1.size() != s2.size()?     if (s1.size() != s2.size()){       throw std::invalid_argument(           'Strings passed to hd() must have the same lenght'       );     }      return std::inner_product(         s1.begin(), s1.end(), s2.begin(),          0, std::plus<unsigned int>(),         std::not2(std::equal_to<std::string::value_type>())     ); } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I couldn't find a question that was quite like mine, but if you can
I have the following string: cn=abcd,cn=groups,dc=domain,dc=com Can a regular expression be used here to
Suppose you have the following string: white sand, tall waves, warm sun It's easy
I have the following string and I would like to remove <bpt *>*</bpt> and
Take the following string as an example: The quick brown fox Right now the
Say I have the following string: I am the most foo h4ck3r ever!! I'm
I have the following string expression in a PowerShell script: select count(*) cnt from
If I have the following string: string s = abcdefghab; Then how do I
Suppose I have following string: String asd = this is test ass this is
I have following string String str = replace :) :) with some other string;

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.