原创 linux下c++ lesson19 容器string

28 篇文章 0 订阅

1-string的构造.cpp

#include <iostream>
#include <string>

using namespace std;

int main()
{
	string s1;                        //无参构造函数
	string s2("helloworld");          //有参构造函数
	string s3(10, 'a');   
	string s4(s2);                    //拷贝构造函数

	cout << s1 << endl;               //重载了输出运算符
	cout << s2 << endl;
	cout << s3 << endl;
	cout << s4 << endl;

	cin >> s1;                        //重载了输入运算符
	cout << s1 << endl;

	s1 += "helloworld";
	if (s1 == s2)
	{
	}

	s1 = s1 + s2;

	return 0;
}

2-string存取.cpp

#include <iostream>
#include <exception>

using namespace std;

int main()
{
	string s("helloworld");

	cout << s[1] << endl;        //重载了下标运算符
	s[1] = 'x';

	cout << s << endl;

	cout << s.at(1) << endl;     //通过成员函数来访问

	//cout << s[20000] << endl;    //越界访问程序异常结束
	try{
		cout << s.at(10) << endl;      //越界访问会抛出异常
	}
	catch (exception &e)
	{
		cout << e.what() << endl;
	}

	return 0;
}

3-c_str.cpp

#include <iostream>
#include <string.h>
#include <string>

using namespace std;

int main()
{
	char buf[32] = {0};
	string s("helloworld");
	strcpy(buf, s.c_str());     //c_str()返回字符串的首地址
	cout << buf << endl;

	return 0;
}

4-copy.cpp

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
	char buf[32] = {0};

	string s("helloworld");
	s.copy(buf, 5);               //第三个参数是默认参数,拷贝的位置,默认是0
	cout << buf << endl;

	memset(buf, 0, sizeof(buf));
	s.copy(buf, 4, 5);
	cout << buf << endl;

	return 0;
}

5-string长度.cpp

#include <iostream>

using namespace std;

int main()
{
	string s("helloworld");

	cout << s.length() << endl;

	if (s.empty())
	{
		cout << "字符串是空" << endl;
	}
	else
	{
		cout << "字符串不为空" << endl;
	}
	return 0;
}

6-string赋值.cpp

#include <iostream>

using namespace std;

int main()
{
	string s1("helloworld");

	s1 = "hello";        //重载了=运算符
	cout << s1 << endl;

	const char *s = "this is test";
	s1.assign(s);
	cout << s1 << endl;

	s1.assign(s, 7);
	cout << s1 << endl;

	s1.assign(5, 'a');     //把5个a赋值给s1
	cout << s1 << endl;

	string s2("hey boy");
	s1.assign(s2);         //把对象s2赋值给s1
	cout << s1 << endl;

	s1.assign(s2, 4, 3);
	cout << s1 << endl;

	return 0;
}

7-string连接.cpp

#include <iostream>

using namespace std;

int main()
{
	string s1("helloworld");

	s1 += "1234";    //重载了+=运算符
	cout << s1 << endl;

	string s2("abcdefg");
	s1 += s2;
	cout << s1 << endl;

	const char *s = "haha";
	s1.append(s);
	cout << s1 << endl;

	s1.append(s, 2);
	cout << s1 << endl;

	s1.append(s2);
	cout << s1 << endl;

	s1.append(s2, 4, 2);
	cout << s1 << endl;

	s1.append(10, 'x');
	cout << s1 << endl;

	return 0;
}

8-string比较.cpp

#include <iostream>

using namespace std;

int main()
{
	string s1("helloworld");
	string s2("helloboy");
	const char *s = "hellogirl";

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

	if (s1.compare(s) > 0)
	{
		cout << s1 << " > " << s << endl;
	}
	return 0;
}

9-string子串.cpp

#include <iostream>

using namespace std;

int main()
{
	string s("helloworld");

	cout << s.substr() << endl;
	cout << s.substr(5, 5) << endl;

	return 0;
}

10-string查找和替换

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
	int p;
	string s1("helloworld");
	string s2("world");

	p = s1.find('o');
	cout << p << endl;

	p = s1.find('x');    //不存在返回-1
	cout << p << endl;

	p = s1.find('o', 5);
	cout << p << endl;

	p = s1.find("ll");
	cout << p << endl;

	p = s1.find(s2);
	cout << p << endl;

	p = s1.rfind('o');    //反向查找
	cout << p << endl;

	s1.replace(5, 5, "xxx");
	cout << s1 << endl;

	s1.replace(5, 3, s2);
	cout << s1 << endl;

	string s3("helloworldhelloworldhelloworldhelloworld");
	p = s3.find("world");
	while (p != -1)
	{
		s3.replace(p, strlen("world"), "x");
		p = s3.find("world", p + strlen("x"));
	}
	cout << s3 << endl;

	return 0;
}

11-插入和删除.cpp

#include <iostream>

using namespace std;

int main()
{
	string s1("helloworld");
	string s2("12345");

	s1.insert(0, "this is ");
	cout << s1 << endl;

	s1.insert(10, s2);
	cout << s1 << endl;

	s1.insert(0, 5, 'x');
	cout << s1 << endl;

	s1.erase(0, 20);
	cout << s1 << endl;

	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值