STL-容器的简单应用

#include<iostream>
using namespace std;

#if 0
#include<vector>
void TestVector1()
{
	vector<int> v1;  //空vector

	vector<int> v2(20); //指定大小为20 的vector

	//定义一个vector,用数组arr进行初始化
	int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	vector<int>v3(arr, arr + sizeof(arr) / sizeof(arr[0])); 

	cout << "v1_size = " << v1.size() << endl;  //元素个数
	cout << "v1_capacity = " << v1.capacity() << endl;  //容量
	cout << "v2_size = " << v2.size() << endl;
	cout << "v2_capacity = " << v2.capacity() << endl;
	cout << "v3_size = " << v3.size() << endl;
	cout << "v3_capacity = " << v3.capacity() << endl;

	//..
	v1 = v3;
	cout << "v1_size = " << v1.size() << endl;
	cout << "v1_capacity = " << v1.capacity() << endl;

	//..
	v3 = v2;
	cout << "v3_size = " << v3.size() << endl;
	cout << "v3_capacity = " << v3.capacity() << endl;

	//..
	cout << "v1 data: ";
	for (size_t i = 0; i < v1.size(); ++i)
		cout << v1[i] << ", ";
	cout << endl;

	cout << "v1 data: ";
	for (size_t i = 0; i < v1.capacity(); ++i)
		cout << v1[i] << ", ";
	cout << endl;	
	
	cout << "v1 data: ";
	for (int i = v1.size() - 1; i >= 0; i--) //不能是size_t类型
		cout << v1[i] << ", ";
	cout << endl;
}

void TestVector2()
{
	//尾插
	vector<int> v;
	v.push_back(1);
	v.push_back(2);
	v.push_back(3);
	v.push_back(4);
	v.push_back(5);
	cout << "v_size = " << v.size() << endl;
	cout << "v_capacity = " << v.capacity() << endl;

	for (size_t i = 0; i < v.size(); ++i)
		cout << v[i] << ", ";
	cout << endl;

	//尾删
	v.pop_back();
	v.pop_back();
	cout << "v_size = " << v.size() << endl;
	cout << "v_capacity = " << v.capacity() << endl;

	for (size_t i = 0; i < v.size(); ++i)
		cout << v[i] << ", ";
	cout << endl;

	//任意位置插入
	v.insert(v.begin(), 0); //在起始位置插入0
	v.insert(v.end(), 4, 5);  //在末尾插入3个4
	cout << "v_size = " << v.size() << endl;
	cout << "v_capacity = " << v.capacity() << endl;
	vector<int>::iterator it = v.begin();
	while (it != v.end())
		cout << *it++ << ", ";
	cout << endl;

	//任意位置删除
	v.erase(v.begin());
	v.erase(v.begin(), v.begin() + 3); //删除一个区间的元素[ )
	it = v.begin();
	while (it != v.end())
		cout << *it++ << ", ";
	cout << endl;
}

void TestVector3()
{
	//vector<int> v;
	//v.push_back(1);
	//v.push_back(2);
	//v.push_back(3);
	//v.push_back(4);
	//v.push_back(5);
	//cout << "v_size = " << v.size() << endl;
	//cout << "v_capacity = " << v.capacity() << endl;
	//vector<int>::iterator it = v.begin();
	//while (it != v.end())
	//	cout << *it++ << ", ";
	//cout << endl;

	assign()是给vector进行赋值
	在赋值前,先将旧元素erase掉,再将新元素插入进去
	//v.assign(7, 10); //给vector10个10
	//it = v.begin();
	//while (it != v.end())
	//	cout << *it++ << ", ";
	//cout << endl;
	//cout << "v_size = " << v.size() << endl;
	//cout << "v_capacity = " << v.capacity() << endl;

	
	v.clear(); //将元素清空, 但容量不变 
	cout << "v_size = " << v.size() << endl;
	cout << "v_capacity = " << v.capacity() << endl;

	..
	//int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	//vector<int>v1(arr, arr + sizeof(arr) / sizeof(arr[0]));
	//cout << "v1_size = " << v1.size() << endl;
	//cout << "v1_capacity = " << v1.capacity() << endl;
	v.resize(n, data) 将vector中的元素缩小到n个
	第二个参数可以不用传,默认情况下使用缺省的
	缩小的是size, 容量不变
	//v1.resize(5); 
	//cout << "v1_size = " << v1.size() << endl;
	//cout << "v1_capacity = " << v1.capacity() << endl;
	//it = v1.begin();
	//while (it != v1.end())
	//	cout << *it++ << ", ";
	//cout << endl;

	int arr[] = { 10,10,10,10,10,10,10,10,10,10 };
	vector<int>v2(arr, arr + sizeof(arr) / sizeof(arr[0]));
	v2.resize(5, 1);  //缩小的时候第二个参数没用
	cout << "v2_size = " << v2.size() << endl;
	cout << "v2_capacity = " << v2.capacity() << endl;
	vector<int>::iterator it = v2.begin();
	while (it != v2.end())
		cout << *it++ << ", ";
	cout << endl;

	it = v2.begin();
	v2.resize(8);  //第二个参数缺省默认用0
	cout << "v2_size = " << v2.size() << endl;
	cout << "v2_capacity = " << v2.capacity() << endl;
	v2.begin();
	while (it != v2.end())
		cout << *it++ << ", ";
	cout << endl;

}

int main()
{
	//TestVector1();
	//TestVector2();
	//TestVector3();
	return 0;
}
#endif

#if 0
#include<list>
//带头双向循环链表

void TestList1()
{
	list<int> l1;

	list<int> l2(10, 1);

	int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	list<int> l3(arr, arr + sizeof(arr) / sizeof(arr[0]));

	list<int> l4(l3);

	cout << "l1_size: " << l1.size() << endl;
	cout << "l2_size: " << l2.size() << endl;
	cout << "l3_size: " << l3.size() << endl;
	cout << "l4_size: " << l4.size() << endl;

	//链表为空则什么都没有
	list<int>::iterator it1 = l1.begin();
	while (it1 != l1.end())
		cout << *it1++ << "->";
	cout << endl;

	list<int>::iterator it2 = l2.begin();
	while (it2 != l2.end())
		cout << *it2++ << "->";
	cout << endl;

	list<int>::iterator it3 = l3.begin();
	while (it3 != l3.end())
		cout << *it3++ << "->";
	cout << endl;

	list<int>::iterator it4 = l4.begin();
	while (it4 != l4.end())
		cout << *it4++ << "->";
	cout << endl;

	list<int>::reverse_iterator it5 = l4.rbegin();
	while (it5 != l4.rend())
		cout << *it5++ << "->";
	cout << endl;
}

void TestList2()
{
	list<int> l;
	l.push_back(1);
	l.push_back(2);
	l.push_back(3);
	l.push_back(4);
	l.push_back(5);
	l.push_back(6);

	list<int>::iterator it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	//..
	l.pop_back();
	l.pop_back();
	l.pop_back();
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	//..
	l.push_front(0);	
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	//
	l.pop_front();
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	//任意位置插入
	list<int>::iterator pos = find(l.begin(), l.end(), 3);
	if (pos != l.end())
		pos = l.insert(pos, 0);
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	//..
	l.erase(pos);
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	l.assign(arr, arr + sizeof(arr) / sizeof(arr[0]));
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;
	cout << "获取链表中第一个元素:" << l.front() << endl;
	cout << "获取链表中最后一个元素:" << l.back() << endl;
}

class Odd
{
public:
	bool operator()(int value)
	{
		return value & 0x01;
	}
};

void TestList3()
{
	int arr[] = { 1,2,3,4,5,6,7,8,9,0 };
	list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));
	list<int>::iterator it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	l.remove(3);
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	l.remove_if(Odd());
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;
}

class Three
{
public:
	bool operator()(int left, int right)
	{
		return 0 == (left + right) % 3;
	}
};

void TestList4()
{
	int arr[] = { 1,2,3,4,5,1,2,3,4,5,6,7,8,9,0 };
	list<int> l(arr, arr + sizeof(arr) / sizeof(arr[0]));
	list<int>::iterator it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	l.unique();
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	l.sort();
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	// unique:将list中连在一起的相同数据删除,只保留第一个
	l.unique();
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;

	//如果list中连续两个数之和为3的倍数时,删除第二个数
	l.unique(Three());
	it = l.begin();
	while (it != l.end())
		cout << *it++ << "->";
	cout << endl;
}

// merge:将2个已序链表合并成一个链表,合并好之后依然有序
// reverse:链表的逆置
void TestList5()
{
	list<int> l1;
	l1.push_back(1);
	l1.push_back(4);
	l1.push_back(6);
	list<int> l2;
	l2.push_back(2);
	l2.push_back(4);
	l2.push_back(7);

	// 将两个已序链表合并成一个链表,合并好之后依然有序
	l1.merge(l2);
	list<int>::iterator it = l1.begin();
	while (it != l1.end())
		cout << *it++ << "->";
	cout << endl;

	l1.reverse();
	it = l1.begin();
	while (it != l1.end())
		cout << *it++ << "->";
	cout << endl;
}

int main()
{
	//TestList1();
	//TestList2();
	//TestList3();
	//TestList4();
	TestList5();
	return 0;
}
#endif


#include<string>

void TestString1()
{
	string s1;
	string s2("hello lc");
	string s3(10, 'a');
	string s4(s2); 
	string s5(s3, 5);  //用s3中前5个字符构造s5
}

void TestString2()
{
	string s("hello bit!!!");
	cout << s.length() << endl;
	cout << s.size() << endl;
	cout << s.max_size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	//清空,size 清0, 不改变吗底层空间的大小
	s.clear();
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	//将有效字符个数增加到10个,多处位置用'a'填充
	s.resize(10, 'a');
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	//将有效字符个数增加到15个,多处位置用缺省值'\0'填充
	s.resize(15);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;

	//将s中的有效字符个数缩减到5个
	s.resize(5);
	cout << s.size() << endl;
	cout << s.capacity() << endl;
	cout << s << endl;
}

void TestString3()
{
	string s;
	s.reserve(100);  //不改变有效字符个数
	cout << s.size() << endl;
	cout << s.capacity() << endl;

	s.reserve(50);
	cout << s.size() << endl; //不改变底层空间大小
	cout << s.capacity() << endl;
}

void TestString4()
{
	string s1("hello Bit");
	const string s2("hello Bit");
	cout << s1 << " " << s2 << endl;
	cout << s1[0] << " " << s2[0] << endl;
	cout << &s1 << " "<< &s2 << endl;

	s1[0] = 'H';
	cout << s1 << endl;
	for (size_t i = 0; i < s1.size(); ++i)
		cout << s1[i] << endl;

	//s2[0] = 'H'; //编译失败,s2不能修改
}

void TestString5()
{
	string str;
	str.push_back(' ');
	str.append("hello"); //追加一个字符串 "hello"
	str += 'b'; //在后面追加一个'b'
	str += "it"; //在后面追加体格 字符串"it"
	cout << str << endl;
	cout << str.c_str() << endl; //以C语言方式打印字符串

	//获取file的后缀
	string file1("string.cpp");
	size_t pos = file1.rfind('.');
	string suffix(file1.substr(pos, file1.size() - pos));
	cout << suffix << endl;

	//npos是string里面一个静态成员变量
	//static const size_t npos = -1;
	//去出url中的域名
	string url("http://www.cplusplus.com/reference/string/string/find");
	cout << url << endl;
	size_t start = url.find("://");
	if (start == string::npos)
	{
		cout << "invalid url" << endl;
		return;
	}
	start += 3;
	size_t finish = url.find('/', start);
	string address = url.substr(start, finish - start);
	cout << address << endl;

	//删除ur的协议前缀
	pos = url.find("://");
	cout << url[pos + 3] << endl;
	url.erase(0, pos + 3);  //[ )
	cout << url << endl;
}

void TestPushBack()
{
	string s;
	size_t sz = s.capacity();
	cout << "making s grow:\n";
	for (int i = 0; i < 100; ++i)
	{
		s += 'c';
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << endl;
		}
	}
	//31 47 70 105
	//  16  23  35 每次扩容增加上次容量的一半
}

//利用reserve提高插入数据的效率,避免增容带来的开销
void TestPushBack_p()
{
	string s;
	s.reserve(100);
	size_t sz = s.capacity();
	cout << "making s grow : \n";
	for (int i = 0; i < 100; ++i)
	{
		s += 'c';
		if (sz != s.capacity())
		{
			sz = s.capacity();
			cout << "capacity changed: " << sz << endl;
		}
	}
	cout << s.capacity() << "   " << s.size() << "   " << sz << endl;
	
	string s1("licha");
	string s3("lichao");
	cout << s1.compare(s3) << endl;
	cout << (s1 < s3) << endl;
}

int main()
{
	//TestString1();
	//TestString2();
	//TestString3();
	//TestString4();
	//TestString5();
	//TestPushBack();
	TestPushBack_p();
	return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值