基于c++浅谈string类函数

来源:互联网 发布:打击网络犯罪宣传资料 编辑:程序博客网 时间:2024/06/06 08:30
#include <iostream>
#include <string>
using namespace std;
int main()
{
 string str1(5, 'c');
 string str2("Now is the time ...");
 string str3(str2, 11, 4);
 string str4("hello world", 6);
 cout << str1 << endl;
 cout << str2 << endl;
 cout << str3 << endl;
 cout << str4 << endl;
 string str5 = str1 + str2;
 cout << str5 << endl;
 cout << "下面开始字符串比较" << endl;
 cout << (str1 == str2) << endl;
 cout << (str1 != str2) << endl;
 cout << (str1 > str2) << endl;
 cout << (str1 < str2) << endl;
 cout << str1[3] << endl;
 cout << "其他方法调用" << endl;
 cout << str1.length() <<endl;                    //string类型求长度
 cout << (str2.find('1') == string::npos) << endl;       //寻找字符串中是否有某个字符
 cout << str2.at(3) << endl;        //取其中的第4个元素
 cout << str2.length() << "," << str2.capacity() << endl;
 string first("This comes first");
 string second("And this is second");
 first.swap(second);
 cout << first << endl;
 cout << second << endl;
 cout << str4.rfind("hello") << endl;
 return 0;
}
原创粉丝点击