C++ string类型

来源:互联网 发布:淘宝正常一天能出几单 编辑:程序博客网 时间:2024/06/02 07:02

没办法,测试需要,转c++,请指教。

// ConsoleApplication2.cpp: 定义控制台应用程序的入口点。//#include "stdafx.h"#include <iostream>#include <string>using namespace std;int main(){      //字符串初始化方式    string s1("Hello world");    cout << "s1="<<s1 << endl;    cout << "s1.length=" << s1.length() << endl;    //string地址初始化方式    string alter("hello world2");    string  s2(alter);    cout << "s2=" << s2 << endl;    cout << "s2.length=" << s2.length() << endl;    //子字符串初始化方式    string s3(alter, 0, 5);//不取第6个字符    cout << "s3=" << s3 << endl;    cout << "s3.length=" << s3.length() << endl;    //由固定长度指针c字符数组转化    char  oper[]={'H','e','l','l','o'};    //char *opera = oper;//结果相同    string s4(oper);    cout << "s4=" << s4 << endl;    cout << "s4.length=" << s4.length() << endl;    //固定长度指针,指定长度    string s5(oper, 5);    cout << "s5=" << s5 << endl;    cout << "s5.length=" << s5.length() << endl;    //使用特殊标签    string s6(s1.begin(), s1.begin() + 5);    cout << "s6=" << s6 << endl;    cout << "s6.length=" << s6.length() << endl;    string pses6(s1.end()-7, s1.end());    cout << "pses6=" << pses6 << endl;    cout << "pses6.length=" << pses6.length() << endl;    //从C字符数组转化为字符串这里太不灵活,我要读取这个字符数组,必须知道它的长度。    //用c++的string类型可以方便操作,在公司面试的OS中。    s5 = oper;    cout << "s5=" << s5 << endl;    cout << "s5.length=" << s5.length() << endl;    //很多时候需要重新赋值。所以应该寻求以一种更好的方法,待续    return 0;}