C++ string

#include
#include<stdio.h>
#include<string.h>
#include
using namespace std;

void test()
{
string str;//""
string str2(“123”);//“123”
string str3 = “abc”;//“abc”
string str4(“0123456789”, 5);//“01234”
string cpy(str3);//“abc”

string str5(str4, 2, 2);//"23"
string str6(10, 'a');//"aaaaaaaaaa"

}

void test4()
{
string str(“0123456789”);
//迭代器的遍历方式
string::iterator it = str.begin();
while (it != str.end())
{
//迭代器的解引用
cout << *it << " ";
//修改值
//*it = ‘a’;
//迭代器向后移动
++it;
}
cout << endl;

it = str.begin();
while (it != str.end())
{
	//迭代器的解引用
	cout << *it << " ";
	//迭代器向后移动
	++it;
}
cout << endl;

cout << "fan" << endl;
string::reverse_iterator rit = str.rbegin();
while (rit!=str.rend())
{
	cout << *rit << " ";
	//反向迭代器 向前移动
	++rit;
}
cout << endl;

cout << "const_iterator" << endl;
string::const_iterator cit = str.begin();
while (cit != str.end())
{
	cout << *cit << " ";
	//const 迭代器不支持写操作,其为只读迭代器
	//*cit = '1';
	++cit;
}
cout << endl;
//vector<int> vec ={ 0,1,2,3,4,5,6,7,8,9 };
//vector<int>::iterator vit = vec.begin();
//while (vit != vec.end())
//{
//	cout << *vit << " ";
//	++vit;
//}
//cout << endl;

}

void printString(const string& str)
{
string::const_iterator it = str.begin();
while (it != str.end())
{
cout << *it << " ";
//*it = ‘a’;
++it;
}
cout << endl;
}

void test5()
{
string str(“0123456789”);
//size:返回有效字符的个数
int ret = str.size();
//length:返回有效字符的个数
ret = str.length();
//capacity:可以存放的最大有效字符个数
ret = str.capacity();
ret = str.max_size();

//只清空内容,不改变容量
str.clear();
ret = str.size();
ret = str.capacity();

}

void test6()
{
string str(“0123456788”);
int size = str.size();
int cap = str.capacity();
//resize(n)
//1.当n>size, 且n>capacity; 增容(开新的空间+拷贝+释放原有空间)+新的位置赋值(如果没有给赋值字符,默认赋值’\0’)+修改size
str.resize(20);
size = str.size();
cap = str.capacity();
//1.当n<size:只修改size
str.resize(10);
size = str.size();
cap = str.capacity();
//3.当n>size,且n<capacity:新的位置赋值(如果没有给赋值字符,默认赋值’\0’)+修改size
str.resize(30, ‘a’);
size = str.size();
cap = str.capacity();

}

void test7()
{
string str(“0123456788”);
int size = str.size();
int cap = str.capacity();

//reserve 调整容量 一定不修改size和内容,如果减小容量,是按需减小
//如果要减少的容量小于size,不做任何操作,如果减小的容量大于size,则进行减小的操作
str.reserve(20);
size = str.size();
cap = str.capacity();

str.reserve(5);
size = str.size();
cap = str.capacity();

}

void test8()
{
string s;
//容量和实际申请空间可能不同:字符串以’\0’结束
//PJ版 string 增容过程:如果为空字符串对象,初识容量大小为15,第一次增容2倍,后续按照1.5倍增加
int size = s.size();
int cap = s.capacity();
cout << “初容量” <<cap<< endl;
for (int i = 0; i < 200; i++)
{
s.push_back(i);
if (cap != s.capacity())
{
cap = s.capacity();
cout << cap << endl;
}
}
}

void test9()
{
string s;
s.reserve(100);
int size = s.size();
int cap = s.capacity();

s.reserve(10);
size = s.size();
cap = s.capacity();

s.shrink_to_fit();
size = s.size();
cap = s.capacity();

s.reserve(10000);
size = s.size();
cap = s.capacity();

s.shrink_to_fit();
size = s.size();
cap = s.capacity();

}

void test10()
{
string str(“0123456789”);
//1.迭代器
string::iterator it = str.begin();
while (it != str.end())
{
cout << *it << " ";
++it;
}
cout << endl;
//operator[];可读可写接口
for (int i = 0; i < str.size(); ++i)
{
cout << str.operator[][i] << " ";
str[i] = ‘a’;
}
cout << endl;
//2.operator[]简写
for (int i = 0; i < str.size(); i++)
{
cout << str[i] << " ";
}
cout << endl;

//at可读可写,但是可读性不强
for (int i = 0; i < str.size(); i++)
{
	cout << str.at(i) << " ";
	cout << endl;
}

//str.at(100)='c'
//3.范围for遍历:支持读写  如果需要修改,则接受类型为引用类型
for (auto& ch : str)
{
	cout << ch << " ";
	ch = 'X';
}
cout << endl;

}
int main()
{
test8();
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值