用rfind函数给我举个例子
时间: 2024-04-08 19:30:53 浏览: 23
当我们使用rfind函数时,它会返回字符串中指定字符串最后一次出现的位置。这是一个例子:
```python
sentence = "Hello World! This is a sample sentence."
last_occurrence = sentence.rfind("is")
print(last_occurrence) # 输出:22
```
在这个例子中,我们使用rfind函数搜索字符串"Hello World! This is a sample sentence."中最后一次出现的子字符串"is"。函数返回的结果是22,表示子字符串"is"最后一次出现在索引位置22处。
相关问题
整理一下他们常用的函数以及举几个例子
下面是这些容器常用的函数和一些示例:
1. unordered_multiset和unordered_set:
- insert(key): 插入一个键。
- erase(key): 删除指定键的元素。
- find(key): 查找指定键的元素。
- count(key): 返回指定键的元素数量。
```cpp
std::unordered_multiset<int> ums;
ums.insert(10);
ums.insert(20);
ums.insert(10);
ums.erase(20);
auto it = ums.find(10);
if (it != ums.end()) {
// 找到了键为10的元素
}
int count = ums.count(10); // 返回2
```
2. unordered_multimap和unordered_map:
- insert(std::pair<key, value>): 插入一个键值对。
- erase(key): 删除指定键的所有元素。
- find(key): 查找指定键的元素。
- count(key): 返回指定键的元素数量。
```cpp
std::unordered_multimap<int, std::string> umm;
umm.insert(std::make_pair(1, "apple"));
umm.insert(std::make_pair(2, "banana"));
umm.insert(std::make_pair(1, "orange"));
umm.erase(2);
auto it = umm.find(1);
while (it != umm.end()) {
// 遍历键为1的所有元素
std::cout << it->second << std::endl;
++it;
}
int count = umm.count(1); // 返回2
```
3. set和map:
- insert(key): 插入一个键。
- erase(key): 删除指定键的元素。
- find(key): 查找指定键的元素。
- count(key): 返回指定键的元素数量。
```cpp
std::set<int> s;
s.insert(10);
s.insert(20);
s.erase(10);
auto it = s.find(20);
if (it != s.end()) {
// 找到了键为20的元素
}
int count = s.count(10); // 返回0
```
4. multiset和multimap:
- insert(key): 插入一个键。
- erase(key): 删除指定键的元素。
- find(key): 查找指定键的元素。
- count(key): 返回指定键的元素数量。
```cpp
std::multiset<int> ms;
ms.insert(10);
ms.insert(20);
ms.insert(10);
ms.erase(20);
auto it = ms.find(10);
while (it != ms.end()) {
// 遍历键为10的所有元素
std::cout << *it << std::endl;
++it;
}
int count = ms.count(10); // 返回2
```
这些函数只是其中的一部分,STL还提供了其他丰富的函数和操作符,可以根据实际需求进行使用。
给我列举C++详细的find使用例子
C++中的`find()`函数可以用于在字符串或容器中查找指定元素的位置,下面是一些`find()`使用的例子:
1. 在字符串中查找单个字符
```c++
string str = "Hello, World!";
char ch = 'W';
size_t found = str.find(ch);
if (found != string::npos) {
cout << "Found " << ch << " at position " << found << endl;
}
else {
cout << "Not found" << endl;
}
// Output: Found W at position 7
```
2. 在字符串中查找子串
```c++
string str = "Hello, World!";
string subStr = "World";
size_t found = str.find(subStr);
if (found != string::npos) {
cout << "Found " << subStr << " at position " << found << endl;
}
else {
cout << "Not found" << endl;
}
// Output: Found World at position 7
```
3. 在容器中查找元素
```c++
vector<int> vec = {1, 2, 3, 4, 5};
int num = 3;
auto it = find(vec.begin(), vec.end(), num);
if (it != vec.end()) {
cout << "Found " << num << " at position " << it - vec.begin() << endl;
}
else {
cout << "Not found" << endl;
}
// Output: Found 3 at position 2
```
4. 在容器中查找符合条件的元素
```c++
vector<int> vec = {1, 2, 3, 4, 5};
auto it = find_if(vec.begin(), vec.end(), [](int x){ return x > 3; });
if (it != vec.end()) {
cout << "Found " << *it << " at position " << it - vec.begin() << endl;
}
else {
cout << "Not found" << endl;
}
// Output: Found 4 at position 3
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)