欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

list

程序员文章站 2022-07-14 08:53:40
...

list

// list.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<list>
#include<string>
#include<algorithm>
using namespace std;
/*void test01()
{
	list<int> myList;
	for (int i = 0; i < 10; i++)
	{
		myList.push_back(i);

	}
	list<int>::_Nodeptr node = myList._Myhead->_Next;
	for (int i = 0; i < myList._Mysize * 2; i++)
	{
		cout << "Node" << node->_Myval << endl;
		node = node->_Next;
		if (node == myList._Myhead)
		{
			node = node->_Next;
		}
	}
}*/
void printList(list<int>& l)
{
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}
void test02()
{

	list<int>list1(10, 10);
	list<int>list2(list1.begin(), list1.end());
	printList(list1);
	printList(list2);
	list2.push_back(100);

	for (list<int>::reverse_iterator it = list1.rbegin(); it != list1.rend(); it++)
	{
		cout << *it << " ";

	}
	cout << endl;

	/*list不支持随机访问
	list<int>::iterator itb = list2.begin();
	ibt = itb + 1;
	*/

	//插入数据
	list<int>list3;
	list3.push_back(10);
	list3.push_back(20);
	list3.push_back(30);//尾插
	list3.push_front(100);//头插
	list3.push_front(200);
	list3.push_front(300);//300 200 100 10 20 30 
	printList(list3);

	//删除数据
	list3.pop_back();//尾删 300 200 100 10 20
	list3.pop_front();//头删 200 100 10 20

	//insert
	list3.insert(list3.begin(), 1000);//1000  200 100 10 20
	printList(list3);//

	list3.push_back(10);// 1000  200 100 10 20 10
	list3.remove(10);
	printList(list3); //1000  200 100  20
}
void test03()
{
	list<int>list3;
	list3.push_back(10);
	list3.push_back(20);
	list3.push_back(30);//尾插
	list3.push_front(100);//头插
	list3.push_front(200);
	cout << "大小" << list3.size() << endl;
	if (list3.empty())
	{
		cout << "kong " << endl;
	}
	else {
		cout << "bu kong" << endl;
	}

	list3.resize(10);//多的补0
	list3.resize(3);//少了删除
	printList(list3); //200  300  10
	

	list<int> list4;
	list4.assign(list3.begin(), list3.end());

	cout << "front" << list4.front() << endl;//200
	cout << "back" << list4.back() << endl;//10
}
//翻转排序
bool mycompare(int v1, int v2)
{
	return v1 > v2;
}
void test4()
{
	list<int>l;
	l.push_back(10);
	l.push_back(20);
	l.push_back(30);
	l.push_back(40);
	l.reverse();
	printList(l);

	//所有不支持随机访问的迭代器 不可以用系统提供的算法
	//如果不支持系统提供算法,那么这个类内部会提供
	//sort(l.begin(), l.end());
	l.sort();//从小到大
	printList(l);
	l.sort(mycompare);//从大到小
	printList(l);
}
//自定义数据类型
class Person{
public:
	Person(string name, int age,int h)
	{
		this->name = name;
		this->age = age;
		this->height = h;
	}
public:
	string name;
	int age;
	int height;
};
bool mycompareperson(Person& p1, Person& p2)
{
	if (p1.age == p2.age)
	{
		return p1.height < p2.height;
	}
	return p1.age > p2.age;
	
}
void test5()
{
	list<Person> l;
	Person p1("aa", 1,156);
	Person p2("bb", 2,166);
	Person p3("cc", 3,176);
	Person p4("dd", 4,186);
	Person p5("ee", 5,196);
	Person p6("ff", 5, 189);
	Person p7("gg", 5, 178);
	l.push_back(p1);
	l.push_back(p2);
	l.push_back(p3);
	l.push_back(p4);
	l.push_back(p5);
	l.push_back(p6);
	l.push_back(p7);
	//打印数据时要按照年龄的降序输出  年龄相同 按照身高升序排序
	//对于自定义数据类型 必须要制定规则
	l.sort(mycompareperson);
	for (list<Person>::iterator i = l.begin(); i != l.end(); i++)
	{
		cout << "name"<<i->name << "你de、年龄是" << i->age <<"身高"<<i->height<< endl;
		
	}

}
int main()
{
	test5();
    return 0;
}


remove删除自定义数据类型

// list.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
#include<list>
#include<string>
#include<algorithm>
using namespace std;
/*void test01()
{
	list<int> myList;
	for (int i = 0; i < 10; i++)
	{
		myList.push_back(i);

	}
	list<int>::_Nodeptr node = myList._Myhead->_Next;
	for (int i = 0; i < myList._Mysize * 2; i++)
	{
		cout << "Node" << node->_Myval << endl;
		node = node->_Next;
		if (node == myList._Myhead)
		{
			node = node->_Next;
		}
	}
}*/
void printList(list<int>& l)
{
	for (list<int>::iterator it = l.begin(); it != l.end(); it++)
	{
		cout << *it << "  ";
	}
	cout << endl;
}
void test02()
{

	list<int>list1(10, 10);
	list<int>list2(list1.begin(), list1.end());
	printList(list1);
	printList(list2);
	list2.push_back(100);

	for (list<int>::reverse_iterator it = list1.rbegin(); it != list1.rend(); it++)
	{
		cout << *it << " ";

	}
	cout << endl;

	/*list不支持随机访问
	list<int>::iterator itb = list2.begin();
	ibt = itb + 1;
	*/

	//插入数据
	list<int>list3;
	list3.push_back(10);
	list3.push_back(20);
	list3.push_back(30);//尾插
	list3.push_front(100);//头插
	list3.push_front(200);
	list3.push_front(300);//300 200 100 10 20 30 
	printList(list3);

	//删除数据
	list3.pop_back();//尾删 300 200 100 10 20
	list3.pop_front();//头删 200 100 10 20

	//insert
	list3.insert(list3.begin(), 1000);//1000  200 100 10 20
	printList(list3);//

	list3.push_back(10);// 1000  200 100 10 20 10
	list3.remove(10);
	printList(list3); //1000  200 100  20
}
void test03()
{
	list<int>list3;
	list3.push_back(10);
	list3.push_back(20);
	list3.push_back(30);//尾插
	list3.push_front(100);//头插
	list3.push_front(200);
	cout << "大小" << list3.size() << endl;
	if (list3.empty())
	{
		cout << "kong " << endl;
	}
	else {
		cout << "bu kong" << endl;
	}

	list3.resize(10);//多的补0
	list3.resize(3);//少了删除
	printList(list3); //200  300  10
	

	list<int> list4;
	list4.assign(list3.begin(), list3.end());

	cout << "front" << list4.front() << endl;//200
	cout << "back" << list4.back() << endl;//10
}
//翻转排序
bool mycompare(int v1, int v2)
{
	return v1 > v2;
}
void test4()
{
	list<int>l;
	l.push_back(10);
	l.push_back(20);
	l.push_back(30);
	l.push_back(40);
	l.reverse();
	printList(l);

	//所有不支持随机访问的迭代器 不可以用系统提供的算法
	//如果不支持系统提供算法,那么这个类内部会提供
	//sort(l.begin(), l.end());
	l.sort();//从小到大
	printList(l);
	l.sort(mycompare);//从大到小
	printList(l);
}
//自定义数据类型
class Person{
public:
	Person(string name, int age,int h)
	{
		this->name = name;
		this->age = age;
		this->height = h;
	}
	//重载== 让remove可以删除自定义的Person类型
	bool operator==(const Person& another)
	{
		if (this->age == another.age && this->height == another.height &&this->name == another.name)
			return true;
		else return false;
	}
public:
	string name;
	int age;
	int height;
};
bool mycompareperson(Person& p1, Person& p2)
{
	if (p1.age == p2.age)
	{
		return p1.height < p2.height;
	}
	return p1.age > p2.age;
	
}
void test5()
{
	list<Person> l;
	Person p1("aa", 1,156);
	Person p2("bb", 2,166);
	Person p3("cc", 3,176);
	Person p4("dd", 4,186);
	Person p5("ee", 5,196);
	Person p6("ff", 5, 189);
	Person p7("gg", 5, 178);
	l.push_back(p1);
	l.push_back(p2);
	l.push_back(p3);
	l.push_back(p4);
	l.push_back(p5);
	l.push_back(p6);
	l.push_back(p7);
	//打印数据时要按照年龄的降序输出  年龄相同 按照身高升序排序
	//对于自定义数据类型 必须要制定规则
	l.sort(mycompareperson);
	for (list<Person>::iterator i = l.begin(); i != l.end(); i++)
	{
		cout << "name"<<i->name << "你的年龄是" << i->age <<"身高"<<i->height<< endl;
		
	}
	//删除gg
	l.remove(p6);//可以直接remove基础数据类型  但是p6 是自定义数据类型
	cout << "---------------------------------" << endl;
	for (list<Person>::iterator i = l.begin(); i != l.end(); i++)
	{
		cout << "name" << i->name << "你的年龄是" << i->age << "身高" << i->height << endl;

	}

}
int main()
{
	test5();
    return 0;
}


上一篇: POJ 1471Triangles

下一篇: List