string类size()、length()、capacity()、reverse()、push_back()、append()、insert()、erase()、substr()等相关函数用法

测试了17个关于string类的函数 结果见注释 

#include <iostream>
using namespace std;


#include <string>

void TestString1()
{
	string s1;
	string s2("hello");
	string s3(s2);
	string s4("hello", 3); //输出前三个字符 即hel
	string s5(10, 'A');

	cout << s2 << endl;
	cin >> s1;
	cout << s1 << endl;
}

// 容量相关
void TestString2()
{
	string s("hello");
	cout << s.size() << endl; //5
	cout << s.length() << endl; //5
	cout << s.capacity() << endl; //15

	// s如果是空字符串 "" 返回true  否则返回false
	if (!s.empty())
	{
		cout << s << endl;//hello
	}

	s.clear();
	if (s.empty())
	{
		cout << "空字符串" << endl;//空字符串
	}
}

// reserve: 扩容,不会改变有效元素的个数
// reserve(size_t newcapacity)
// 假设当前扩容的string对象底层旧容量oldcapacity
// newcapacity > oldcapacity:reserve方法才会真正扩容
// newcapacity < oldcapacity
//   newcapacity >= 16: reserve不会将空间缩小的
//   newcapacity <= 15: reserve才会将空间缩小--15
void TestString3()
{
	string s("hello");

	s.reserve(20);  // 31
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//31

	s.reserve(30);
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//31

	s.reserve(40);   // 47
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//47

	s.reserve(50);   // 70
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//70

	s.reserve(60);   // 70
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//70

	s.reserve(50);     // 70
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//70

	s.reserve(40);   // 70
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//70

	s.reserve(30);   // 70
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//70

	s.reserve(20);   // 70
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//70

	s.reserve(15);     // 15
	cout << s.size() << endl;//5
	cout << s.capacity() << endl;//15
}

/*
char* 
size_t size
size_t capacity
总共占用12字节
*/

void TestString4()
{
	cout << sizeof(string) << endl; //输出40
}

// void resize(size_t newsize, char ch)
// void resize(size_t newsize)
// newsize: 将string对象中有效字符个数修改到newsize个
// ch: 如果是增多,多出的元素使用ch填充
// 将string对象中有效元素个数增加到newsize,多出的位置使用ch填充
// 注意:在增多的过程中可能会扩容
//      在减少的过程中容量不变
void TestString5()
{
	string s("hello");   // size:5  capacity:15
	s.resize(10, '!');   // size:10  capacity:15
	s.resize(20, '$');   // size:20  capacity:31
	s.resize(30, '%');   // size:30  capacity:31
	s.resize(20, '8');   //截断了 没有用8替换 
	s.resize(8);         //hello!!!
	s.resize(3);         //hel
}

/*
1. 可以知道string内部的扩容机制
2. 如果大概知道要往string中放多少个元素,在push_back之前
   可以提前先将容量给好,否则一边插入一边扩容效率非常低
*/
void TestString6()
{
	string s;
	// s.reserve(100);
	size_t cap = s.capacity();
	for (size_t i = 0; i < 100; ++i)
	{
		s.push_back('A');//s.capacity()达到一定的值会改变
		if (cap != s.capacity())
		{
			cap = s.capacity();
			cout << cap << endl;
		}
	}
}
//输出31 47 70 105

#include <assert.h>
void TestString7()
{
	string s("hello");

	cout << s[0] << endl;  //h
	s[0] = 'H';
	cout << s << endl;    //Hello

	cout << s.at(0) << endl; //H
	s.at(0) = 'h';
	cout << s << endl;      //hello

	//cout << s[100] << endl;    // []越界:assert
	//cout << s.at(100) << endl; // at越界:exception: std::out_of_range 
}


void TestString8()
{
	string s("hello");
	s += ' ';
	s += "world";

	string ss("!!!");
	s += ss;

	s.append(10, 'A');
	cout << s << endl;//hello world!!!AAAAAAAAAA
}

void TestString9()
{
	string s("world");
	s.insert(s.begin(), ' ');
	s.insert(0, "hello");
	cout << s << endl;//hello word
}


void TestSTring10()
{
	string s("11122233333333333334444");
	s.erase(0, 3);
	cout << s << endl;       //22233333333333334444
	s.erase(s.begin()+s.find('3'), s.begin()+s.rfind('3')+1); //删掉了中间的3
	cout << s << endl;       //2224444
}

// 获取一个文件的后缀
void TestSTring11()
{
	string filename("1.t22111.txt");
	cout << filename.substr(filename.rfind('.') + 1) << endl; //输出txt
}

// 一行单词,单词和单词之间使用空格隔开,最后一个单词的长度
void TestSTring12()
{
	string words;
	// cin >> words;
	while (getline(cin, words))
	{
		cout << words.substr(words.rfind(' ') + 1).size() << endl;//输出空格后面字符个个数
	}
}

// 遍历
void TestString13()         //三次正向输出 两次反向输出
{         
	int array[] = { 1, 2, 3 };
	array[0];

	string s("hello");
	// 借助下标+[]
	for (size_t i = 0; i < s.size(); ++i)
	{
		cout << s[i];  // char& operator[](size_t index)
	}
	cout << endl;

	// 范围for
	for (auto e : s)
		cout << e;
	cout << endl;

	// 使用正向迭代器
	string::iterator it = s.begin();
	while (it != s.end())
	{
		cout << *it;
		++it;
	}
	cout << endl;

	// string::reverse_iterator rit = s.rbegin();
	auto rit = s.rbegin();
	// auto a = 10;  int a = 10;
	while (rit != s.rend())
	{
		cout << *rit;
		++rit;
	}
	cout << endl;

	reverse(s.begin(), s.end());
	cout << s << endl;

}


// https:\www.baidu.com\index.html
void TestString14()
{
	string s("https:/www.baidu.com/index.html");
	cout << s.find("www") << endl; //7
	cout << s.find("com") << endl;  //17
	
	size_t pos = s.find("www");
	size_t end = s.find("com");
	if (end != string::npos)
	{
		cout << s.substr(pos, end + 3 - pos) << endl; //www.baidu.com  17+3-7=13 即从w开始截取了13个字符
	}
	else
	{
		cout << "com不存在" << endl;
	}
}

void TestString15()
{
	string s("1234");

	//将字符串转换为整形,atoi需要字符指针作为参数
	int value = atoi(s.c_str());
	cout << value << endl;  //1234
	cout << typeid(value).name() << endl; //int
}

// oj: 给一个单词,返回该单词的长度
void TestString16()
{
	// 一次性接收一个字符串---cin在接收字符串时遇到空格 、回车等空白字符停止接收
	//string word;
	//while (cin >> word)
	//{
	//	cout << word.size() << endl;
	//}

	// 一次性要接收一行
	string words;
	while (getline(cin, words))
	{
		cout << words << endl;
	}
}

#include <algorithm>

void TestString17()
{
	string s1("hello");
	string s2("World");
	if (s1 < s2)
	{
		cout << "s1 < s2" << endl;
	}
	else
	{
		cout << "s1 >= s2" << endl; //输出s1>=s2 注意W是大写
	}

	string ss[] = { "hello", "world", "bit", "aaa" };
	sort(ss, ss+sizeof(ss) / sizeof(string)); //按首字母排序 注意是a小z大
}


void Testisalpha()
{
	cout << isalpha('A') << endl; //输出1
	cout << isalpha('-') << endl; //输出0
}

int main()
{
	 //TestString1();
	 //TestString2();
	 //TestString3();
	 //TestString4();
	 //TestString5();
	 //TestString6();
	 //TestString7();
	 //TestString8();
	 //TestString9();
	 //TestSTring10();
	 //TestSTring11();
	 //TestSTring12();
	 //TestString13();
	 //TestString14();
	 //TestString15();
	 //TestString16();
	 //TestString17();
	   Testisalpha();
	   return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值