C++ Weekly - Ep 267 - C++20's starts_with and ends_with

preview_player
Показать описание
☟☟ Awesome T-Shirts! Sponsors! Books! ☟☟

T-SHIRTS AVAILABLE!

WANT MORE JASON?

SUPPORT THE CHANNEL

GET INVOLVED

JASON'S BOOKS

► C++23 Best Practices

► C++ Best Practices

JASON'S PUZZLE BOOKS

► Object Lifetime Puzzlers Book 1

► Object Lifetime Puzzlers Book 2

► Object Lifetime Puzzlers Book 3

► Copy and Reference Puzzlers Book 1

► Copy and Reference Puzzlers Book 2

► Copy and Reference Puzzlers Book 3

► OpCode Puzzlers Book 1


RECOMMENDED BOOKS

AWESOME PROJECTS

O'Reilly VIDEOS

Рекомендации по теме
Комментарии
Автор

I kind of like cartoon Jason commenting on coding Jason.

matthiasrahlf
Автор

Wouldn't you use compare instead of find? Compare has an overload that allows you to specify place and length within the string to compare like so:

std::string test = "hello world";
bool startsWith = test.compare(0, 5, "hello") == 0;
bool endsWith = test.compare(test.size() - 5, 5, "world") == 0;
return startsWith && endsWith;

AusSkiller
Автор

Hey Jason, great videos (love cartoon Jason joining the video), thank you for spoiling even those of us fewer years of experience. One thing you haven't covered that might make for another short video is the different emplace methods (eg. emplace, emplace_hint and try_emplace for unordered maps and so on). The small (but useful) differences between these recently helped me out quite a bit, in a situation where I had to create multiple instances of a class with an unknown (potentially different) amount of parameters. I initially overlooked try_emplace completely, because of the "try_" naming, making me think of it as a "try-catch"-y thing.

olivermadsen
Автор

This would be really useful on string iterators for parsers, checking if the current iterator position starts_with a terminating delimiter. Though, if we create a new string or string_view from the iterator, then we add unnecessary memory and time costs (unless there is some other way I am not aware of). Thanks for showing this Jason!

elche
Автор

We use boost::starts_with, ends_with, and iequals a lot. Looks like iequals has not been ported to std yet.

somebodyyyyy
Автор

To make a good library, just examine the source code of many projects and see what common functions/classes their devs wrote

perfectionbox
Автор

This will definitely replace my use of just writing a for loop. I'm not sure why someone would be using the inefficient find version when they could've just used for loop before though (and extracted it into a function). Kind of a strange workaround pre-C++20 that makes no sense ngl

lincolnsand
Автор

That's why I call all my variables val0, val1, val2

aportabales
Автор

Definitely the old way is very annoying. A way to improve performance may be to take a substring of length "hello" from str, then search on that

kasserater
Автор

I really don't understand why they just can't add more string utility functions: contains, toUppercase, toLowerCase, isNumeric, ... .
Also why can't this startsWith be generalized for every container type?

akj
Автор

Regarding the ends with workaround with find: is there a constexpr function to determine the length of a string literal at compile time? Sometimes it would just be more expressive instead of just writing 3 like in this example.

muluman
Автор

A std::basic_string_view is almost the same thing as a std::span, right?

Tyranisaur
Автор

Jason, I think that with string_view it is unnecessary to specify "constexpr" because it is automatic. Is that true, or it is just a cleverness of the compiler? Thanks.

mariogalindoq
Автор

Why we declared constexpr there? Any specific reason? Please reply me :)

yigiterkal
Автор

I think the generic container version would just use std::equal:

std::string str{"hello world"};
std::string compare{"hello"};

bool startsWith{str.size() >= compare.size() && std::equal(compare.begin(), compare.end(), str)};
bool endsWith{str.size() >= compare.size() && std::equal(compare.begin(), compare.end(), str.end() - compare.length())};

KingMir
Автор

Your ends_with substitute has a bug. It wont work if the string you're looking for appears twice in the input string. Consider input "Hello world world".
Just a proof that we needed ends_with in the standard library =)
Edit: I didn't see the overlaying text added in post suggesting to use rfind.

flshi
Автор

I'm going out on a limb: by C++29 the language will have been transformed into a fully native version of C#/.NET. I think that's the plan, although they haven't told Bjarne yet... (and don't intend to!)

treyquattro
Автор

CPP becoming more like python. There's a mind bender for you

DrGreenGiant
Автор

all std containers' should have a .contains()

phantom_stnd
Автор

I think your implementation of ends_with has a bug. In case the string, in your example "rld", is in the string multiple times it would return false. You need to use rfind instead as that returns the last match in the string.

Shadow