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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:41:05+00:00 2026-05-27T06:41:05+00:00

How would I do something in c++ similar to the following code: //Lang: Java

  • 0

How would I do something in c++ similar to the following code:

//Lang: Java
string.replaceAll("  ", " ");

This code-snippet would replace all multiple spaces in a string with a single space.

  • 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-05-27T06:41:06+00:00Added an answer on May 27, 2026 at 6:41 am
    bool BothAreSpaces(char lhs, char rhs) { return (lhs == rhs) && (lhs == ' '); }
    
    std::string::iterator new_end = std::unique(str.begin(), str.end(), BothAreSpaces);
    str.erase(new_end, str.end());   
    

    How this works. The std::unique has two forms. The first form goes through a range and removes adjacent duplicates. So the string “abbaaabbbb” becomes “abab”. The second form, which I used, takes a predicate which should take two elements and return true if they should be considered duplicates. The function I wrote, BothAreSpaces, serves this purpose. It determines exactly what it’s name implies, that both of it’s parameters are spaces. So when combined with std::unique, duplicate adjacent spaces are removed.

    Just like std::remove and remove_if, std::unique doesn’t actually make the container smaller, it just moves elements at the end closer to the beginning. It returns an iterator to the new end of range so you can use that to call the erase function, which is a member function of the string class.

    Breaking it down, the erase function takes two parameters, a begin and an end iterator for a range to erase. For it’s first parameter I’m passing the return value of std::unique, because that’s where I want to start erasing. For it’s second parameter, I am passing the string’s end iterator.

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

Sidebar

Related Questions

I would do something similar to this in PHP: http://mysite.com/en/museum/gallery/garden http://mysite.com/es/museo/galeria/jardin It's possible? How
I would like to perform something similar to this (ie get the sum of
I'm doing something similar to the following code. I have already gone through AddtoStructFunction()
I'd like to create DetachedCriteria which would perform something similar to the following SQL
Consider the following code: java.util.Map<String, String> map = new java.util.HashMap<String, String>(); ... String key
Given the following xaml code, I would like to create this xaml code the
I have used getopt in Python and was hoping there would be something similar
I would like to implement something similar to a c# delegate method in PHP.
I would like to implement something similar to 37Signals's Yellow Fade effect. I am
I would like to achieve something similar to how scala defines Map as both

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.