STL std::string_view and when to use it versus std::string | Modern Cpp Series Ep. 113

  Рет қаралды 6,057

Mike Shah

Mike Shah

Жыл бұрын

►Full C++ Series Playlist: • The C++ Programming La...
►Find full courses on: courses.mshah.io/
►Join as Member to Support the channel: / @mikeshah
►Lesson Description: In this lesson I show you how to use STL string_view to provide a faster way to access strings (especially when only performing read-only operations).
►KZfaq Channel: / mikeshah
►Please like and subscribe to help the channel!
►Join our free community: courses.mshah.io/communities/...

Пікірлер: 38
@qcnck2776
@qcnck2776 Жыл бұрын
Thanks for using the term "pass by copy", which really makes so much more sense than saying "pass by value". It explicitly states what is being passed. And it may be even better to say "pass a copy" to the function, and "pass a reference".😁
@MikeShah
@MikeShah Жыл бұрын
Cheers, I also like pass-by copy-value as you state because it's clear. I think older textbooks used copy-value more frequently
@MsDuketown
@MsDuketown 10 ай бұрын
​​​​@@MikeShahyeah, [regarding value - copy discussion) the wording should explains the how and the what should be clear via it's usage. Avoiding unnecessary copies was populairized under Zero-copy, although there are many other scenario's embedded in the functionality of the language, ie. context switch. Great stuff to automate via CI/CD automation and typesafe languages. And that's what's happening for a long time in softwareland like GitHub.
@MikeShah
@MikeShah 10 ай бұрын
Cheers -- Agreed! @@MsDuketown
@thestarinthesky_
@thestarinthesky_ Жыл бұрын
I am starting to watch STL from beginning. such an amazing content! Thanks for your time and effort!
@MikeShah
@MikeShah Жыл бұрын
Cheers!
@reptilicusrex4748
@reptilicusrex4748 Жыл бұрын
An excellent explanation as usual. Thanks for making these videos.
@MikeShah
@MikeShah Жыл бұрын
Cheers! Thank you for the kind words 🙂
@manfredkernMK
@manfredkernMK 6 ай бұрын
Maybe, that you now also have a course, you should "redo" your first video in this series and mention it there. It might be a little late, if one already watched half the series (more than 100 videos) ...
@MikeShah
@MikeShah 6 ай бұрын
Not a bad idea 🙂The latest course will also get some additions
@learntolearn3063
@learntolearn3063 Жыл бұрын
Basically i use string view instead of memory map the file. Mean to say open a file and seek it end to know length rewind it again use read api and use string view to buffer and do simd operations on those data.
@MikeShah
@MikeShah Жыл бұрын
Neat -- cool application 🙂
@learntolearn3063
@learntolearn3063 Жыл бұрын
@@MikeShah 🙂👍
@bsdooby
@bsdooby Жыл бұрын
Not sure I really get the benefit of string_view; passing the initial std::string by ref should be as efficient as can be (?) OFC, if you need to have a char* on the caller site first, then I agree. But why this detour?
@MikeShah
@MikeShah Жыл бұрын
The primary advantage in my mind is the avoiding of copies as you've stated without having to worry about ref, as this shows intent. The second advantage is that you also get this advantage if you want to pass a substr (substring) of the original string, and also do not have to pay for a copy in that use case either :) (And the third advantage as you've stated, if you have char* then you avoid allocations again).
@bsdooby
@bsdooby Жыл бұрын
@@MikeShah Sorry to hit you up, again. By showing intent you mean, that, by using std::string_view, we tell the caller, that we only peek into the string, and not doing funky things with it...(?)
@MikeShah
@MikeShah Жыл бұрын
​@@bsdooby Yup exactly -- though this is the same as 'const std::string& ref', just in my opinion a bit more explicit in a function signature.
@bsdooby
@bsdooby Жыл бұрын
@@MikeShah Thank you for taking the time to answer these questions. Very much appreciate it!
@thestarinthesky_
@thestarinthesky_ Жыл бұрын
Thank you for this great explanations. I was going to ask if you would post any videos on STL :)
@MikeShah
@MikeShah Жыл бұрын
Cheers! Lots of STL videos in the pipeline 🙂
@thestarinthesky_
@thestarinthesky_ Жыл бұрын
@@MikeShah Yay!!! Great! Thanks!
@user-fw7sl8qr6n
@user-fw7sl8qr6n 3 ай бұрын
I definitely see the use case here with const char* however, I tried to replicate this in compiler explorer for (1) std::string s and const std::string s and in both cases it still seemed to have an allocator at the site of the function call. Does this only work for string literals (e.g. only for const char*)?
@MikeShah
@MikeShah 3 ай бұрын
Might need to see an example -- do you have the compiler explorer snippet?
@thestarinthesky_
@thestarinthesky_ Жыл бұрын
I have a question: because std::string_view is a non-owning view of a sequence of characters, it's important to be careful when using it as a data member in a class or struct, right? I mean, If we're not careful, we could end up with a dangling pointer if the memory that the std::string_view points to is deallocated while the std::string_view still exists. For insurance if we call the copy constructor of this class and now two objects points to the same memory, what would happen if one of the objects go out of scope? We'd have dangling pointer and some other issue. Am I right?
@MikeShah
@MikeShah Жыл бұрын
That's probably a good point, using it as a data member in a struct or class is probably not the intent, so prefer string_view or span for use in arguments of functions or member functions, not as member variables in a class.
@thestarinthesky_
@thestarinthesky_ Жыл бұрын
@@MikeShah thanks for the response🌸🙏🏽
@dizhang571
@dizhang571 Жыл бұрын
If I already have a string (which has been constructed and allocated), I guess using string_view isn't saving me allocations any more since I can pass const std::string&?
@MikeShah
@MikeShah Жыл бұрын
Correct -- however if you only want to work with a subset of the string, a string_view still may be preferred. I'd say string_view provides a clearly contract with the intention of the code as well (i.e. read-only view of a string or a subset of the string)
@dizhang571
@dizhang571 Жыл бұрын
@@MikeShah Thanks for your reply. For the subset part do you mean that the substr() method will return a string_view as well and won't cause any heap allocation, while a string::substr() will create another string, which is doing additional allocation?
@MikeShah
@MikeShah Жыл бұрын
@@dizhang571 Correct, substr will cause another allocation whereas working with a string_view will not (because we're just offsetting at the string data i.e. [pos + data .. data)
@axelBr1
@axelBr1 16 күн бұрын
@@MikeShah Thanks for giving a useful example of where passing a string_view is better than passing a reference to a string.
@MikeShah
@MikeShah 16 күн бұрын
@@axelBr1 cheers!
@johnabrossimow
@johnabrossimow 3 ай бұрын
std::string owns data, std::string_view doesn't. Both have the same interface.
@TVSh0rts
@TVSh0rts Жыл бұрын
Why isnt there are string view without the const? Just char* data;
@guru_mahendrakar
@guru_mahendrakar 3 ай бұрын
13:00 printString(const std::string& t) printString("HloWorld"); passing const char * pointer how implicit converting std::string printString(std::string&& t)-> That's Work Beacuse Comoiler Create Object And Passing the Referance I'm Stuck On With Morning .... I Wanna Move Next Video 😢. No More Videos On This Topic. TRYING to Read But Implementation So Big 😔
@robertstrickland9722
@robertstrickland9722 Жыл бұрын
Hey Mike! I was wondering if you had plans to make some videos about utilizing some of the less commonly used Unix system headers such as zlib.h, tar.h, or any system libraries that you think are just useful.
@MikeShah
@MikeShah Жыл бұрын
Certainly! They'll be in the C series 😀
STL std::array (Since C++11)  | Modern Cpp Series Ep. 114
23:50
Mike Shah
Рет қаралды 2,2 М.
Why Is He Unhappy…?
00:26
Alan Chikin Chow
Рет қаралды 62 МЛН
Идеально повторил? Хотите вторую часть?
00:13
⚡️КАН АНДРЕЙ⚡️
Рет қаралды 6 МЛН
Fast and Furious: New Zealand 🚗
00:29
How Ridiculous
Рет қаралды 44 МЛН
Useful gadget for styling hair 🤩💖 #gadgets #hairstyle
00:20
FLIP FLOP Hacks
Рет қаралды 10 МЛН
are "smart pointers" actually smart?
9:44
Low Level Learning
Рет қаралды 72 М.
Why The Windows Phone Failed
24:08
Apple Explained
Рет қаралды 151 М.
Introduction to constexpr | Modern Cpp Series Ep. 86
10:56
Mike Shah
Рет қаралды 11 М.
How Strings Work in C++ (and how to use them)
19:26
The Cherno
Рет қаралды 453 М.
Compilers, How They Work, And Writing Them From Scratch
23:53
Adam McDaniel
Рет қаралды 133 М.
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 158 М.
what even is a "reference"?
5:44
Low Level Learning
Рет қаралды 125 М.
Rate This Smartphone Cooler Set-up ⭐
0:10
Shakeuptech
Рет қаралды 6 МЛН
Todos os modelos de smartphone
0:20
Spider Slack
Рет қаралды 65 МЛН
$1 vs $100,000 Slow Motion Camera!
0:44
Hafu Go
Рет қаралды 28 МЛН
Samsung laughing on iPhone #techbyakram
0:12
Tech by Akram
Рет қаралды 6 МЛН
ноутбуки от 7.900 в тг laptopshoptop
0:14
Ноутбуковая лавка
Рет қаралды 3,5 МЛН
iPhone 15 Pro Max vs IPhone Xs Max  troll face speed test
0:33