【C++】string类的使用④(常量成员Member constants)

简介: C++ `std::string` 的 `find_first_of`, `find_last_of`, `find_first_not_of`, `find_last_not_of` 函数分别用于从不同方向查找目标字符或子串。它们都返回匹配位置,未找到则返回 `npos`。`substr` 用于提取子字符串,`compare` 则提供更灵活的字符串比较。`npos` 是一个表示最大值的常量,用于标记未找到匹配的情况。示例代码展示了这些函数的实际应用,如替换元音、分割路径、查找非字母字符等。

==find_first_of==

在这里插入图片描述

从前往后查找在string串中任何存在于某字符或字符串中的字符

(1) string
size_t find_first_of (const string& str, size_t pos = 0) const;
从pos位置开始查找string串的字符是否存在于str串中。如果存在,返回匹配上的下标位置;如果找到string串的末尾也没有,则返回常量成员npos(可以看作是一个很大的值,可以提前看下文末的常量成员内容)。
(2) c-string
size_t find_first_of (const char* s, size_t pos = 0) const;
从pos位置开始查找string串的字符是否存在于字符串s中。如果存在,返回匹配上的下标位置;如果找到string串的末尾也没有,则返回常量成员npos。
(3) buffer
size_t find_first_of (const char* s, size_t pos, size_t n) const;
从pos位置开始查找string串的字符是否存在于字符串s前n个字符组成的子串中。如果存在,返回匹配上的下标位置;如果找到string串的末尾也没有,则返回常量成员npos。
(4) character
size_t find_first_of (char c, size_t pos = 0) const;
从pos位置开始查找string串的字符是否为字符c。如果存在,返回匹配上的下标位置;如果找到string串的末尾也没有,则返回常量成员npos。

使用案例:

// string::find_first_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t
using namespace std;
int main()
{
   
   
    // 将句子中的元音字母转换成星号(*)
    string str("Please, replace the vowels in this sentence by asterisks.");
    size_t found = str.find_first_of("aeiou");
    while (found != string::npos)
    {
   
   
        str[found] = '*';
        found = str.find_first_of("aeiou", found + 1);
    }

    cout << str << '\n';

    return 0;
}

在这里插入图片描述

==find_last_of==

在这里插入图片描述

和find_first_of的功能及其相似,不过此函数是从后往前找

(1) string
size_t find_last_of (const string& str, size_t pos = npos) const;
(2) c-string
size_t find_last_of (const char* s, size_t pos = npos) const;
(3) buffer
size_t find_last_of (const char* s, size_t pos, size_t n) const;
(4) character
size_t find_last_of (char c, size_t pos = npos) const;

使用案例:

// string::find_last_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>         // std::size_t
using namespace std;

void SplitFilename(const std::string& str)
{
   
   
    cout << "Splitting: " << str << '\n';
    size_t found = str.find_last_of("/\\");
    cout << " path: " << str.substr(0, found) << '\n';
    cout << " file: " << str.substr(found + 1) << '\n';
}

int main()
{
   
   
    string str1("/usr/bin/man");
    string str2("c:\\windows\\winhelp.exe");

    SplitFilename(str1);
    SplitFilename(str2);

    return 0;
}

在这里插入图片描述

==find_first_not_of==

在这里插入图片描述

在之前find_first_of的基础上,这个看名字似乎就能理解其功能了。没错这个就是从前往后找string串中任何不存在于某字符或字符串中的字符

string (1)
size_t find_first_not_of (const string& str, size_t pos = 0) const;
c-string (2)
size_t find_first_not_of (const char* s, size_t pos = 0) const;
buffer (3)
size_t find_first_not_of (const char* s, size_t pos, size_t n) const;
character (4)
size_t find_first_not_of (char c, size_t pos = 0) const;

使用案例:

// string::find_first_not_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t
using namespace std;
int main()
{
   
   
    string str("look for non-alphabetic characters...");

    size_t found = str.find_first_not_of("abcdefghijklmnopqrstuvwxyz ");

    if (found != string::npos)
    {
   
   
        cout << "The first non-alphabetic character is " << str[found];
        cout << " at position " << found << '\n';
    }

    return 0;
}

在这里插入图片描述

==find_last_not_of==

在这里插入图片描述

从后往前找string串中任何不存在于某字符或字符串中的字符

string (1)
size_t find_last_not_of (const string& str, size_t pos = npos) const;
c-string (2)
size_t find_last_not_of (const char* s, size_t pos = npos) const;
buffer (3)
size_t find_last_not_of (const char* s, size_t pos, size_t n) const;
character (4)
size_t find_last_not_of (char c, size_t pos = npos) const;

使用案例:

// string::find_last_not_of
#include <iostream>       // std::cout
#include <string>         // std::string
#include <cstddef>        // std::size_t
using namespace std;
int main()
{
   
   
    string str("Please, erase trailing white-spaces   \n");
    string whitespaces(" \t\f\v\n\r");

    size_t found = str.find_last_not_of(whitespaces);
    if (found != string::npos)
        str.erase(found + 1);
    else
        str.clear();            // str对象串中全为空白

    cout << '[' << str << "]\n";

    return 0;
}

在这里插入图片描述

==substr==

在这里插入图片描述
string substr (size_t pos = 0, size_t len = npos) const;
返回一个由string串pos位置开始跨越len个字符(len过大时就取到string对象末尾)创建的子对象(也是string类型)

使用案例:

// string::substr
#include <iostream>
#include <string>
using namespace std;
int main ()
{
   
   
  string str="We think in generalities, but we live in details.";
                                           // (quoting Alfred N. Whitehead)

  string str2 = str.substr (3,5);     // "think"

  size_t pos = str.find("live");      // 取"live"在str对象中的位置

  string str3 = str.substr (pos);     // 取从"live"开始到整个str结尾构造字串给str3

  cout << str2 << ' ' << str3 << '\n';

  return 0;
}

在这里插入图片描述

==compare==

在这里插入图片描述

按字典序规则将string对象(或其字串)和别的字符序列进行比较。和之前非成员函数重载的比较运算符很相似,但其功能更全一些,可以直接进行子串之间的比较

string (1)
int compare (const string& str) const;
substrings (2)
int compare (size_t pos, size_t len, const string& str) const;
int compare (size_t pos, size_t len, const string& str, size_t subpos, size_t sublen) const;
c-string (3)
int compare (const char* s) const;
int compare (size_t pos, size_t len, const char* s) const;
buffer (4)
int compare (size_t pos, size_t len, const char* s, size_t n) const;

直接上代码案例吧,其实也好懂:

// comparing apples with apples
#include <iostream>
#include <string>
using namespace std;
int main()
{
   
   
    string str1("green apple");
    string str2("red apple");

    if (str1.compare(str2) != 0)
        cout << str1 << " is not " << str2 << '\n';

    if (str1.compare(6, 5, "apple") == 0)
        cout << "still, " << str1 << " is an apple\n";

    if (str2.compare(str2.size() - 5, 5, "apple") == 0)
        cout << "and " << str2 << " is also an apple\n";

    if (str1.compare(6, 5, str2, 4, 5) == 0)
        cout << "therefore, both are apples\n";

    return 0;
}

在这里插入图片描述

🔥常量成员(Member constants)

在这里插入图片描述

==npos==

在这里插入图片描述
static const size_t npos = -1;
npos是一个const类型的静态成员常量,表示size_t类型的数据可能取到的最大值

可以通过string::npos获取其值,在上面的很多代码案例中都有用到。

整个值,常被当作缺省参数用在string的很多成员函数中(如len,sublen等),表示取到string对象的末尾。

当它作为返回值被返回时,常被用于表示无匹配项

这个常数被赋值为-1,是因为size_t是一个无符号整型,-1代表的就是无符号整型(size_t)可能取到的最大值。

结语

本篇博客,介绍了关于string的字符串操作,可以查找和获取字符串的相关内容;以及常量成员,表示size_t可能取到的最大值,作为返回值返回时常用于表示无匹配项
string的使用系列到这里就结束了。博主后续还会分享string类的模拟实现以及STL更多的内容,感谢大家的支持。♥

相关文章
|
5天前
|
安全 编译器 C语言
【C++数据结构】string的模拟实现
【C++数据结构】string的模拟实现
|
5天前
|
存储 安全 编译器
【C++】类和对象(下)
【C++】类和对象(下)
【C++】类和对象(下)
|
4天前
|
编译器 C++
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
virtual类的使用方法问题之C++类中的非静态数据成员是进行内存对齐的如何解决
|
4天前
|
编译器 C++
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
virtual类的使用方法问题之静态和非静态函数成员在C++对象模型中存放如何解决
|
4天前
|
编译器 C++
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
virtual类的使用方法问题之在C++中获取对象的vptr(虚拟表指针)如何解决
|
5天前
|
编译器 C++
【C++】类和对象(中)
【C++】类和对象(中)
|
5天前
|
存储 编译器 程序员
【C++】类和对象(上)
【C++】类和对象(上)
|
6天前
|
C++ 容器
C++中自定义结构体或类作为关联容器的键
C++中自定义结构体或类作为关联容器的键
13 0
|
6天前
|
存储 算法 搜索推荐
【C++】类的默认成员函数
【C++】类的默认成员函数
|
11天前
|
C++
C++ --> 类和对象(三)
C++ --> 类和对象(三)
26 9