Frank Wang
Frank Wang

Reputation: 920

using std::string::find to find a longer string in a small one

From this code:

#include <iostream>
#include <typeinfo>
#include <string>

int main() {
    std::string const s = "Thisisastring";
    std::string const ss = "Thisisastringg";
    std::string const sss = "Thisisastrin";
    auto p = ss.find(s);
    std::cout << "p:" << typeid(p).name() << "\np =" << p << std::endl;
    auto pp = sss.find(s);
    std::cout << "pp:" << typeid(pp).name() << "\npp =" << pp << std::endl;
    return 0;
}

I get the following output:

p:m
p =0
pp:m
pp =18446744073709551615

Questions:

Upvotes: 0

Views: 126

Answers (2)

Md. Monirul Islam
Md. Monirul Islam

Reputation: 731

The value of pp equal to string::npos (2^64-1 = 18446744073709551615). That is, the string is not found, not an overflow.

The string.find(...) method returns any of the followings

  1. If the string is matched, it return the position of the first character of the first match.
  2. If no matches were found, it returns string::npos

where npos is a static member constant value with the greatest possible value for an element of type size_t (an unsigned integral type).

To verify it print the string::npos

std::cout << std::string::npos << std::endl;

Upvotes: 4

Holt
Holt

Reputation: 37626

size_type is not a type by itself, it is an alias defined as follow:

typedef Allocator::size_type size_type; // until c++11
typedef std::allocator_traits<Allocator>::size_type size_type; // since c++11

Since you are using std::string, the allocator is std::allocator<char> and thus size_type is std::size_t.

The return value of std::type_info::name is implementation defined, and m is what your compiler (presumably g++) chose to use for std::size_t.

The return value of std::basic_string::find when the substring is not found is std::basic_string::npos, which is defined as:

static const size_type npos = -1;

In your case, it comes down to:

static const std::size_t npos = -1;
// which is (due to arithmetic modulo 2^n):
static const std::size_t npos = std::numeric_limits<std::size_t>::max();

And with your compiler, the resulting value is 18446744073709551615 which is 2^64 - 1 because std::size_t is probably a 64-bits value on your computer. This is a possible value, std::size_t is not required to be 64-bits long.

You should always test the return value of std::basic_string::find (and other related functions) against std::basic_string::npos directly.

Upvotes: 1

Related Questions