类string入门

73 篇文章 32 订阅
#include <iostream>
#include<string>
#include<algorithm>
using namespace std;

int main()
{
    string s;                     //s是string类的对象
	cout << s.length() << endl;   // 0

	s = "123456";
	cout << s.length() << endl;   // 6

	char str[] = "hello";
	s = str;
	cout << s << endl;            // hello

	s += ' ';
	cout << s << endl;            // hello
	
	s += "world";                 // hello world
	cout << s << endl;

	s.append(", hello");
	s.append(" computer!");
	cout << s << endl;            // hello world, hello computer!

	s = "123456";
	string::iterator it;                  
	it = s.begin();
	cout << *it << endl;            // 1
	cout << *(s.end() - 1) << endl; // 6
	s.insert(it + 1, 'C');          
	cout << s << endl;              //1C23456

	s.erase(it + 1, it + 2);        //注意:去掉的是[a, b)
	cout << s << endl;              //123456

	cout << s[0] << endl;           // 1

	cout << s.empty() << endl;      // 0

	s.replace(3, 2, "bc");          
	cout << s << endl;				// 123bc6

	s.replace(3, 2, "45");          
	cout << s << endl;              // 123456

	s = "abcdef";
	cout << s.find('a') << endl;    // 0
	cout << s.find("cd") << endl;	// 2
	cout << s.find("acm") << endl;	// 一个很大的数

	reverse(s.begin(), s.end());    //需要#include<algorithm>
	cout << s << endl;              //fedcba

	return 0;
}

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值