C++ string类型

没办法,测试需要,转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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值