C++标准模板库-STL

C++标准模板库-STL

容器

顺序容器:
vector
list
deque 
适配器
statck 
queue
priority_queue

顺序容器

vector: 顺序表
insert();
push_back();
erase();
pop_back();
empty();
begin();
end();
…….
list: 链表
insert();
push_back();
erase();
pop_back();
empty();
front();
back();
sort();
deque: 双端
insert();
push_back();
erase();
pop_back();
empty();
Push_front();
 stack:  

     
适配器,它可以将任意类型的序列容器转换为一个堆栈,一般使用deque作为支持的序列容器。元素只能后进先出(LIFO)
push();
top();
pop();注意,出栈操作只是删除栈顶元素,并不返回该元素

queue:      

 
适配器,它可以将任意类型的序列容器转换为一个队列,一般使用deque作为支持的序列容器。元素只能先进先出(FIFO)
push();
front()/back();
pop();注意,出栈操作只是删除栈顶元素,并不返回该元素
 

关联容器:
map
set //tree
multimap
multiet
map:键值对(key/value)容器
map<string, double>  stu;
insert(   make_pair<string, double>(“john”,95.5)   );
stu[“keiven”] = 80.0;

cout<<“john    :  ”<<stu[“john”] <<endl;
cout<<“keiven :  ”<<stu[“keiven”] <<endl;
set:
set<int>  a;
a.insert(1);
a.insert(3);
a.insert(5);
if(a.end != a.find(3) )
	cout<<“have 3”<<endl;
if(a.end() !=  a.find(30) )
	cout<<“have 30”<<endl;

迭代器

iterator: “神奇的指针”

迭代器容器的作用是提供一种统一的方式来访问容器中的元素,而不需要了解具体容器的实现细节。它可以以统一的方式遍历容器中的元素,并可以进行增加、删除、修改等操作。

使用迭代器容器,可以通过调用容器对象的begin()方法获取一个指向容器中第一个元素的迭代器,通过调用end()方法获取一个指向容器中最后一个元素之后的位置的迭代器。然后可以使用迭代器的相关方法,如next()、previous()、hasNext()、hasPrevious()等来遍历容器中的元素或执行其他操作。

#include <iostream>
#include <ostream>

using namespace std;

class myList
{
	struct Node
	{
		Node(int x, Node* ptr = NULL) :data(x), next(ptr) {}
		int data;
		Node* next;
	};
public:
	class iterator//迭代器类
	{
	public:
		iterator(Node* ptr = NULL) :pos(ptr) {}
		iterator& operator++(int)//自加运算符重载
		{
			if (NULL != pos)
				pos = pos->next;
			return *this;
		}

		int& operator*()//指针运算符重载
		{
			return pos->data;
		}

		bool operator!=(iterator x)//判断迭代器是否相等 
		{
			return pos != x.pos;//判断迭代器里psd是否相等
		}
	private:
		Node* pos;
	};
public:
	myList() :head(NULL) {}
	~myList()
	{
		while (head)
		{
			Node* tem = head;
			head = head->next;
			delete tem;
		}
	}

	void insert_head(int data)
	{
		Node* node = new Node(data);
		node->next = head;
		head = node;
	}

	iterator begin()
	{
		return iterator(head);
	}
	iterator end()
	{
		return iterator(NULL);
	}


	friend ostream& operator<<(ostream& out, const myList& list);
private:
	Node* head;
};

ostream& operator<<(ostream& out, const myList& list)
{
	myList::Node* tem = list.head;
	while (tem)
	{
		out << tem->data << ',';
		tem = tem->next;
	}
	out << endl;

	return out;
}

int main()
{
	myList list;

	list.insert_head(1);
	list.insert_head(2);
	list.insert_head(4);
	list.insert_head(3);

	cout << list;

	myList::iterator i = list.begin();
	while (i != list.end())
	{
		cout << *i << endl;
		i++;
	}

}

vector迭代器是一种用于访问和操作vector容器中元素的迭代器。

要使用vector迭代器,首先需要包含头文件。

以下是几个常用的vector迭代器操作示例:

  1. 迭代器的声明和初始化:
vector<int> vec = {1, 2, 3, 4, 5};
vector<int>::iterator it; // 声明一个迭代器
  1. 遍历vector容器并输出元素:
for (it = vec.begin(); it != vec.end(); ++it) {
    cout << *it << " ";
}
  1. 修改vector容器中的元素:
for (it = vec.begin(); it != vec.end(); ++it) {
    *it *= 2;
}
  1. 在指定位置插入新元素:
it = vec.begin() + 2;
vec.insert(it, 10); // 在第三个位置插入元素10
  1. 删除指定位置的元素:
it = vec.begin() + 3;
vec.erase(it); // 删除第四个位置的元素

需要注意的是,当vector容器中的元素发生变化(如插入、删除元素),迭代器可能会失效,因此在对容器进行修改时,需要小心处理迭代器的使用。

map容器

#include <iostream>
#include <map>

using namespace std;

int main()
{
	map<string, string> user_passwd;//实现带有两个键值对存储的容器

	user_passwd.insert(user_passwd.begin(), pair<string, string>("aaa", "1111"));//查看STL手册查看C++容器的使用
	user_passwd.insert(user_passwd.begin(), pair<string, string>("aaa4", "114411"));
	user_passwd.insert(user_passwd.begin(), pair<string, string>("aaa2", "111331"));
	user_passwd.insert(user_passwd.begin(), pair<string, string>("aaa3", "111441"));

	map<string, string>::iterator i = user_passwd.begin();//定义一个迭代器
	while (i != user_passwd.end())
	{
		cout << (*i).first << ',' << (*i).second << endl;
		i++;
	}

	cout << user_passwd["aaa2"] << endl;
}

 

算法 #include<algorithm>

sort(b,e)//排序
sort(b,e,回掉函数)//改变排序方式
unique(b,e);     //如果有重复元素将重复元素去掉 使用该算法前,要先对元素进行排序 
find_if(b,e,谓词);//告诉迭代器,可以找到要找的对象
count_if(b,e,谓词);//查找满足某种要求的个数
for_each(b,e,回掉函数);//用于对容器中的元素进行遍历,并对每个元素执行指定的操作。
#include <iostream>
#include<algorithm>

using namespace std;

bool cmp(int a, int b)
{
	return a > b;
}

void show(int data)
{
	cout << data << endl;
}

bool fcmp(int data)
{
	return data == 34;
}

int main()
{
	//vector<int> arr;

	int arr[] = { 1,1234,23,4,23,42,34,23,42,34,2,2,2,444,22 };
	int n = sizeof(arr) / sizeof(int);

	int* p = find_if(arr, arr + n, fcmp);
	if (p != arr + n)
		cout << "got it !\n";

	cout << "num of 34: " << count_if(arr, arr + n, fcmp) << endl;

	/*
		for(int i = 0; i<n; i++)
			cout <<arr[i]<<',';
		cout<<endl;
	*/
	for_each(arr, arr + n, show);

	sort(arr, arr + n);//排序
	//	sort(arr, arr+n, cmp);
	cout << "xxxxxxxxxxxxxxxxxxx\n";
	unique(arr, arr + n);

	for_each(arr, arr + n, show);
	/*
		for(int i = 0; i<n; i++)
			cout <<arr[i]<<',';
		cout<<endl;
	*/
}

C++写一条链表

#include <iostream>
#include <ostream>

using namespace std;

class myList
{
	struct Node//类中结构体 默认是public
	{
		Node(int x, Node* ptr = NULL) :data(x), next(ptr) {}
		int data;
		Node* next;
	};
public:
	myList() :head(NULL) {}//初始化表一个:
	~myList()
	{
		while (head)
		{
			Node* tem = head;
			head = head->next;
			delete tem;
		}
	}

	void insert_head(int data)
	{
		Node* node = new Node(data);
		node->next = head;
		head = node;
	}


	friend ostream& operator<<(ostream& out, const myList& list);
private:
	Node* head;
};

ostream& operator<<(ostream& out, const myList& list)
{
	myList::Node* tem = list.head;
	while (tem)
	{
		out << tem->data << ',';
		tem = tem->next;
	}
	out << endl;

	return out;
}

int main()
{
	myList list;

	list.insert_head(1);
	list.insert_head(2);
	list.insert_head(4);
	list.insert_head(3);

	cout << list;

}

模块化链表

#include <iostream>
#include <ostream>

using namespace std;

template <typename T>//模板化
class myList
{
	struct Node
	{
		Node(T x, Node* ptr = NULL) :data(x), next(ptr) {}
		T data;
		Node* next;
	};
public:
	class iterator
	{
	public:
		iterator(Node* ptr = NULL) :pos(ptr) {}
		iterator& operator++(int)
		{
			if (NULL != pos)
				pos = pos->next;
			return *this;
		}

		int& operator*()
		{
			return pos->data;
		}

		bool operator!=(iterator x)
		{
			return pos != x.pos;
		}
	private:
		Node* pos;
	};
public:
	myList() :head(NULL) {}
	~myList()
	{
		while (head)
		{
			Node* tem = head;
			head = head->next;
			delete tem;
		}
	}

	void insert_head(T data)
	{
		Node* node = new Node(data);
		node->next = head;
		head = node;
	}

	iterator begin()
	{
		return iterator(head);
	}
	iterator end()
	{
		return iterator(NULL);
	}


	template <typename X>//名字失效 模板名字
	friend ostream& operator<<(ostream& out, const myList<X>& list);
private:
	Node* head;
};

template <typename X >//名字失效 模板名字
ostream& operator<<(ostream& out, const myList<X>& list)
{
	typename myList<X>::Node* tem = list.head;
	while (tem)
	{
		out << tem->data << ',';
		tem = tem->next;
	}
	out << endl;

	return out;
}

int main()
{
	myList<int> list;

	list.insert_head(1);
	list.insert_head(2);
	list.insert_head(4);
	list.insert_head(3);

	cout << list;

	myList<int>::iterator i = list.begin();
	while (i != list.end())
	{
		cout << *i << endl;
		i++;
	}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值