DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Data Engineering
  3. Data
  4. Let's Talk About String Operations in C++17

Let's Talk About String Operations in C++17

A software developer provides a quick but informative post on working with string operations in C++17 abd takes a look at some code.

Bartłomiej Filipek user avatar by
Bartłomiej Filipek
·
Oct. 16, 18 · Tutorial
Like (1)
Save
Tweet
Share
6.30K Views

Join the DZone community and get the full member experience.

Join For Free

In September, our local C++ User Group started a "new year" of meetings after a little break in August. I had a pleasure to give a talk about string operations in C++17.

The Talk

For my book, I wrote a lot of content about string_view, std::searcher and std::to_chars, std::from_chars and I wanted to give a short summary of those features.

In the talk I included some of my benchmarks and notes that were covered in my blog, for example:

  • Preprocessing Phase for C++17’s Searchers
  • Speeding up Pattern Searches with Boyer-Moore Algorithm from C++17
  • Speeding Up string_view String Split Implementation
  • Performance of std::string_view vs std::string from C++17

Most of the time we spent discussing string_view as this feature might have a bigger impact on your code. std::searcher and low-level conversion routines are quite specialized so they won't be used that often as views.

For example, during the discussion, we shared the experience using string views. One case is that when you refactor some existing code you'll often find a situation where you can use views through chains of function calls, but then, at some point, you're stuck as you have to perform conversion to string anyway.

Another thing, that was brought up by Andrzej Krzemieński (from Andrzej's C++ Blog), is that while string_view is supposed not to allocate any extra memory, you should be still prepared for memory allocations for exceptions.

Have a look at this code:

#include <iostream>
#include <stdexcept>
#include <string_view>

void* operator new(std::size_t n)
{
    std::cout << "new() " << n << " bytes\n";
    return malloc(n);
}

int main()
{
    std::string_view str_view("abcdef");

    try {
        for (std::size_t i = 0; true; ++i)
            std::cout << i << ": " << str_view.at(i) << '\n';
    }
    catch (const std::out_of_range& e) {
        std::cout << "Whooops. Index is out of range.\n";
        std::cout << e.what() << '\n';
    }
}

Play @Coliru

The code uses str_view.at(i) that can throw when trying to access an index out of range. When an exception is created, you'll see some memory allocation — for the message string.
It's probably not a super common use case to use at, but it's an interesting observation.

The Slides

from Bartlomiej Filipek

Summary

The talk was my third presentation for the Krakow User Group. It's an amazing experience, and I hope to be able to deliver more good stuff in the future!

What is your experience with string views, searchers and low-level conversion routines? Have you played with the new features?

Data Types Strings c++

Published at DZone with permission of Bartłomiej Filipek, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Public Cloud-to-Cloud Repatriation Trend
  • Last Chance To Take the DZone 2023 DevOps Survey and Win $250! [Closes on 1/25 at 8 AM]
  • The Role of Data Governance in Data Strategy: Part II
  • Exploring the Benefits of Cloud Computing: From IaaS, PaaS, SaaS to Google Cloud, AWS, and Microsoft

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: