Reputation: 169
I'm a beginner programmer and I'm trying to understand this piece if code and how it works...
So here is the code:
#include <iostream>
#include <string> //C++ Strings Library
using namespace std;
int main() {
string s1 {};
string wordFind {};
s1 = "The secret word is Boo";
cout << "Enter the word to find: ";
cin >> wordFind;
size_t position = s1.find(wordFind);
if (position != string::npos)
cout << "Found " << wordFind << " at position: " << position << endl;
else
cout << "Sorry! " << wordFind << " not found" << endl;
return 0;
}
These are the questions that I actually have:
size_t
and when should we use them exactly?(All I know is that we should use them for array indexing and loop counting)size_t
here? couldn't it be like this?:position = s1.find(wordFind);
Edit:Thank you everyone for helping me...I appreciate it:)
Upvotes: 1
Views: 3258
Reputation: 132118
Let's start by saying that IMHO, std::string
is kind of a lame string class design, or at least - certainly shows its age, in several ways.
- What is
size_t
and when should we use it exactly?
This is std::size_t
(in C++ it's not simply size_t
). But let's not talk about when exactly when you should use it, because...
- Why did we use
size_t
here?
I'd say we should not have. We should either have written
auto position = s1.find(wordFind);
(like you actually suggested, but with auto
to indicate automatic type deduction),
or if we want to be explicit, then:
std::string::size_type position = s1.find(wordFind);
Because that's what find()
returns. It often happens to be std::size_t
, but: (a) Maybe not, I'm not even sure (thanks @Ayxan); (b) you shouldn't care about whether it's std::size_t
or not, usually.
But even the above options are not right. There's a deeper problem here - and the problem is that find()
should really return an [std::optional][2]<std::string::size_type>
. Except that C++ didn't have an optional type until recently, so the string class kind of jerry-rigged it. A failure to find a substring returns a value consecrated as inherently invalid: std::string::npos
(which has type std::string::size_type
). So perhaps what we should have written is:
auto to_optional = [](std::string::size_type pos_or_npos) {
return pos_or_npos == std::string::npos ?
std::nullopt :
std::optional<std::string::size_type>{pos_or_npos};
};
auto position = to_optional(s1.find(wordFind));
Which is more like what we actually mean: Either you got some position, or you didn't.
- What is position exactly?(I mean is it made up or is it a part of C++?)
See my last paragraph.
- Does the if condition mean to search the word until the end of the string?
No, the find()
already searches until the end. With our recasting, the if()
would look like:
if (position.has_value())
// ... etc etc
else
// ... etc etc
Upvotes: 1
Reputation: 29985
What is
size_t
and when should we use them exactly?(All I know is that we should use them for array indexing and loop counting)
size_t
is an unsigned integer type large enough to represent the size of any object in C++, including array types. (size as in how much memory it occupies in bytes).
Why did we use
size_t
here?
Because it is typically large enough, and using something like int
wouldn't be enough if the string is larger than the maximum number int
can store. Pedantically, you should have used std::string::size_type
, which is guaranteed to be capable of holding the size of a std::string
, no matter how large it is.
couldn't it be like this?:
position = s1.find(wordFind);
No, you have to specify the type of position
. Alternatively you can use auto
: auto position = s1.find(wordFind);
What is position exactly?(I mean is it made up or is it a part of C++?)
Just a variable representing the index of the substring (word) you are searching for.
Does the if condition mean to search the word untill the end of the string?
It's checking to see if the word is found. std::string::npos
is a special number std::string::find
returns when the sought substring is not found.
Upvotes: 3