关于字符串的一些函数

本文介绍了C++中字符串转换函数std::to_string,以及std::string类的substr方法、std::reverse用于反转序列、tolower处理字符大小写、容器操作如std::find和std::vector的使用,以及排序算法lower_bound的应用实例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

to_string(i) 是一个在 C++ 中用于将整数 i 转换为字符串的函数调用。

#include <string>  
#include <iostream>  
  
int main() {  
    int i = 123;  
    std::string str_i = std::to_string(i);  
    std::cout << str_i << std::endl;  // 输出 "123"  
    return 0;  
}

这里,我们使用了 std::to_string 函数,它是定义在 <string> 头文件中的。转换后的字符串被存储在 std::string 类型的变量 str_i 中。

substr() 是 C++ 标准库中 std::string 类的一个成员函数。这个函数用于从字符串中提取子字符串。

substr() 的原型如下:

string substr(size_t pos = 0, size_t len = npos) const;

其中:

  • pos 是子字符串的起始位置(基于 0 的索引)。默认值为 0,表示从字符串的开始处提取。
  • len 是子字符串的长度。默认值为 std::string::npos,表示提取到字符串的末尾。

返回值是从字符串中提取的子字符串。

以下是一个使用 substr() 的简单示例:

#include <iostream>  
#include <string>  
  
int main() {  
    std::string s = "Hello, World!";  
    std::string sub = s.substr(7, 5);  // 从位置 7 开始,提取长度为 5 的子字符串  
    std::cout << sub << std::endl;  // 输出 "World"  
    return 0;  
}

注意,如果指定的 pos 大于字符串的长度,substr() 将抛出 std::out_of_range 异常。

在C++中,std::reverse是一个算法,用于反转序列中的元素。这个算法通常用于容器,如std::vectorstd::list等。

下面是一个简单的例子,展示如何使用std::reverse来反转一个std::vector

#include <iostream>  
#include <vector>  
#include <algorithm>  
  
int main() {  
    std::vector<int> v = {1, 2, 3, 4, 5};  
    std::reverse(v.begin(), v.end());  
  
    for (int i : v) {  
        std::cout << i << " ";  
    }  
    return 0;  
}
5 4 3 2 1

在这个例子中,我们首先创建了一个包含五个整数的std::vector。然后,我们使用std::reverse来反转这个向量的元素。最后,我们使用范围for循环来打印反转后的向量。

在C++中,tolower 是一个标准库函数,属于 <cctype> 头文件。这个函数用于将大写字母转换为小写字母。

函数的原型是:

int tolower(int c);

其中 c 是要转换的字符。如果 c 是一个大写字母(A-Z),则返回相应的小写字母(a-z)。如果 c 不是大写字母,则返回 c 本身。

下面是一个简单的示例:

 
#include <iostream>  
#include <cctype>  // for tolower  
  
int main() {  
    char ch = 'A';  
    std::cout << "Lowercase of " << ch << " is: " << std::tolower(ch) << std::endl;  
    return 0;  
}

输出将是:

Lowercase of A is: a

string::npos 是 C++ 标准库中 std::string 类的一个静态成员常量。它通常表示一个不可能的或无效的位置。具体来说,它的值为 std::string::size_type 类型的最大可能值,通常是一个非常大的数。

在 std::string 的上下文中,npos 常常用于表示字符串中不存在某个子串或字符。例如,当你使用 find 函数来查找一个子串或字符在字符串中的位置时,如果该子串或字符不存在于字符串中,find 函数将返回 npos

示例:

#include <iostream>  
#include <string>  
  
int main() {  
    std::string s = "Hello, world!";  
    std::size_t found = s.find("world");  
    if (found != std::string::npos) {  
        std::cout << "Found at position: " << found << std::endl;  
    } else {  
        std::cout << "Not found" << std::endl;  
    }  
    return 0;  
}

在上面的示例中,如果 "world" 子串存在于字符串中,find 函数将返回它的起始位置。如果不存在,则返回 npos

std::find 是 C++ 标准库中的一个算法,用于在容器(如数组、向量、列表等)中查找特定元素。它位于 <algorithm> 头文件中。

std::find 的基本语法如下:

template <class InputIt, class T>  
InputIt find(InputIt first, InputIt last, const T& value);

其中:

  • first 和 last 分别表示要搜索的范围的起始和结束迭代器。
  • value 是要查找的值。

该函数返回一个迭代器,指向第一个等于 value 的元素。如果没有找到该元素,则返回 last

以下是一个简单的示例:

#include <iostream>  
#include <algorithm>  // for std::find  
#include <vector>  
  
int main() {  
    std::vector<int> v = {1, 2, 3, 4, 5};  
    int value_to_find = 3;  
    auto it = std::find(v.begin(), v.end(), value_to_find);  
    if (it != v.end()) {  
        std::cout << "Found " << value_to_find << " at position: " << std::distance(v.begin(), it) << std::endl;  
    } else {  
        std::cout << value_to_find << " not found" << std::endl;  
    }  
    return 0;  
}

输出:

Found 3 at position: 2

在上面的示例中,我们使用 std::find 在一个整数向量中查找值 3。如果找到,则输出其位置;否则,输出未找到的消息。

在 C++ 的标准模板库(STL)中,lower_bound 是一个常用的算法,它在一个已排序的范围(如数组、vector、list 等)中查找不小于(即大于或等于)某个值的第一个元素的迭代器。这个算法通常与 upper_bound 一起使用,后者查找大于某个值的第一个元素的迭代器。

lower_bound 的原型在 <algorithm> 头文件中定义,大致如下:

template< class ForwardIt, class T >  
ForwardIt lower_bound( ForwardIt first, ForwardIt last, const T& value );
  • first, last:表示要搜索的范围的起始和结束迭代器。
  • value:要搜索的值。

函数返回一个迭代器,指向不小于 value 的第一个元素。如果 value 大于或等于范围中的所有元素,则返回 last。如果 value 小于范围中的所有元素,则返回 first

下面是一个简单的示例,演示了如何使用 lower_bound:

#include <iostream>  
#include <vector>  
#include <algorithm>  
  
int main() {  
    std::vector<int> v = {1, 3, 5, 7, 9};  
    int target = 4;  
  
    auto it = std::lower_bound(v.begin(), v.end(), target);  
  
    if (it != v.end()) {  
        std::cout << "Found element not less than " << target << ": " << *it << std::endl;  
    } else {  
        std::cout << "No element not less than " << target << " found." << std::endl;  
    }  
  
    // 输出:Found element not less than 4: 5  
  
    return 0;  
}

在这个示例中,我们搜索不小于 4 的第一个元素,并找到了 5。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值