栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++STL容器篇(二)

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++STL容器篇(二)

目录

双向链表

队列


双向链表
  • list
    #include
    #include
    #include
    using namespace std;
    //基本操作:操作基本数据类型
    void testList() {
    	liststrList;
    	//插入元素
    	strList.push_back("stringList1");
    	strList.push_back("stringList2");
    	strList.push_back("stringList3");
    	list ::iterator iter;
    	//迭代器方式遍历(不删除遍历)
    	for (iter = strList.begin(); iter != strList.end(); iter++)
    		cout << *iter << endl;
    		cout << endl;
    		cout << "是否为空:" << boolalpha << !strList.empty() << endl;
    		cout << "元素个数:" << strList.size() << endl;
    		//删除方式遍历
    		while (!strList.empty()) {
    			cout << strList.front() << endl;
    			strList.pop_front();//头部删除;  pop_back()  尾部删除
    		}
    		cout << "元素个数:" << strList.size() << endl;//此时元素个数已经变为0
    		listintList;
    		for (int i = 0; i < 3; i++)
    			intList.push_back(i);
    		auto result = find(intList.begin(), intList.end(), 2);//如果没有找到返回的是end(结束)的位置
    		if (result == intList.end())
    			cout << "没找到!!!" << "  ";
    		intList.insert(result, 230);
    		for (auto v : intList)
    			cout << v << "  ";
    		cout << endl;
    		//删除函数
    		intList.erase(result);
    		for (auto v : intList)
    			cout << v << "  ";
    		cout << endl;
    		intList.reverse();//反转链表
    		for (auto v : intList)
    			cout << v << "  ";
    		cout << endl;
    		//从大到小排序;intList.sort(less())从小到大排序;intList.sort()从小到大排序
    		intList.sort(greater());
    		for (auto v : intList)
    			cout << v << "  ";
    		cout << endl;
    }
    void testDelete() {
    	int a[] = { 1,2,2,3 };
    	listdata;
    	data.assign(a, a + 4);
    	list::iterator iter;
    	for (iter = data.begin(); iter != data.end();) {
    		if (*iter == 2)
    			iter=data.erase(iter);
    		else
    			iter++;
    	}
    	for (auto v : data)
    		cout << v << "  ";
    }
    //操作自定类型数据 
    class Nice {
    public:
    	Nice(string name, int age,int num):name(name),age(age),num(num){}
    	void print() {
    		cout << name <<"t" << age << " t " << num << endl;
    	}
    	bool operator==(string name) { return this->name ==name; }
    	bool operator<(const Nice& nice)const { return this->age < nice.age; }
    	bool operator>(const Nice& nice)const { return this->num > nice.num; }
    protected:
    	string name;
    	int age;
    	int num;
    };
    void testNice() {
    	listdata;
    	string name;
    	int age;
    	int num;
    	while (1) {
    		cin >> name >> age >> num;
    		data.push_back(Nice(name, age, num));
    		while (cin.get() != 'n');
    		if (cin.get()=='n')break;	
    	}
    	cout << "姓名:t年龄:t编号:" << endl;
    	for (auto v : data)
    		v.print();
    	auto result = find(data.begin(), data.end(), "name1");
    	data.sort(less());
    	data.sort(greater());
    }
    int main() {
    	
    	testNice();
    
    	return 0;
    }

  • stack
    #include
    #include
    #include
    using namespace std;
    void testStack() {
    //穿脱原则
    //FILO
    //1 2 3
    //3 2 1
    //push(data)
    //pop()删除
    //top 获取栈顶元素
    //empty() size()
    	stackintStack;
    	for (int i = 0; i < 3; i++)
    		intStack.push(i);
    	while (!intStack.empty()){
    		cout << intStack.top() << "  ";
    	intStack.pop();}
    	cout << endl;
    }
    void numToBinary(int data)//转二进制
    {
    	stackbin;
    	while (data) {
    		bin.push(data % 2);
    		data = data / 2;
    	}
    	for (int i = bin.size(); i < 8; i++)
    		bin.push(0);
    	while (!bin.empty()) {
    		cout << bin.top();
    		bin.pop();
    	}
    }
    int main() {
    	testStack();
    	numToBinary(8);
    
    
    	return 0;
    }

队列
  • queue/deque/priority_queue
    #include
    #include
    #include
    using namespace std;
    void testQueue() {
    	queuedata;
    	for (int i = 0; i < 3; i++)
    		data.push(i);//入队
    	while (!data.empty()) {
    		cout << data.front() << "  ";//获取队头元素
    		data.pop();//出队
    	}
    	cout << endl;
    	cout << data.size() << endl;
    
    }
    int main() {
    	testQueue();
    
    	return 0;
    }
    #include
    #include
    #include
    using namespace std;
    //从头出来
    void pop_frontData(dequedata) {
    	while (!data.empty()) {
    		cout << data.front() << "  ";
    		data.pop_front();
    	}
    	cout << endl;
    }
    //从尾出来
    void pop_backData(dequedata) {
    	while (!data.empty()) {
    		cout << data.back() << "  ";
    		data.pop_back();
    	}
    	cout << endl;
    }
    void testDeque() {
    	dequedata;
    	for (int i = 0; i < 3; i++){
    		data.push_back(i);//尾插法入队
    		data.push_front(i);//头插法入队
    }
    	data.push_back(456);
    	pop_frontData(data);
    	pop_backData(data);
    }
    int main() {
    	testDeque();
    
    
    	return 0;
    }
    #include
    #include
    #include
    using namespace std;
    void testCreatePriority_queue() {
    	priority_queuedata;
    	priority_queue>data2;//默认排序准则
    	priority_queue, less>data3;//完整函数参数,从大到小出队
    	//优先队列,按照数据的优先顺序出队,VIP服务
    	data.push(12);
    	data.push(23);
    	data.push(34);
    	while (!data.empty()) {
    		cout << data.top() << " ";
    		data.pop();//出队
    	}
    	cout << endl;
    	priority_queue, greater>data4;//从小到大出队
    	data4.push(12);
    	data4.push(23);
    	data4.push(34);
    	while (!data4.empty()) {
    		cout << data4.top() << " ";
    		data4.pop();//出队
    	}
    }
    int main() {
    	testCreatePriority_queue();
    
    	return 0;
    }

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/664709.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号