C++中string重要函数示例

1.遍历加修改

void test_string1()
{
	string s1("hello world");

	//1.下标加[]
	for (size_t i = 0; i < s1.size(); ++i)
	{
		s1[i] += 1;
	}
	for (size_t i = 0; i < s1.size(); ++i)
	{
		cout << s1[i] << " ";
	}
	cout << endl;
	//i f m m p !x p s m e

	//2.迭代器
	string::iterator it = s1.begin();
	while (it != s1.end())
	{
		*it -= 1;
		++it;
	}

	//不能重定义it。即:string::iterator it = s1.begin();
	it = s1.begin();
	while (it != s1.end())
	{ 
		cout << *it << " ";
		++it;
	}
	cout << endl;
	//h e l l o   w o r l d

	//3.范围for,自动往后迭代,自动判断结束
	for (auto& e : s1) //for(char& e : s1)
	{
		e -= 1;
	}

	for (auto e : s1)
	{
		cout << e << " ";
	}
	cout << endl;
	//延伸:范围for的底层是:迭代器
	//g d k k n  v n q k c
}

2.反向迭代器:rbegin() 和 rend()

void test_string2()
{
	string s1("hello world");
	string::reverse_iterator rit = s1.rbegin();//auto rit = s1.rbegin();
	while (rit != s1.rend())
	{
		cout << *rit << " ";
		++rit;
	}
	cout << endl;
	//d l r o w   o l l e h
}
//迭代器的意义?
//对于string,无论是正着遍历,倒着遍历,下标加[]都足矣,为什么还要迭代器
//对于string,下标和[]就足够好用,确实可以不用迭代器
//但是如何是其他容器(数据结构),迭代器好用
//迭代器的意义:所有的容器都可以使用迭代器这种方式去访问修改
//例如:list,map,set是不支持下标加[]
//结论:
//对于string要会用迭代器,但是可以不用 

3.const 迭代器

void fun(const string& s)
{
	//必须使用const_iterator,否则会出现,类型无法转化的问题
	string::const_iterator it = s.cbegin();//auto it = s.begin();
	while (it != s.cend())
	{
		cout << *it << " ";
		++it;
	}
	cout<<endl;

	//反向const迭代器
	string::const_reverse_iterator ritb = s.crbegin();
	string::const_reverse_iterator rite = s.crend();
}

4.增容

void TestPushBack()
{
	string s;
	size_t sz = s.capacity();
	cout << "making s grow:\n";
	cout << "capacity changed: " << sz << '\n';
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		//s+='c';
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}
//making s grow :
//capacity changed : 15
//capacity changed : 31
//capacity changed : 47
//capacity changed : 70
//capacity changed : 105

//Linux下
//first : 0
//1
//2
//4
//8
//16
//32
//64
//128

5.reserve申请至少具体数值的容量

void Testreserve()
{
	string s;
	size_t sz = s.capacity();
	s.reserve(100);
	cout << "making s grow:\n";
	cout << "capacity changed: " << sz << '\n';
	for (int i = 0; i < 100; ++i)
	{
		s.push_back('c');
		//s+='c';
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << '\n';
		}
	}
}
//making s grow :
//capacity changed : 15
//capacity changed : 111
//因为牵扯到对齐的要求,所以,,,

6.resize 开空间,对这些空间给一个初始值,进行初始化

void Testresize()
{
	string s1;
	s1.reserve(10);
	cout << s1 << endl;

	string s2;
	s2.resize(10,'x');
	//不给值的情况下,会给一个 阿斯克吗值为0 的字符
	cout << s2 << endl;

	string s3("hello world");
	s3.reserve(20);
	cout << s3 << endl;

	string s4("hello world");
	s4.resize(20,'X');
	cout << s4 << endl;

	string s5("hello world");
	s5.resize(5);
	cout << s5 << endl;
}
//
//xxxxxxxxxx
//hello world
//hello worldXXXXXXXXX
//hello

7.c_str

void Testc_str()
{
	string s("hello world");
	cout << s << endl;//size是多少就是多少
	cout << s.c_str() << endl;//遇见\0就结束

	string file("test.txt");
	//用c语言的方式打开文件
	FILE* fout = fopen(file.c_str(), "w");
}

8.查 find

void testfind()
{
	string file("test.txt");
	//要求取出文件的后缀
	//找到字符串中第一个指定字符串位置
	size_t pos = file.find('.');
	if (pos != string::npos)
	{
		string suffix = file.substr(pos, file.size()-pos);
		//不写第二个数字就一直取到结尾
		cout << suffix << endl;
	}

	//要求取出文件的后缀
	string newfile("test.txt.zip");
	pos = newfile.rfind('.');
	if (pos != string::npos)
	{
		string suffix = newfile.substr(pos, newfile.size() - pos);
		cout << suffix << endl;
	}

	//要求将下面解析出三个
	string url("http://www.cplusplus.com/reference/string/string/find/");
	size_t pos1 = url.find(':');
	string protocol = url.substr(0, pos1);//协议
	cout << protocol << endl;

	size_t pos2 = url.find('/', pos1 + 3);
	string domain = url.substr(pos1 + 3, pos2 - pos1 - 3);//域名
	cout << domain << endl;

	string uri = url.substr(pos2 + 1);//资源定位
	cout << uri << endl;

}
//延伸:npos
//npos是-1
//但是npos是无符号整形,所以是一个很大的数字

// 延伸:网址构成
// http://   协议
// www.cplusplus.com 域名
// 后面是uri  资源定位

9.插入insert

void testinsert()
{
	string s("hello world");
	s += ' ';
	s += '!!!';
	cout << s << endl;

	//头插 效率低O(N)尽量少用
	s.insert(0, 1, 'x');
	s.insert(s.begin(), 'y');
	s.insert(0, "test");
	cout << s << endl;

	//中间位置插入
	s.insert(4, "xxxx");
	cout << s << endl;
}
//hello world !
//testyxhello world !
//testxxxxyxhello world !

10.删除

void testerase()
{
	string s("hello world");
	s.erase(0,1);
	cout << s << endl;
	s.erase(s.size() - 1, 1);
	cout << s << endl;
	s.erase(2, 2);
	cout << s << endl;

	//尾删也可以用 pop_back()
	s.pop_back();
	cout << s << endl;

	s.erase(3);
	cout << s << endl;
}
//ello world
//ello worl
//el worl
//el wor
//el 

11.compare

void Compare()
{
	string s1("hello world");
	string s2("string");
	cout << (s1 < s2) << endl;// 1

	cout << ("hhhh" < s2) << endl;// 1
	cout << (s1 < "sss") << endl;// 1
}

12.stoi

void Stoi()
{
	int val = stoi("1234");
	cout << val << endl;//1234

}
//stoi整形
//stol长整型
//stoul无符号整形
//stoll long long
//stoull无符号长整型
//stof float
//stod double
//stold long double

13.to_string

void stringto_string()
{
	string str = to_string(2.13);
	cout << str << endl;//2.130000
}
  • 12
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 11
    评论
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值