string------修改

本文通过实例演示了C++中标准库字符串的各种操作方法,包括插入、赋值和删除等,展示了如何使用迭代器和不同参数进行精确控制。

  s.insert( itr, t )                                           s.insert(pos, n, c)

  s.insert( itr, n, t )                                       s.insert(pos,  s2)                        

  s.insert( itr, b, e )                                      s.insert(pos, s2,  pos2, len)

  s.assign(b, e)                                          s.insert(pos,  cp, len)

  s.assign(n, t)                                            s.insert(pos,  cp)

  s.erase(itr)                                                 s.assign(s2)

  s.erase(itr, e)                                             s.assign(s2, pos2, len)

                                                                      s.assign(cp, len)

                                                                      s.assign(cp)

                                                                      s.erase( pos, len )


#include <iostream>
#include <string>

using namespace std;

int main( int argc, char** argv )
{
	string s("hello");
	string s2("abcdef");

	string::iterator itr = s.begin();

	s.insert(itr, 'A');
	cout<<s<<endl;
	
	itr = s.begin();
	s.insert(itr, 3, 'B');
	cout<<s<<endl;


	string::iterator b = s2.begin();
	string::iterator e = s2.end();
	itr = s.begin();
	s.insert(itr, b, e);
	cout<<s<<endl;

	s = "hello";
	cout<<s<<endl;

	s.assign(b,e);
	cout<<s<<endl;

	s.assign(8, 'K');
	cout<<s<<endl;

	s = "abcdef";
	itr = s.begin();
	s.erase(itr);
	cout<<s<<endl;
	
	b = s.begin();
	b++;
	b++;
	e = s.end();
	e--;
	s.erase( b, e );
	cout<<s<<endl;

	return 0;
}


#include <iostream>
#include <string>

using namespace std;

int main( int argc, char** argv )
{
	string s("hello");
	string s2("abcdef");

	string::iterator itr = s.begin();

	s.insert(itr, 'A');
	cout<<s<<endl;
	
	itr = s.begin();
	s.insert(itr, 3, 'B');
	cout<<s<<endl;


	string::iterator b = s2.begin();
	string::iterator e = s2.end();

	s = "hello";
	s2 = "abc";
	s.insert(0, 3, 'A');
	cout<<s<<endl;

	s.insert(5,s2);
	cout<<s<<endl;

	s2 = "123456";
	s.insert(0, s2, 2, 3);
	cout<<s<<endl;

	char *cp = "Stately plump Buck";
	s.assign(cp, 7);
	cout<<s<<endl;

	s.assign(cp);
	cout<<s<<endl;

	s = "hello";
	s.insert(0, cp, 7);
	cout<<s<<endl;

	s.insert(0, cp);
	cout<<s<<endl;

	s = "hello";
	s2 = "abcdef";

	s.assign(s2, 2, 3);
	cout<<s<<endl;

	s.assign(s2);
	cout<<s<<endl;

	s.erase(2, 3);
	cout<<s<<endl;

	s = "123456789";
	s.erase(s.size()-5, 5);
	cout<<s<<endl;

	s.insert(s.size(), 5, '!');
	cout<<s<<endl;

	s = "abc";
	s.erase(0, 1).insert(0, "A");
	cout<<s<<endl;

	s = "abc";
	s[0] = 'A';
	cout<<s<<endl;

	return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值