在C++中
以下代码使用
- #include
- #include
- using namespace std;
-
- int main() {
- string str = "Hello, world!";
- int len = str.size();
- cout << "字符串的长度为: " << len << endl;
- return 0;
- }
以下代码使用
- #include
- #include
- using namespace std;
-
- int main() {
- char str1[] = "Hello";
- char str2[10];
- strcpy(str2, str1);
- cout << "复制的字符串是: " << str2 << endl;
- return 0;
- }
尽管两者都提供了字符串操作,但它们的实现方式和用法是不同的。在C++实际编程中,推荐使用