STL相关的练习代码

#include <iostream>
#include <string>//需要包含该头文件才能使用 getline()
#include <vector>
#include <list>
#include <stack>
#include <queue>


using namespace std;

//template<typename T>//后面不添分号,将其与函数定义时作类比
template<class T>//使用 class 或者 typename 都可以
void my_swap(T &left, T &right)
{
	T tmp = left;
	left = right;
	right = tmp;

	cout << "交换后的数值:" << endl << left << " " << right << endl;
}

template<class T>
T my_add(const T &left, const T &right)
{
	return left + right;
}

int main()
{
	/*TEST 1:*/
	//cout << "hello" << endl;
	//string s1 = "hello";//拷贝初始化
	//cout << s1 << endl ;
	//string s2 = s1;
	//cout << s2 << endl;
	//string s3(5, 'a');
	//cout << s3;
	//string s4("hello");//直接初始化

	/*TEST 2:*/
	/*string s5;
	getline(cin, s5);
	cout << s5;*/

	/*TEST 3:*/
	//string s3("hello bit");
	cout << s3.size() << endl << s3.empty() << endl;//其 size 不会返回带有\0的字符个数

	//if (s3.size() > 4)
	//{
	//	cout << "数字4发生整形提升" << endl;//注意size()的返回类型是无符号类型
	//}
	//else
	//{
	//	cout << "没有发生整型提升" << endl;
	//}

	/*TEST 4:*/
	/*string s4(5, 'a'), s5;
	s5 = s4;
	cout << s5 << endl;*/

	/*TEST 5:俩个string对象相加*/
	//string s5(5, 'a'), s6(3, 'b'),end;
	/*end = s5 + s6;
	cout << end << endl;*/
	/*s5 = s5 + s6;
	cout << s5 << endl;*/

	//string s7="hello", s8="bit";//因为是从字面常量区拷贝过来的,所以可以对其修改
	//s7 = s7 + s8;
	//cout << s7;

	/*TEST 6:*/
	//string s6("hello bit");
	///*for (auto i : s6)
	//	cout << i ;*/
	//for (auto &i : s6)
	//{
	//	i = toupper(i);//大写函数的调用
	//	cout << i;
	//}

	/*TEST 7:*/
	//string s7("hello bit");
	cout << s7.empty();//其如果字符串的长度为0,则empty()返回0
	//cout << s7[5] << endl;
	//string s8;
	//cout << s8[3] << endl;//对与string类对象的索引必须要确保其string类不为空

	/*TEST 8:*/
	//vector<int> v8;//vector的使用需要引头文件
	//float num = 3.141592;
	int x{ num };转换不会被执行,因为这是初始化列表
	//int x(num);//转换会被执行
	//cout << x;

	/*TEST 9:*/
	/*vector<int> v9(3);
	cout << v9[0] << endl;*/
	/*vector<string> v3(3);
	cout << v3[0];*///此时程序会执行值初始化,根据vector元素的类型来执行默认初始化

	/*TEST 10:*/
	/*vector<int> v10(3);
	cout << v10[2] << endl;*/

	/*vector<int> v10{ 1, 2, 3 };*/
	//cout << v10[2] << endl;

	/*vector<string> v11{ "hello" };
	cout << v11 << endl;

	vector<string> v12{ 5, "hello" };
	cout << v12;*/

	/*TEST 11:*/
	//vector<int> v11;
	//for (int i = 0; i < 10; i++)
	//{
	//	v11.push_back(i);
	//	//cout << v11[i] << endl;
	//}

	//cout << v11.size() << endl;

	/*TEST 12:*/
	//vector <int> v12(3);
	//cout << v12[3];//发生了越界

	/*TEST 13:*/
	//vector<int> v13;
	//for (int i = 0; i < 4; i++)
	//{
	//	v13.push_back(i);
	//}
	v13::iterator it;
	//vector<int> ::iterator it;
	//for (it = v13.begin(); it != v13.begin(); it++)
	//{
	//	cout << *it << endl;
	//}

      /*TEST 14:vector 的迭代器*/
  //    vector<int> v;
	 // for (int i = 0; i < 4; i++)
	 // {
		//  v.push_back(i);
		//  //cout << v[i] << endl;
	 // }

	 // //cout << v.capacity() << endl;
	  v.pop_back();
	 // 

	 // vector<int> :: const_iterator it,end;
	 // it = v.begin();
	 // //cout << *(it+2) << endl;
	 // //cout << it[2] << endl;//迭代器作数组名
	 // //for ( auto it = v.begin(); it != v.end(); it++)//要想输出 vector 里面的全部元素,尾迭代器end()就不能减去1
	 // //{
		// // //cout << *it+1 << endl;
		// // *it = 8;
		// // cout << *it;
	 // //}
	 // //end = v.end();
	  //cout << end - it;//计算出vector中元素的长度
	  
	  

	  /*TEST 15:string 的迭代器*/
	  //string s("hello bit");
	  //string::iterator it;//每次使用迭代器之前都需要先声明其所在的命名空间
	  //for (auto it = s.begin(); it != s.end(); it++)
	  //{
		 // cout << *it ;
	  

//vector<string> v{ "hello", "bit" };
//vector<string> ::iterator it;
//it = v.begin();
cout << *it;
cout << (*it).empty();//不为空则返回0,其中迭代器的解引用必须要用括号括起来
cout << it->empty();
//cout << *(it + 1) << endl;

             /*TEST 16:list*/
             //forward_list; 
			 //list<int> l;
			 //for (int i = 0; i < 4; i++)
			 //{
				// //l[i] = i;
				// l.push_back(i);//在list尾部插入值为i的元素
				// //cout << l[i] << endl;这是错误的写法
			 //}

			 //for ( auto e : l)
			 //{
				// cout << e << endl;
			 //}

			 //cout << "---------" << endl;

			 //list<int> :: iterator it, end;
			 //it = l.begin();
			 //end = l.end();
			 //l.swap(it, end);

			 

			 for (auto e : l)
			 {
				 cout << e << endl;
			 }

			 //l.push_front(9);
			 //it = l.begin();//如果不使用范围for循环去输出头插后的链表时,那么就要更新头部的迭代器
			 如果是有多个头插,那么很容易忽略最后的头部迭代器更新
			 //while (it != end)
			 //{
				// cout << *it << endl;
				// it++;
			 //}

			 //cout << "-----------" << endl;

			 //l.push_front(10);
			 //l.push_front(11);
			 //l.push_front(12);
			 //l.pop_back();

			 //for (auto e : l)//当容器中的元素是像类这种占据较大内存的元素时,尽量使用引用,避免拷贝带来程序执行效率上的降低
			 //{
				// cout << e << endl;
			 //}

			 //cout << "----------" << endl;
			 //auto pos = find(l.begin(), l.end(), 0);//find的返回值也是一个迭代器
			 //if (pos != l.end())
			 //{
				// l.insert(pos, 100);//insert 之后的 pos 不会失效
				// l.erase(pos);//erase之后的 pos 会失效,因为其结点被释放了,造成了野指针问题
			 //}

			 //for (auto e : l)
			 //{
				// cout << e << endl;
			 //}

			 //cout << "-------------" << endl;

			 l.swap(l.begin());
			 //for (auto e : l)
			 //{
				// cout << e << endl;
			 //}

			 /*sort 默认排升序,不能直接使用算法库里的sort,
			 因为有前提:其要排序的元素所在的物理内层空间必须是连续的,
			 所以只能使用容器里的sort*/

			 /*直接使用链表排序后的效率不如在vector中排序后再拷贝回去来的快*/

			 /*cout << l.empty() << endl;
			 cout << l.size() << endl;
*/
			 /*list<int> ::iterator it;
			 cout << *it << endl;*/

			 /*对于链表的迭代器,其 it = l.begin(),指向的是第一个结点的地址,对其 it 进行解引用而
			 得到第一个结点的地址,而不是其数据,
			 不同于 vector 和 string,
			 对其 it 进行 ++ 时,链表中不会到达下一个结点,
			 因为内存空间不连续,
			 这就是 list 迭代器与 vector 和 string 的一个不同点,
			 而要想解决这个问题,
			 就需要对函数的封装和对运算符的重载*/

             /*TEST 17 :list 的浅拷贝*/
             /*list<int> l;
			 l.push_back(0);
			 l.push_back(1);
			 l.push_back(2);
			 l.push_back(3);

			 list<int> ::iterator it = l.begin(),end = l.end();
			 for (auto e : l)
			 {
				 cout << e << endl;
			 }

			 cout << "---------" << endl;*/

			 //list<int> copy = l;//浅拷贝,只是将链表l所在的地址拷贝进了 copy 中
			 ///*而当析构函数运行时,链表中的结点就会被释放俩次(因为不能free俩次),从而造成程序崩溃*/
			 //list<int>::iterator ct = copy.begin();
			 //cout << *ct << endl;
			 //cout << "-------" << endl;

			 //for (auto e : copy)/*这里可以运行浅拷贝后的正确结果,注意浅拷贝也可以修改值,
				//				但是copy修改后原链表l里的值也变了,
				//				而它们二者本应该是独立的*/
				// cout << e << endl;
			 //}

			 //cout << "---------" << endl;

			 /*clear与析构函数的区别:
			 clear只是将数据清除,
			 而析构直接是销毁一切相关的*/

             /*TEST 18 : */
             /*栈和队列没有其迭代器,
			 即也就不能使用 范围for循环了,
			 因为范围for循环的本质就是迭代器;
			 因为如果要适配对应的迭代器,则保持不了自身的特性,
			 理由是:
			 栈是“先进后出”,队列是“先进先出”的结构特点
			 
			 本质上说:stack 与 queue 已经不属于容器的范畴了,
			 而是容器适配器。*/

    //         stack<int> st;
			 //st.push(0);
			 //st.push(1);
			 //st.push(2);
			 //st.push(3);

			

			 //cout << st.empty() << endl;
			 //cout << "---------" << endl;
			 //if (!st.empty())
			 //{
				// cout << st.top() << endl;//找到栈顶的元素

				// cout << "--------" << endl;
				// 
				// st.pop();//移除栈顶元素
				// 
				// cout << st.top() << endl;
			 //}

			 //cout << "---------" << endl;
			 利用 while 循环 + top() 来完成对 stack 里面元素的全部打印
			 //while (!st.empty())
			 //{
				// cout << st.top() << endl;
				// st.pop();//打印一个删一个,要不然就是死循环
			 //}

               /*TEST 19 :实现 queue 里全部元素的打印*/
      //         queue<int> q;

			   //q.push(0);
			   //q.push(1);
			   //q.push(2);
			   //q.push(3);

			   ///*if (!q.empty())
			   //{
				  // cout << q.front() << endl;
				  // cout << "-------" << endl;
				  // 
				  // q.pop();
				  // cout << q.front() << endl;
			   //}*/

			   输出打印 queue 中的全部元素
			   //while (!q.empty())
			   //{
				  // cout << q.front() << endl;
				  // q.pop();
			   //}

                /*TEST 20 : 反向迭代器*/
	   //         vector <int> v = {1,2,3,4};
				//vector<int> ::iterator it = v.begin(),end = v.end();
				//while (it != end)
				//{
				//	cout << *it << endl;
				//	it++;
				//}

				//cout << "---------" << endl;
				//vector<int> ::reverse_iterator rit = v.rbegin(),rend = v.rend();
				cout << *rit << endl;
				//while (rit != rend)
				//{
				//	cout << *rit << endl;
				//	rit++;
				//}

                /*TEST 21 : 模板 ---- 泛型编程,不光只有一种特定的类型*/
              /*int left = 2,right = 3;
               my_swap(left, right);

			   float left2 = 1.1, right2 = 2.2;
			   my_swap(left2, right2);*/

              /* cout << my_add(1, 2) << endl;
			   cout << my_add(1.0, 2.0) << endl;
			   cout << my_add(1, (int)2.0) << endl;
			   cout << my_add<double>(1.0, 2) << endl;
              */
//template<class T>
//class my_stack
//{
//private:
//	T *_a;
//	int capacity;
//	int top;
//};
//
//stack<int> st;
//stack<char> st2;

              /*TEST 22 : */
//class Time
//{
//public:
//	Time(int hour = 0)//缺省值的默认构造,因为有它的存在,编译器不会再生成默认构造函数了
//		:_hour(hour)
//		,_t(year)
//	{
//		_hour = hour;
//	}
//private:
//	int _hour;
//};
//
//class Date
//{
//private:
//	int _year;
//	Time _t;
//};

            /*TEST 23 : 赋值运算符的重载*/
//class Date
//{
//public:
//	Date(int year = 1, int month = 1, int day = 1)
//	{
//		_year = year;
//		_month = month;
//		_day = day;
//	}
//
//	void operator=(const Date &d1)//隐含参数 this 指针指向运算符=的左侧对象
//		//引用返回与引用传参可以减少拷贝构造的次数
//	{
//		_year = d1._year;
//		_month = d1._month;
//		_day = d1._day;
//	}
//
//	Date operator+(const int day)
//	{
//		Date ret = *this;
//		ret += day;
//		return ret;
//	}
//
//private:
//	int _year;
//	int _month;
//	int day;

           /*TEST 24 : 非类型模板参数*/
           template<class T,double n>
		   class A
		   {
		   };

};
//赋值与拷贝构造:
/*俩个存在的对象之间有=号则表示赋值*/
			 





	return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值