使用c++ string 实现Cstring的ReverseFind 方法
时间: 2023-08-02 08:09:10 浏览: 71
C++ 中的 std::string 类提供了类似于 CString 的 ReverseFind 方法,可以使用该方法查找字符串中最后一个匹配字符的位置。实现方法如下:
```c++
#include <iostream>
#include <string>
using namespace std;
int main() {
string str = "hello world";
char ch = 'l';
// 查找字符 'l' 最后一次出现的位置
size_t pos = str.rfind(ch);
if (pos != string::npos) {
cout << "Last occurrence of " << ch << " found at position " << pos << endl;
} else {
cout << ch << " not found in string" << endl;
}
return 0;
}
```
在上面的代码中,我们使用 std::string 类的 rfind 方法查找字符 'l' 最后一次出现的位置,并将结果存储在变量 pos 中。如果找到了该字符,则输出其位置;否则输出未找到的提示信息。
相关问题
请用c++ string 实现Cstring中的GetBuffer 和ReleaseBuffer 方法
好的,下面是C++中用string实现Cstring中的GetBuffer和ReleaseBuffer方法的示例代码:
```c++
#include <iostream>
#include <string>
class CString
{
public:
CString() : m_pStr(nullptr) {}
~CString() { ReleaseBuffer(); }
char* GetBuffer(int nSize)
{
ReleaseBuffer(); // 先释放旧内存
m_pStr = new char[nSize + 1]; // 申请新内存
return m_pStr;
}
void ReleaseBuffer()
{
if (m_pStr != nullptr)
{
delete[] m_pStr; // 释放内存
m_pStr = nullptr;
}
}
private:
char* m_pStr; // 存储字符串的指针
};
int main()
{
CString str;
std::string input;
std::cout << "请输入字符串:";
std::cin >> input;
char* pBuf = str.GetBuffer(input.length());
if (pBuf != nullptr)
{
std::copy(input.begin(), input.end(), pBuf);
pBuf[input.length()] = '\0';
std::cout << "获取到的字符串为:" << str << std::endl;
}
str.ReleaseBuffer();
return 0;
}
```
上述代码实现了C++中用string实现Cstring中的GetBuffer和ReleaseBuffer方法,其中GetBuffer方法申请新内存并返回指向该内存的指针,ReleaseBuffer方法释放内存并将指针置为nullptr。在main函数中,先输入一个字符串,然后调用GetBuffer方法获取指向该字符串的指针,将字符串内容拷贝到该指针指向的内存中,并输出获取到的字符串。最后调用ReleaseBuffer方法释放内存。
请用c++ string实现Cstring的 trim trimleft trimright 带参数的方法
以下是使用C++ string实现Cstring的trim、trimleft、trimright带参数的方法的示例代码:
```c++
#include <iostream>
#include <string>
using namespace std;
string trim(string str, const string& chars) {
str.erase(0, str.find_first_not_of(chars));
str.erase(str.find_last_not_of(chars) + 1);
return str;
}
string trimleft(string str, const string& chars) {
str.erase(0, str.find_first_not_of(chars));
return str;
}
string trimright(string str, const string& chars) {
str.erase(str.find_last_not_of(chars) + 1);
return str;
}
int main() {
string str = " \t Hello, World! \t ";
cout << "Original string: " << str << endl;
cout << "Trimmed string: " << trim(str, " \t") << endl;
cout << "Trimmed left string: " << trimleft(str, " \t") << endl;
cout << "Trimmed right string: " << trimright(str, " \t") << endl;
return 0;
}
```
在上面的代码中,使用了string的成员函数find_first_not_of和find_last_not_of实现了trim、trimleft、trimright方法,并且使用了const string&参数来指定要删除的字符集。在主函数中,我们定义了一个字符串str,并使用三种不同的方式对其进行了处理和输出。注意,在以上代码中,我们将字符串作为参数传递给了函数,这意味着函数会对原始字符串进行更改。如果您不希望更改原始字符串,可以将它们复制到新字符串中并进行更改。
相关推荐
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)