C++中string的使用详解(构造函数、遍历,迭代器遍历、类型转换、计算大小、赋值与连接、比较、查找、替换、插入、删除、小写转大写等)

注意点及解释都在程序中有体现,并作出详细介绍

#include <iostream>
#include <string>
#include <exception>
#include <functioal>
#include <algorithm>
using namespace std;

//string类的构造函数
void string_init()
{
    string s1;			//无参构造函数
    string s2("helloworld");	//字符串初始化s2
    string s3(10,'a');		//10个字符a初始化s3
    string s4(s3);		//拷贝构造函数
    string s5 = s2;

    cin >> s1;
    cout << s1 << endl;
    cout << s2 << endl;
    cout << s3 << endl;
    cout << s4 << endl;
    cout << s5 << endl;
}

//string类的遍历  operator[]  at()  
void string_at()    //at()函数用于返回字符串中第n个字符的位置,并提供范围检查,当越界时会抛出out_of_range异常,下标运算符[]不提供检查访问
{
    string s1("helloworld");
    try
    {
	cout << s1.at(10) << endl;	    //若越界at()函数会抛出异常
    }
    catch(exception &e)
    {
	cout << "at()函数捕获到异常:" << e.what() << endl; 
    }

}

//string类 迭代器遍历字符串
void string_iterator()
{
    string s1 = "helloworld";
    for(string::iterator it=s1.begin(); it != s1.end(); it++)
    {
	cout << *it;
    }
    cout << endl;
}

//string类与char*类型转换
void string_str()
{
    //const char *str() const;   返回一个非NULL终止的字符数组
    //const char *c_str() const;    返回一个以NULL终止的字符串
    string s1 = "helloworld";
    const char *str = s1.c_str();
    cout << "str=" << str << endl;
    cout << "s1.c_str()=" << s1.c_str() << endl;

    //int copy(char *s, int n , int pos = 0)const;  把当前字符串从位置pos开始的n个字符拷贝到以s为起始的字符数组中
    char buf[32] = {0};
    //s1.copy(buf, 4, 6);
    s1.copy(buf,5);		//从字符串开始出复制 第三个参数可省略
    cout << "buf=" << buf << endl;
}

//计算大小
void string_length()
{
    string s("helloworld");
    cout << "s.length()=" << s.length() << endl;
    if(s.empty())
    {
	cout << "s is empty" << endl;
    }
    else
    {
	cout << "s is not empty " << endl;
    }
}

//string类的赋值与连接
void string_assign()
{
    string s1("helloworld");
    string s2("1234567890");

    s1.assign(s2);
    cout << "s1=" << s1 << endl;

    s1.assign(10,'A');		    //将10个字符A赋值给s1
    cout << "s1=" << s1 << endl;

    s1.assign("1234567890", 5);	    //将字符串前5个字符赋值给s1
    cout << "s1=" << s1 << endl;

    s1.assign(s2,3,4);		    //从s2字符串的第3个字符开始往后的4个字符赋值给s1
    cout << "s1=" << s1 << endl;

    s1 = s1 + s2;		    //连接
    cout << "s1=" << s1 << endl;

    s1.append(s2);		    //连接
    cout << "s1=" << s1 << endl;

    string s4("AAAAA");
    string s5("BBBBB");
    s4.append("CCCCC",3);	    //将字符串的前3个字符连接到s4后边
    cout << "s4=" << s4 << endl;

    s5.append(5,'D');		    //在s5结尾添加5个字符D
    cout << "s5=" << s5 << endl;

    s4.append(s5,2,5);		    //将s5从第2个字符开始往后的5个字符添加到s4后边
    cout << "s4=" << s4 << endl;
}

//string类的比较
void string_compare()
{
    string s1("helloworld");
    string s2("helloaaaaa");
    if(s1.compare(s2) > 0)
    {
	cout << "s1 > s2" << endl;
    }
    else
    {
	cout << "s1 < s2" << endl;
    }

    if(s1.compare("aaa") > 0)
    {
	cout << "s1 > aaa" << endl;
    }
    else
    {
	cout << "s1 < aaa" << endl;
    }
}

//string类 返回一个字符串
void string_substr()
{
    string s1("helloworld");
    string s2;

    s2 = s1.substr();
    cout << "s2=" << s2 << endl;

    s2 = s1.substr(3,4);
    cout << "s2=" << s2 << endl;
}

//string类 查找
void string_find()
{
    string s1("helloworld");
    string s2("owo");

    int index;
    //index = s1.find('o');		//默认从0开始查找字符o
    index = s1.find('o',5);		//从第5个字符开始查找字符o
    //index = s1.find('o',8);		//从第8个字符开始查找字符o, 找不到返回值为-1
    cout << "index=" << index << endl;

    index = s1.find("world",1);
    cout << "index=" << index << endl;
    index = s1.find(s2,0);
    cout << "index=" << index << endl;

    index = s1.rfind(s2,9);		//反向查找
    cout << "index==" << index << endl;
}

//string类 替换
void string_replace()
{
    string s1("11111");
    string s2("22222");

    s1.replace(1,3,"aaa");	    //删除从1开始后的3个字符  用aaa替换
    cout << "s1=" << s1 << endl;

    s1.replace(1,3,"bbbbb",3);	    //删除从1开始后的3个字符 并用"bbbbb"的前3个字符替换
    cout << "s1=" << s1 << endl;

    s1.replace(1,3,"bbbbb",2,4);    //删除从1开始后的3个字符 并用"bbbbb"的第2到第4个字符替换
    cout << "s1=" << s1 << endl;

    string s3("helloworldhelloworldhelloworldhelloworld");
    string s4("world");
    int index = s3.find(s4,0);
    while(index != -1)
    {
	s3.replace(index,s4.length(),"AAAAA");
	index = s3.find(s4,index + strlen("AAAAA"));
    }
    cout << "s3=" << s3 << endl;
}

//string类 插入与删除
void string_insert()
{
    string s1("helloworld");
    string s2("1234567890");

    s1.insert(5,s2);		    //在字符串s1的第5个位置插入s2
    cout << "s1=" << s1 << endl;

    s1.insert(5,"AAAAAAAAAA",5);		    //在字符串s1的第5个位置插入s2的前5个字符
    cout << "s1=" << s1 << endl;

    s2.insert(5,10,'X');	    //在s2的第五个字符位置插入10个字符X
    cout << "s2=" << s2 << endl;

    s2.erase(5,10);		    //删除从第5个开始后的10个字符
    cout << "s2=" << s2 << endl;
}

//sreing 字符转大写 
void string_transform()
{
    string s1("aaaaaBBBBB");
    transform(s1.begin(), s1.end(), s1.begin(), ::toupper);
    cout << "s1=" << s1 << endl;
}
    
int main()
{
    //string_init();
    //string_at();
    //string_iterator();
    //string_str();
    //string_length();
    //string_assign();
    //string_compare();
    //string_substr();
    //string_find();
    //string_replace();
    //string_insert();
    string_transform();

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值