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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T09:50:40+00:00 2026-05-26T09:50:40+00:00

I want to trim a string in C++ with this code: std::string str( Trim

  • 0

I want to trim a string in C++ with this code:

std::string str("  Trim test   ");
str.erase(                                 /* 1 */
          0,                               /* 2 */
          str.find_first_not_of(" ")       /* 3 */
         )                                 /* 4 */
   .erase(                                 /* 5 */
          str.find_last_not_of(" ") + 1,   /* 6 */
          std::string::npos                /* 7 */
         );                                /* 8 */

Does the standard allow line #6 to be calculated before line #1 is executed, so that when #5 is finally called, the argument might no longer be valid?

  • 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-26T09:50:41+00:00Added an answer on May 26, 2026 at 9:50 am

    Yes, the standard allow line #6 to be calculated before line #1 is executed.

    From the Wikipedia page on sequence points:

    [Sequence points occur] Before a function is entered in a function call. The order in which the arguments are evaluated is not specified, but this sequence point means that all of their side effects are complete before the function is entered. In the expression f(i++) + g(j++) + h(k++), f is called with a parameter of the original value of i, but i is incremented before entering the body of f. Similarly, j and k are updated before entering g and h respectively. However, it is not specified in which order f(), g(), h() are executed, nor in which order i, j, k are incremented. Variables j and k in the body of f may or may not have been already incremented. Note that a function call f(a,b,c) is not a use of the comma operator and the order of evaluation for a, b, and c is unspecified.

    You should rewrite it as

    str.erase(0, str.find_first_not_of(" "));           
    
    str.erase(str.find_last_not_of(" ") + 1, std::string::npos);     
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to trim an exception message with the below code: Response.Redirect(IllegalCharactersError.aspx?error=); string message
I need to Trim a String . But I want to remove all the
I want to extract variables from math expression using c#. I wrote this code
I want to trim trailing whitespace at the end of all XHTML paragraphs. I
I want to trim the column headers of a CSV file, fields below is
I have a html file that I want to trim. I want to remove
I have a table with 7M records I want to trim down to 10k
I want to mimic the trim behavior of Photoshop (crop the area that is
I want to be able to trim images, many of which are very long
want to know why String behaves like value type while using ==. String s1

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.