could you please do not use <string.h>
时间: 2023-12-11 19:01:59 浏览: 64
Sure! Instead of using <string.h>, we can use the string functions provided by the C++ Standard Library.
For example, instead of using strcpy(), we can use the string assignment operator "=" to copy the contents of one string to another:
```c++
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello";
std::string str2 = str1; // str2 now contains "Hello"
std::cout << str2 << std::endl;
return 0;
}
```
Similarly, we can use the string member function length() to get the length of a string:
```c++
#include <iostream>
#include <string>
int main() {
std::string str = "Hello";
std::cout << "Length of string is: " << str.length() << std::endl;
return 0;
}
```
And instead of using strcat(), we can use the string member function append() to concatenate two strings:
```c++
#include <iostream>
#include <string>
int main() {
std::string str1 = "Hello";
std::string str2 = " world!";
str1.append(str2);
std::cout << str1 << std::endl; // prints "Hello world!"
return 0;
}
```
I hope that helps! Let me know if you have any further questions.
阅读全文