1

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:

  1. 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)
  2. Why did we use size_t here? couldn't it be like this?:

position = s1.find(wordFind);

  1. What is position exactly?(I mean is it made up or is it a part of C++?)
  2. Does the if condition mean to search the word untill the end of the string?

Edit:Thank you everyone for helping me...I appreciate it:)

David Peterson
  • 169
  • 2
  • 9
  • 2
    `position = s1.find(wordFind);` lacks a type for the variable `position`. I don't know what you mean with "*is it made up or is it a part of C++?*". Regarding `size_t` see e.g. [What's the difference between size_t and int in C++?](https://stackoverflow.com/questions/502856/whats-the-difference-between-size-t-and-int-in-c). Regarding the `if` condition see [What does string::npos mean](https://stackoverflow.com/questions/3827926/what-does-stringnpos-mean). – walnut Feb 02 '20 at 22:35
  • 1
    I think U should read some c++ book because you need to know somethings can't be included in one answer – asmmo Feb 02 '20 at 22:35
  • @anonymous yeah you're right...I definitely should...right now I'm learning from a udemy course made by Frank mitropoulos and also internet :)) – David Peterson Feb 02 '20 at 22:38
  • @walnut Ok...I explain... 1. by made up I mean is it like naming a variable by ourselves or is it like a keyword here(for example: int num1 {}; I gave num1 ny myself) 2.what I don't understand from size_t is this: does it return the size of the object in byte? – David Peterson Feb 02 '20 at 22:45
  • 1
    @DavidPeterson `size_t` is just a type like e.g. `int` and `position` is just an arbitrary variable name. I second the recommendation to read a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). All of this should have been made clear in an introductory course or book. – walnut Feb 02 '20 at 22:49
  • 1
    `size_t` should be used when 1. You want something to represent an index and you're not doing any arithmetic on it or 2. You want it to work with values outside of the range of `int`. Otherwise use `int` as default – Sopel Feb 02 '20 at 22:59

2 Answers2

3

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.

Aykhan Hagverdili
  • 28,141
  • 6
  • 41
  • 93
1

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.

  1. 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...

  1. 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.

  1. What is position exactly?(I mean is it made up or is it a part of C++?)

See my last paragraph.

  1. 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
einpoklum
  • 118,144
  • 57
  • 340
  • 684